200 likes | 211 Views
Learn about what deadlocks are, their characteristics, and various methods for preventing, avoiding, detecting, and handling deadlocks in a system.
E N D
Chapter 5 Deadlocks
Contents • What is deadlock? • Characterization • Resource allocation graph • Methods for handling deadlocks • Prevention • Avoidance • Detection
What is deadlock? • System has several resources and several instances of each resource • Request can be satisfied by allocation of any instance of the type • Request made before use and released after use • Processes may make any number of resources –same or different type • Request and release are system calls • Ex. Request and release device, open and close files, allocate and free memory
What is deadlock? • 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 • Events are resource acquisition and release • 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
What is deadlock? • 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 • Multithread programs are good candidates for deadlocks
Characterization • In a deadlock, processes never finish executing and system resources are tied up, preventing other jobs from starting • Features that characterize deadlock: • Mutual exclusion • Hold and wait • No preemption • Circular wait • All these conditions must hold simultaneously in a system for deadlocks to occur
Resource allocation graph • 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 • 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 • Cycle in the graph deadlock may exist
Methods for handling deadlocks • Prevention or avoidance • After deadlock has occurred, detect it and recover • Ignore the problem – pretend the problem never occurs • 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
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 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 (3rd) 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
Prevention • Mutual exclusion must hold for non-sharable resources – printers • For resources like read only files it can be denied • Mutual exclusion for all resources can’t be denied • Hold and wait: • Request granted only when no other is being held • Allow request only when process has none
Difference between 2 methods • Ex. Copy data from tape to disk, sort the disk file and print • Process must request tape, disk, printer perform copy, sort and print – printer is held though used much later • 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 • Disadvantage: low resource utilization and starvation
No preemption • Can this condition be broken? • Possible: • If at the time of request the resource can’t be allocated immediately, preempt all resources currently held – implicitly released • Has to make request again for old and new resources
No preemption • When request is made check availability • If available make allocation • 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 • 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 • Process starts only all preempted and new requests are granted
Circular wait • How this be broken? • Processes request for resources in increasing order of enumeration • 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 • Should it require disk drive, printer must be released
Avoidance • Prevention: • Prevent deadlocks by regulating requests and grants • Restraints ensure that at least one of the four conditions is broken and hence deadlocks can’t happen • Avoidance: • Gather additional information about how resources are to be requested by each process • Ex. P require tape drive and printer • Q require printer and tape drive • This situation can cause deadlock • 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 • Resource allocation algorithm and banker’s algorithm – to avoid deadlock
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 • Ensure that unsafe state never occur • When system enters unsafe state deadlocks can happen • Ex. R1 (tape drive) -12 units. P1, P2, P3 are 3 processes demanding R1.
Safe state P1 P2 P3 Max Needs 10 4 9 Current allocation 5 2 2 (at time t0) No. of free R1 = 3 At time t0 system is in safe state. <P2,P1,P3> satisfies safety conditions. Suppose at time t1 P3 requests and one more of R1 and is allocated. System is no more in a safe state.
Sample P1 P2 P3 Max Needs 10 4 9 Current allocation 5 2 3 (at time t0) No. of free R1 = 2 • No sequence of processes satisfy safety conditions. Hence unsafe state. Potential deadlock. • Mistake is in granting the request of P3 • Request to be granted only if it leaves the system in safe state
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)
Sample • Deadlock