Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money Deadlocks • Computer contain many resources – Some can be shared – The rest cannot be shared • Examples of non-shared resources are – Printers – Tape drives – Special memory addresses – File access Deadlocks • Example: Printing – Imagine if two processes had access to the printer simultaneously – One line might from process one – The second might be from process two – And repeating… • Solution: grant temporary exclusive access to a shared resource Deadlocks • Many times, processes need access to 2+ • • • • resources at the same time Two processes are scanning a document and writing it to CD First process scans, then tries to write to CD Second process locks the CD first, then tries to scan Both processes block! This is called a deadlock. Deadlocks • Same situation can occur across multiple machines as well • Many times on a network, these same devices are shared and the same problem occurs • It may involve 3,4,5+ users and machines Resources • Deadlocks occur when processes exclusive access to devices, file, etc. • To simplify, we refer to any object that can grant exclusive access as a resource. • In a given computer system there are 10s100s resources to be managed. Preemptable and Nonpreemptable Resources • There are two types of resources: – Preemptable – resource that can be taken away from a process without any ill effects • Memory • Usually does not cause deadlocks – Nonpreemtable – resource that cannot be taken away from a process without having a computation fail • CD Writer • We focus on this for deadlocks Resources • The sequence to use a resource is: – Request the resource – Use the resource – Release the resource • If the resource is not available when requesting, the process is forced to wait, either: – Blocking – Returns an error code and process tries again later Resource Acquisition • For some resources, such as a database system, the user process can manage the resources • One way is to use a semaphore, as in critical regions Resource Acquisition typedef int semaphore; semaphore resource1; void processA(void){ down(&resource1); use_resource1(); up(&resource1); } Resource Acquisition – 2 Resources typedef int semaphore; semaphore resource1; semaphore resource2; void processA(void){ down(&resource1); down(&resource2); usebothresources(); up(&resource1); up(&resource2); } Resource Acquisition • Only one process involved so far • What happens when we go to two processes? Resource Acquisition typedef int semaphore; semaphore resource1; semaphore resource2; void processA(void){ down(&resource1); down(&resource2); usebothresources(); up(&resource2); up(&resource1); } void processB(void){ down(&resource1); down(&resource2); usebothresources(); up(&resource2); up(&resource1); } Resource Acquisition typedef int semaphore; semaphore resource1; semaphore resource2; void processA(void){ down(&resource1); down(&resource2); usebothresources(); up(&resource2); up(&resource1); } void processB(void){ down(&resource2); down(&resource1); usebothresources(); up(&resource1); up(&resource2); } Introduction to Deadlocks • Deadlocks are formally defined by – A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause • Since they are all waiting, none of them will wake up • Assumption of no interrupts and single threads Conditions for Deadlock 1. Mutual Exclusion – each resource is either 2. 3. 4. currently assigned to one process or is available Hold and Wait – processes currently holding resources can request new resources No preemption – Resources previously granted cannot forcibly be taken away from the process. They must be released by the process Circular Wait – there must be a circular chain of 2+ processes, each whom is waiting for a resource held by the next member of the chain Conditions for Deadlock • All four conditions must exist for a deadlock to occur • If one is absent, deadlock cannot occur • Many of these are related to system policy choices Deadlock Modeling • Holt showed in 1972 that the four conditions could be modeled using direct graphs • The graphs have two types of nodes: – Processes – shown as circles – Resources – shown as squares Deadlock Modeling • The arc from process to graph and vice versa determines if the resource is held or being waited on: – An arc from a resource (square) to a process (circle) indicates the resource is held by that process – An arc from a process to a resource indicates the process is blocked waiting for the resource Deadlock Modeling Dealing with Deadlocks • Ignore the problem, maybe it will ignore you? – Used by UNIX and Windows • Detection and Recovery • Dynamic avoidance by careful resource allocation • Prevention by structurally negating one of the four conditions for deadlocks Ostrich Algorithm • Here we stick our head in the sand like an ostrich and pretend there is no problem • Different reactions – Mathematicians say it is unacceptable and must be prevented all costs – Engineers might ask how often does it really occur and how serious the situation is Ostrich Algorithm • Most operating systems suffer from this approach • Example: Unix Process table – Fixed length and finite – If fork() fails because the table is full, program waits and tries again (/bin/login does this, for example) Ostrich Algorithm • However, consider this: • Assume 100 process table slots • Ten programs run that need to create 12 sub-processes each • After each program creates 9 processes, the table is full • No process finishes because it keeps trying to fork! Ostrich Algorithm • Do we abandon the processes and the fork calls? • Similar to this is the maximum number of open files, which is restricted by the size of the i-node table • The solution is not obvious or easy, which is why most operating systems choose this approach