Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Module 4 Deadlock in Operating System

3,423 views

Published on

Published in: Education

Module 4 Deadlock in Operating System

  1. 1. Module 4 - Deadlock Hemang Kothari Assistant Professor Computer Engineering Department MEFGI, Rajkot. Email: hemang.kothari@marwadieducation.edu.in Slides: http://www.slideshare.net/hemangkothari 2/20/2014 Computer Engineering Department - MEFGI 1
  2. 2. Content • • • • • • Deadlock Problem Deadlock Characterization Deadlock Detection Deadlock recovery Deadlock avoidance Deadlock Prevention. 2/20/2014 Computer Engineering Department - MEFGI 2
  3. 3. Motivation 2/20/2014 Computer Engineering Department - MEFGI 3
  4. 4. Technical Issue 2/20/2014 Computer Engineering Department - MEFGI 4
  5. 5. What is Deadlock • A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. • Example – System has 2 tape drives. – P1 and P2 each hold one tape drive and each needs another one. 2/20/2014 Computer Engineering Department - MEFGI 5
  6. 6. Deadlock Continue • Traffic only in one direction. • Each section of a bridge can be viewed as a resource. • If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). • Several cars may have to be backed up if a deadlock occurs. • Starvation is possible. 2/20/2014 Computer Engineering Department - MEFGI 6
  7. 7. Technical Terms • Formal definition: 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 – Usually the event is release of a currently held resource • None of the processes can … – run – release resources 2/20/2014 Computer Engineering Department - MEFGI 7
  8. 8. Deadlock Characterization • Mutual exclusion: only one process at a time can use a resource. Four conditions • Hold and wait: a process holding at least one resource is waiting to acquire hold additional resources held by other simultaneously processes. then and then only • No preemption: a resource can be released only voluntarily deadlock arise by the process holding it, after that process has completed its task. • Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. 2/20/2014 Computer Engineering Department - MEFGI 8
  9. 9. System Model • Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices • Each resource type Ri has Wi instances. • Each process utilizes a resource as follows: – request – use – release 2/20/2014 Computer Engineering Department - MEFGI 9
  10. 10. Deadlock Modeling • Modeled with directed graphs – resource R assigned to process A – process B is requesting/waiting for resource S – process C and D are in deadlock over resources T and U 2/20/2014 Computer Engineering Department - MEFGI 10
  11. 11. Modeling  Strategies for dealing with Deadlocks • Just ignore the problem altogether • Detection and recovery • Dynamic avoidance – Careful resource allocation • Prevention – negating one of the four necessary conditions 2/20/2014 Computer Engineering Department - MEFGI 11
  12. 12. How it Occurs 2/20/2014 Computer Engineering Department - MEFGI 12
  13. 13. How can we avoid 2/20/2014 Computer Engineering Department - MEFGI 13
  14. 14. Example of a Resource Allocation Graph 2/20/2014 Computer Engineering Department - MEFGI 14
  15. 15. Resource Allocation Graph With A Deadlock 2/20/2014 Computer Engineering Department - MEFGI 15
  16. 16. Resource Allocation Graph With A Cycle But No Deadlock 2/20/2014 Computer Engineering Department - MEFGI 16
  17. 17. Moral of the Story • If graph contains no cycles no deadlock. • If graph contains a cycle – if only one instance per resource type, then deadlock. – if several instances per resource type, possibility of deadlock. 2/20/2014 Computer Engineering Department - MEFGI 17
  18. 18. Methods to handle Deadlock • Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. • Ensure that the system will never enter a deadlock state. • Allow the system to enter a deadlock state and then recover. 2/20/2014 Computer Engineering Department - MEFGI 18
  19. 19. st 1 Solution - The Ostrich Algorithm • Pretend there is no problem • Reasonable if – deadlocks occur very rarely – cost of prevention is high • UNIX and Windows takes this approach • It is a trade off between – convenience – correctness 19
  20. 20. 2nd Solution - Deadlock Prevention Restrain the ways request can be made • Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources. • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. – Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. – Low resource utilization; starvation possible. 2/20/2014 Computer Engineering Department - MEFGI 20
  21. 21. Deadlock Prevention (cont) • No Preemption – – If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. – Preempted resources are added to the list of resources for which the process is waiting. – Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. • Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 2/20/2014 Computer Engineering Department - MEFGI 21
  22. 22. 3rd Deadlock Avoidance • If we have future information – Max resource requirement of each process before they execute • Can we guarantee that deadlocks will never occur? • Avoidance Approach: – Before granting resource, check if state is safe – If the state is safe ? no deadlock! 2/20/2014 Computer Engineering Department - MEFGI 22
  23. 23. Safe State • When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. • System is in safe state if there exists a safe sequence of all processes. • Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<I. • If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. • When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. • When Pi terminates, Pi+1 can obtain its needed resources, and so on. 2/20/2014 Computer Engineering Department - MEFGI 23
  24. 24. Example of Safe State • Suppose there are 12 tape drives p0 p1 p2 max need 10 4 9 current usage 5 2 2 3 drives remain could ask for 5 2 7 • Current state is safe because a safe sequence exists: <p1,p0,p2> p1 can complete with current resources p0 can complete with current+p1 p2 can complete with current +p1+p0 • If p2 requests 1 drive, then it must wait to avoid unsafe state. 2/20/2014 Computer Engineering Department - MEFGI 24
  25. 25. 2/20/2014 Computer Engineering Department - MEFGI 25
  26. 26. Banker’s Algorithm • Suppose we know the “worst case” resource needs of processes in advance – A bit like knowing the credit limit on your credit cards. (This is why they call it the Banker’s Algorithm) • Observation: Suppose we just give some process ALL the resources it could need… – Then it will execute to completion. – After which it will give back the resources. • So… – A process pre-declares its worst-case needs – Then it asks for what it “really” needs, a little at a time – The algorithm decides when to grant requests • It delays a request unless: – It can find a sequence of processes such that it could grant their outstanding need. So they would terminate. letting it collect their resources and in this way it can execute everything to completion 2/20/2014 Computer Engineering Department - MEFGI 26
  27. 27. More Technically • Decides whether to grant a resource request. • Data structures: n: integer # of processes m: integer # of resources available[1..m] - available[i] is # of avail resources of type i max[1..n,1..m] - max demand of each Pi for each Ri allocation[1..n,1..m] - current allocation of resource Rj to Pi need[1..n,1..m] max # resource Rj that Pi may still request let request[i] be vector of # of resource Rj Process Pi wants 2/20/2014 Computer Engineering Department - MEFGI 27
  28. 28. Implementation Code 1. If request[i] > need[i] then error (asked for too much) 2. If request[i] > available[i] then wait (can’t supply it now) 3. Resources are available to satisfy the request Let’s assume that we satisfy the request. Then we would have: available = available - request[i] allocation[i] = allocation [i] + request[i] need[i] = need [i] - request [i] Now, check if this would leave us in a safe state: if yes, grant the request, if no, then leave the state as is and cause process to wait. 2/20/2014 Computer Engineering Department - MEFGI 28
  29. 29. Safety Check free[1..m] = available /* how many resources are available */ finish[1..n] = false (for all i) /* none finished yet */ Step 1: Find an i such that finish[i]=false and need[i] <= work /* find a proc that can complete its request now */ if no such i exists, go to step 3 /* we’re done */ Step 2: Found an i: finish [i] = true /* done with this process */ free = free + allocation [i] /* assume this process were to finish, and its allocation back to the available list */ go to step 1 Step 3: If finish[i] = true for all i, the system is safe. Else Not
  30. 30. Example of Banker’s Algorithm • 5 processes P0 through P4; 3 resource types A (10 instances), B (5instances, and C (7 instances). • Snapshot at time T0: Allocation Max Available ABC ABC ABC P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 Operating System Concepts
  31. 31. Example (Cont.) • The content of the matrix. Need is defined to be Max – Allocation. Need ABC P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 • The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria. Operating System Concepts
  32. 32. Example P1 Request (1,0,2) (Cont.) • Check that Request Available (that is, (1,0,2) (3,3,2) true. Allocation Need Available ABC ABC ABC P0 0 1 0 743 230 P1 3 0 2 020 P2 3 0 1 600 P3 2 1 1 011 P4 0 0 2 431 • Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement. • Can request for (3,3,0) by P4 be granted? • Can request for (0,2,0) by P0 be granted?
  33. 33. Review • A system has four processes and five allocatable resources. The current allocation and maximum needs are as follows: Allocated Maximum Available Process A 1 0 2 1 1 11212 00x11 Process B 2 0 1 1 0 22210 Process C 1 1 0 1 0 21310 Process D 1 1 1 1 0 11221 • What is the smallest value of x for which this is a safe state? 2/20/2014 Computer Engineering Department - MEFGI 33
  34. 34. Solution • The needs matix is as follows: 01001 02100 10300 00111 • If x is 0, we have a deadlock immediately. • If x is 1, process D can run to completion. When it is finished, the available vector is 1 1 2 2 1. • Now A can run to complete, the available vector then becomes 2 1 4 3 2. • Then C can run and finish, return the available vector as 3 2 4 4 2. • Then B can run to complete. Safe sequence D A C B. 2/20/2014 Computer Engineering Department - MEFGI 34
  35. 35. Deadlock Detection & Recovery • Allow system to enter deadlock state • Detection algorithm • Recovery scheme 2/20/2014 Computer Engineering Department - MEFGI 35
  36. 36. Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph
  37. 37. Recovery • Recovery through preemption – take a resource from some other process – depends on nature of the resource • Recovery through rollback – checkpoint a process periodically – use this saved state – restart the process if it is found deadlocked 2/20/2014 Computer Engineering Department - MEFGI 37
  38. 38. Recovery • Recovery through killing processes – crudest but simplest way to break a deadlock – kill one of the processes in the deadlock cycle – the other processes get its resources – choose process that can be rerun from the beginning 2/20/2014 Computer Engineering Department - MEFGI 38
  39. 39. 1. Create the need matrix (max-allocation) 2. Use the safety algorithm to test if the system is in a safe state. 3. If the system is in a safe state, can the following requests be granted, why or why not? Please also run the safety algorithm on each request as necessary. a. P1 requests (2,1,1,0) b. P1 requests (0,2,1,0) 2/20/2014 Computer Engineering Department - MEFGI 39
  40. 40. Solution – Need Matrix 2/20/2014 Computer Engineering Department - MEFGI 40
  41. 41. Use the safety algorithm to test if the system is in a safe state – 2nd Solution Check to see if need-0 (0,1,0,0) is less than or equal to work. It is, so let’s set finish to true for that process and also update work by adding the allocated resources (0,1,1,0) for that process to work 2/20/2014 Computer Engineering Department - MEFGI 41
  42. 42. Continue Now, let’s check to see if need-1 (0,4,2,1) <= work. Remember that we have to check each element of the vector need-1 against the corresponding element in work. Because 1 is not less than 0 (the fourth element), we need to move on to P2. Need2 (1,0,0,1) is not less than work, so must move on to P3. Need3 (0,0,2,0) is less than work, so we can update work and finish. 2/20/2014 Computer Engineering Department - MEFGI 42
  43. 43. Next, let’s look at P4. Need-4 (0,6,4,2) is less than work, so we can update work and finish as follows: Now we can go back up to P1. Need1 (0,4,2,1) is less than work, so let’s update work and finish 2/20/2014 Computer Engineering Department - MEFGI 43
  44. 44. Finally, let’s look at P2. Need2 (1,0,0,1) is less than work, so we can then say that the system is in a safe state and the processes will be executed in the following order: P0,P3,P4,P1,P2 2/20/2014 Computer Engineering Department - MEFGI 44
  45. 45. 3rd Solution • If the system is in a safe state, can the following requests be granted, why or why not? Please also run the safety algorithm on each request as necessary. • P1 requests (2,1,1,0) – We cannot grant this request, because we do not have enough available instances of resource A. • P1 requests (0,2,1,0) – There are enough available instances of the requested resources, so first let’s pretend to accommodate the request and see what the system looks like: – system is in a safe state when the processes are run in the following order: P0,P3,P4,P1,P2. We therefore can grant the resource request 2/20/2014 Computer Engineering Department - MEFGI 45

×