Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Deadlocks. Contents What is deadlock? What is deadlock? Characterization Characterization Resource allocation graph Resource allocation graph.

Similar presentations


Presentation on theme: "Chapter 5 Deadlocks. Contents What is deadlock? What is deadlock? Characterization Characterization Resource allocation graph Resource allocation graph."— Presentation transcript:

1 Chapter 5 Deadlocks

2 Contents What is deadlock? What is deadlock? Characterization Characterization Resource allocation graph Resource allocation graph Methods for handling deadlocks Methods for handling deadlocks Prevention Prevention Avoidance Avoidance Detection Detection

3 What is deadlock? System has several resources and several instances of each resource System has several resources and several instances of each resource Request can be satisfied by allocation of any instance of the type Request can be satisfied by allocation of any instance of the type Request made before use and released after use Request made before use and released after use Processes may make any number of resources – same or different type Processes may make any number of resources – same or different type Request and release are system calls Request and release are system calls Ex. Request and release device, open and close files, allocate and free memory Ex. Request and release device, open and close files, allocate and free memory

4 What is deadlock? A set of processes are in deadlock state if A set of processes are in deadlock state if when every process is waiting for an event that can be caused only by another process in the set when every process is waiting for an event that can be caused only by another process in the set Events are resource acquisition and release Events are resource acquisition and release Resource can be physical (disks, printers) or logical (files) Resource can be physical (disks, printers) or logical (files) Ex. Suppose there are 3 tape drives processes P1, P2, P3 are holding one each and if each is making a request for another – P1,P2, P3 are in deadlock state Ex. Suppose there are 3 tape drives processes P1, P2, P3 are holding one each and if each is making a request for another – P1,P2, P3 are in deadlock state

5 What is deadlock? Deadlock is possible that involves different resource types Deadlock is possible that involves different resource types Ex. System has 1 printer and 1 tape drive. Suppose that P1 is holding the tape drive and P2 holding the printer and P1 requesting printer and P2 requesting tape drive – a deadlock has occurred Ex. System has 1 printer and 1 tape drive. Suppose that P1 is holding the tape drive and P2 holding the printer and P1 requesting printer and P2 requesting tape drive – a deadlock has occurred Multithread programs are good candidates for deadlocks Multithread programs are good candidates for deadlocks

6 Characterization In a deadlock, processes never finish executing and system resources are tied up, preventing other jobs from starting In a deadlock, processes never finish executing and system resources are tied up, preventing other jobs from starting Features that characterize deadlock: Features that characterize deadlock: Mutual exclusion Mutual exclusion Hold and wait Hold and wait No preemption No preemption Circular wait Circular wait All these conditions must hold simultaneously in a system for deadlocks to occur All these conditions must hold simultaneously in a system for deadlocks to occur

7 Resource allocation graph Make 2 sets of vertices – one each for all active processes and other one for each resource Make 2 sets of vertices – one each for all active processes and other one for each resource A directed edge from P1 to R3 means that P1 has made request to R3 and waiting for allocation – request edge – inserted at the time request A directed edge from P1 to R3 means that P1 has made request to R3 and waiting for allocation – request edge – inserted at the time request Directed edge from R4 to P5 means that R4 has been allocated to P5 – assignment edge – request edge changes to assignment edge when request fulfilled – deleted after use and release Directed edge from R4 to P5 means that R4 has been allocated to P5 – assignment edge – request edge changes to assignment edge when request fulfilled – deleted after use and release No cycle in the graph – no set of processes in deadlock state No cycle in the graph – no set of processes in deadlock state Cycle in the graph deadlock may exist Cycle in the graph deadlock may exist

8 Methods for handling deadlocks 1.Prevention or avoidance 2.After deadlock has occurred, detect it and recover 3.Ignore the problem – pretend the problem never occurs Prevention: Ensure that one of the conditions cannot hold Prevention: Ensure that one of the conditions cannot hold Avoidance: OS will be informed about resources requests of each process during its lifetime – this additional knowledge can be used to decide if process should wait for a particular resource Avoidance: OS will be informed about resources requests of each process during its lifetime – this additional knowledge can be used to decide if process should wait for a particular resource

9 Handling deadlock If prevention or avoidance is not done, deadlock may occur – provide algorithm to detect deadlock and one more to recover from deadlock – second solution If prevention or avoidance is not done, deadlock may occur – provide algorithm to detect deadlock and one more to recover from deadlock – second solution If prevention or detection is done, and if deadlock occur, system performance deteriorate with more and processes asking for deadlocked resources and can collapse – OS reinstalled - third solution If prevention or detection is done, and if deadlock occur, system performance deteriorate with more and processes asking for deadlocked resources and can collapse – OS reinstalled - third solution This (3 rd ) is not viable approach, this is what is being done by most OS – cheaper This (3 rd ) is not viable approach, this is what is being done by most OS – cheaper Other options (wise) are very expensive and additional functions difficult to implement and must be used constantly Other options (wise) are very expensive and additional functions difficult to implement and must be used constantly

10 Prevention Mutual exclusion must hold for non-sharable resources – printers Mutual exclusion must hold for non-sharable resources – printers For resources like read only files it can be denied For resources like read only files it can be denied Mutual exclusion for all resources cant be denied Mutual exclusion for all resources cant be denied Hold and wait: Hold and wait: 1.Request granted only when no other is being held 2.Allow request only when process has none

11 Difference between 2 methods Ex. Copy data from tape to disk, sort the disk file and print Ex. Copy data from tape to disk, sort the disk file and print 1.Process must request tape, disk, printer perform copy, sort and print – printer is held though used much later 2.Allow process to request initially tape and disk to perform copy and sort When this is over release tape and disk. Request for disk and printer and when granted print operation takes place and finally release these two When this is over release tape and disk. Request for disk and printer and when granted print operation takes place and finally release these two Disadvantage: low resource utilization and starvation Disadvantage: low resource utilization and starvation

12 No preemption Can this condition be broken? Can this condition be broken? Possible: Possible: 1.If at the time of request the resource cant be allocated immediately, preempt all resources currently held – implicitly released 2.Has to make request again for old and new resources

13 No preemption 1.When request is made check availability 2.If available make allocation 3.If not, check if allocated to some other process and this process is waiting for additional resources – preempt the requested resources and make allocation to requesting process 4.If resources are not available or held by a waiting process the requesting process is put under wait state While waiting some its resources may be preempted While waiting some its resources may be preempted Process starts only all preempted and new requests are granted Process starts only all preempted and new requests are granted

14 Circular wait How this be broken? How this be broken? Processes request for resources in increasing order of enumeration Processes request for resources in increasing order of enumeration R1 be tape drive, R5 be disk drive, R12 be printer R1 be tape drive, R5 be disk drive, R12 be printer If a process requires tape drive and printer at the same time, first request tape drive and then for printer If a process requires tape drive and printer at the same time, first request tape drive and then for printer Should it require disk drive, printer must be released Should it require disk drive, printer must be released

15 Avoidance Prevention: Prevention: Prevent deadlocks by regulating requests and grants Prevent deadlocks by regulating requests and grants Restraints ensure that at least one of the four conditions is broken and hence deadlocks cant happen Restraints ensure that at least one of the four conditions is broken and hence deadlocks cant happen Avoidance: Avoidance: Gather additional information about how resources are to be requested by each process Gather additional information about how resources are to be requested by each process Ex. P require tape drive and printer Ex. P require tape drive and printer Q require printer and tape drive Q require printer and tape drive This situation can cause deadlock This situation can cause deadlock Use the knowledge gathered to decide whether wait for requests should happen Use the knowledge gathered to decide whether wait for requests should happen Thus avoid deadlock by making the process Q not join the wait queue for tape drive when it is using printer Thus avoid deadlock by making the process Q not join the wait queue for tape drive when it is using printer Resource allocation algorithm and bankers algorithm – to avoid deadlock Resource allocation algorithm and bankers algorithm – to avoid deadlock

16 Safe state State when system can allocate resources to each process in some order and still avoid deadlock – called safe state State when system can allocate resources to each process in some order and still avoid deadlock – called safe state Not all unsafe states are deadlock states Not all unsafe states are deadlock states Ensure that unsafe state never occur Ensure that unsafe state never occur When system enters unsafe state deadlocks can happen When system enters unsafe state deadlocks can happen Ex. R1 (tape drive) -12 units. P1, P2, P3 are 3 processes demanding R1. Ex. R1 (tape drive) -12 units. P1, P2, P3 are 3 processes demanding R1.

17 Safe state P1 P2 P3 P1 P2 P3 Max Needs 10 4 9 Current allocation 5 2 2 (at time t 0 ) No. of free R1 = 3 At time t 0 system is in safe state. satisfies safety conditions. Suppose at time t 1 P3 requests and one more of R1 and is allocated. System is no more in a safe state.

18 Sample P1 P2 P3 P1 P2 P3 Max Needs 10 4 9 Current allocation 5 2 3 (at time t 0 ) No. of free R1 = 2 No sequence of processes satisfy safety conditions. Hence unsafe state. Potential deadlock. No sequence of processes satisfy safety conditions. Hence unsafe state. Potential deadlock. Mistake is in granting the request of P3 Mistake is in granting the request of P3 Request to be granted only if it leaves the system in safe state Request to be granted only if it leaves the system in safe state

19 Resource allocation graph - revisited Same as before except that we make dashed edges when there is a claim for a resource (all requests for resources should be filed before) Same as before except that we make dashed edges when there is a claim for a resource (all requests for resources should be filed before)

20 Sample Deadlock Deadlock


Download ppt "Chapter 5 Deadlocks. Contents What is deadlock? What is deadlock? Characterization Characterization Resource allocation graph Resource allocation graph."

Similar presentations


Ads by Google