Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.

Similar presentations


Presentation on theme: "Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money."— Presentation transcript:

1 Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

2 Deadlocks Computer contain many resources Computer contain many resources –Some can be shared –The rest cannot be shared Examples of non-shared resources are Examples of non-shared resources are –Printers –Tape drives –Special memory addresses –File access

3 Deadlocks Example: Printing 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 Solution: grant temporary exclusive access to a shared resource

4 Deadlocks Many times, processes need access to 2+ resources at the same time Many times, processes need access to 2+ resources at the same time Two processes are scanning a document and writing it to CD Two processes are scanning a document and writing it to CD First process scans, then tries to write to CD First process scans, then tries to write to CD Second process locks the CD first, then tries to scan Second process locks the CD first, then tries to scan Both processes block! This is called a deadlock. Both processes block! This is called a deadlock.

5 Deadlocks Same situation can occur across multiple machines as well Same situation can occur across multiple machines as well Many times on a network, these same devices are shared and the same problem occurs Many times on a network, these same devices are shared and the same problem occurs It may involve 3,4,5+ users and machines It may involve 3,4,5+ users and machines

6 Resources Deadlocks occur when processes exclusive access to devices, file, etc. 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. To simplify, we refer to any object that can grant exclusive access as a resource. In a given computer system there are 10s- 100s resources to be managed. In a given computer system there are 10s- 100s resources to be managed.

7 Preemptable and Nonpreemptable Resources There are two types of resources: There are two types of resources: –Preemptable – resource that can be taken away from a process without any ill effects Memory Memory Usually does not cause deadlocks Usually does not cause deadlocks –Nonpreemtable – resource that cannot be taken away from a process without having a computation fail CD Writer CD Writer We focus on this for deadlocks We focus on this for deadlocks

8 Resources The sequence to use a resource is: 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: 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

9 Resource Acquisition For some resources, such as a database system, the user process can manage the resources 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 One way is to use a semaphore, as in critical regions

10 Resource Acquisition typedef int semaphore; semaphore resource1; void processA(void){ down(&resource1);use_resource1();up(&resource1);}

11 Resource Acquisition – 2 Resources typedef int semaphore; semaphore resource1; semaphore resource2; void processA(void){ down(&resource1);down(&resource2);usebothresources();up(&resource1);up(&resource2);}

12 Resource Acquisition Only one process involved so far Only one process involved so far What happens when we go to two processes? What happens when we go to two processes?

13 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);}

14 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);}

15 Introduction to Deadlocks Deadlocks are formally defined by 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 Since they are all waiting, none of them will wake up Assumption of no interrupts and single threads Assumption of no interrupts and single threads

16 Conditions for Deadlock 1. Mutual Exclusion – each resource is either currently assigned to one process or is available 2. Hold and Wait – processes currently holding resources can request new resources 3. No preemption – Resources previously granted cannot forcibly be taken away from the process. They must be released by the process 4. 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

17 Conditions for Deadlock All four conditions must exist for a deadlock to occur All four conditions must exist for a deadlock to occur If one is absent, deadlock cannot occur If one is absent, deadlock cannot occur Many of these are related to system policy choices Many of these are related to system policy choices

18 Deadlock Modeling Holt showed in 1972 that the four conditions could be modeled using direct graphs Holt showed in 1972 that the four conditions could be modeled using direct graphs The graphs have two types of nodes: The graphs have two types of nodes: –Processes – shown as circles –Resources – shown as squares

19 Deadlock Modeling The arc from process to graph and vice versa determines if the resource is held or being waited on: 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

20 Deadlock Modeling

21

22

23 Dealing with Deadlocks Ignore the problem, maybe it will ignore you? Ignore the problem, maybe it will ignore you? –Used by UNIX and Windows Detection and Recovery Detection and Recovery Dynamic avoidance by careful resource allocation Dynamic avoidance by careful resource allocation Prevention by structurally negating one of the four conditions for deadlocks Prevention by structurally negating one of the four conditions for deadlocks

24 Ostrich Algorithm Here we stick our head in the sand like an ostrich and pretend there is no problem Here we stick our head in the sand like an ostrich and pretend there is no problem Different reactions 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

25 Ostrich Algorithm Most operating systems suffer from this approach Most operating systems suffer from this approach Example: Unix Process table 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)

26 Ostrich Algorithm However, consider this: However, consider this: Assume 100 process table slots Assume 100 process table slots Ten programs run that need to create 12 sub-processes each Ten programs run that need to create 12 sub-processes each After each program creates 9 processes, the table is full After each program creates 9 processes, the table is full No process finishes because it keeps trying to fork! No process finishes because it keeps trying to fork!

27 Ostrich Algorithm Do we abandon the processes and the fork calls? 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 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 The solution is not obvious or easy, which is why most operating systems choose this approach


Download ppt "Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money."

Similar presentations


Ads by Google