Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Concurrency: Deadlock and Starvation Chapter 6.

Similar presentations


Presentation on theme: "1 Concurrency: Deadlock and Starvation Chapter 6."— Presentation transcript:

1 1 Concurrency: Deadlock and Starvation Chapter 6

2 2 Deadlock n Permanent blocking of a set of processes n Normally due to the fact that they u wait for limited system resources for which they compete or u wait for messages u since messages can be seen as resources, in general it can be said that it is due to contention on resources. n There is no satisfactory solution in the general case u to determine whether a program contains a potential deadlock is a computationally unsolvable problem

3 3 Leading example for this chapter: n Consider a system that has a printer and a disk n Suppose two processes P1 and P2, which behave in the same way: u Pi starts by asking for either printer or disk, but will need to use both printer and disk later to finish n Consider the following sequence of events: u P1 asks for printer, gets it u P2 asks for disk, gets it u Now deadlock will occur when P1 and P2 claim the second resource they need to finish n By this example, it should be clear that there can be ways to avoid a deadlock and that if a deadlock occurs it is possible to recuperate from it

4 4 Details n Scenario not leading to deadlock: u P1 starts, takes the printer u P1 takes the disk, can complete u P2 now starts n We can select a good scenario if we can find out in advance: force a certain order of execution n But how to find out? Difficult: we must distinguish among u waiting for a resource that will arrive and u waiting for a resource that will never arrive n How to recover after detection? Possibility: suspend a process and take resources away from it

5 5 The Conditions for Deadlock n These 3 conditions of policy must be present for a deadlock to be possible (necessary conditions): u 1: Mutual exclusion F only one process may use a given resource at a time u 2: Hold-and-wait F a process may hold allocated resources while awaiting assignment of others u 3: No preemption F no resource can be forcibly removed from a process holding it

6 6 The Conditions for Deadlock n We also need the occurrence of a particular sequence of events that result in : u 4: Circular wait F a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain, such that no process can complete without the resource held by the next

7 7 Relation between the 4 conditions deadlock mut. exclusion implies equivalent circular wait no preemption hold and wait

8 8 Aspects of handling deadlocks n Deadlock prevention u disallow 1 of the 3 necessary conditions of deadlock occurrence, or the equivalent condition n Deadlock avoidance u do not grant a resource request if this allocation might lead to deadlock n Deadlock detection and recovery u always grant resource requests when possible. But periodically check for the presence of deadlock and then recover from it

9 9 Deadlock Prevention n The OS is designed in such a way as to exclude a priori the possibility of deadlock n Indirect methods of deadlock prevention: u to disallow one of the 3 policy conditions n Direct methods of deadlock prevention: u to prevent the occurrence of circular wait

10 10 Indirect methods of deadlock prevention n Mutual Exclusion u cannot be disallowed u ex: only 1 process at a time can write to a file or hold a block of memory. n Hold-and-Wait u can be disallowed by requiring that a process request all its resources at once u block the process until all requests can be granted simultaneously u but process may be held up for a long time waiting for all its requests u so resources allocated to a process may remain unused for a long time. These resources could be used by other processes u an application would need to be aware of all the resources that will be needed

11 11 Indirect methods of deadlock prevention n No preemption u Can be prevented in several ways. But whenever a process must release a resource whose usage is in progress, the state of this resource must be saved for later resumption. u Hence: practical only when the state of a resource can be easily saved and restored later, such as the processor.

12 12 Direct methods of deadlock prevention n A protocol to prevent circular wait: u define a strictly increasing linear ordering O() for resource types. Ex: F R1: tape drives: O(R1) = 2 F R2: disk drives: O(R2) = 4 F R3: printers: O(R3) = 7 u A process initially requests a number of instances of a resource type, say Ri. A single request must be issued to obtain several instances. u After that, the process can request instances for resource type Rj if and only if O(Rj) > O(Rn), where Rn is a resource type already granted

13 13 Applied to our leading example... n Deadlock cannot occur because we have decided that disk < printer, n so a process cannot ask for disk after having asked for printer

14 14 Prevention of circular wait n Circular wait cannot hold under this protocol. In the example below, either RA<RB, or RB<RA. Suppose RA<RB. The situation below can occur because P1 has obtained RB and then requested RA, while P2 has obtained RA and then requested RB. But P1 cannot request RA after RB. There are other cases, but they are symmetrical and are excluded by the same reasoning.

15 15 Prevention of circular wait n This protocol prevents deadlock but will often deny resources unnecessarily (inefficient) because of the ordering imposed on the requests

16 16 Deadlock Prevention: Summary n We disallow one of the 3 policy conditions or use a protocol that prevents circular wait n This leads to inefficient use of resources and inefficient execution of processes

17 17 Deadlock Avoidance n We allow the 3 policy conditions but make judicious choices to assure that the deadlock point is never reached n Allows more concurrency than prevention n Two approaches: u do not start a process if its demand might lead to deadlock u do not grant an incremental resource request if this allocation might lead to deadlock n In both cases: maximum requirements of each resource must be stated in advance

18 18 Resource allocation graphs (Silberschatz) n Process n Resource having 4 copies (instances) n Pi asks for one instance of Ri, of which we have 4 n Pj has been assigned one copy of Rj PiPi PiPi RjRj

19 19 Example of resource allocation graphs: represents a state of the system P1 waits P2 waits P3 does not wait Possible deadlock?

20 20 Resource allocation graph with deadlock Cycles: P1  R1  P2  R3  P3  R2  P1 P2  R3  P3  R2  P2 cannot get out

21 21 Allocation graph with cycle but without deadlock (why?) Circular wait, but resources will become available

22 22 Observations n Cycles in a resource allocation graph do not necessarily imply a circular wait n If there is no cycle in the graph, no deadlock n If there are cycles: u If only one instance per resource type, deadlock u If several instances, possibility of deadlock F must ask question: is there a process that can terminate and if so, which other processes can terminate as a consequence?

23 23 Safe state n A state is safe if the system can get out of it without deadlock u In a safe state we can identify a sequence of process terminations, leading to a state where all processes terminate n Do not allocate a resource to a process if the resulting state is not safe

24 24 Deadlock detection n The system is allowed to enter a deadlock state n Deadlock is detected n There is recovery from deadlock n Detection of deadlock is not obvious: u if the OS sees some processes that wait, it cannot conclude that there is deadlock u there is deadlock only if there is a circular wait from which it is impossible to get out

25 25 Resource allocation graph and wait graph The second graph can be obtained from the first one Is there deadlock?

26 26 Detour... All the following discussion on banker algorithm etc. is only for those interested. Banker algorithm is fairly complicated and not very practical

27 27 Trivial example of avoidance in our leading example n Banker knows that P1 needs 1 now, will need 2 to finish. Knows the same about P2. n Gives 1 to P1, but not 1 to P2 because it sees that there is no way that the two processes can continue after this n Gives 1 more to P1 to allow it to finish. Then will give 1 to P2 to allow it to start, and so on.

28 28 Resource types n Resources in a system are partitioned in resources types n Each resource type in a system exists with a certain amount. Let R(i) be the total amount of resource type i present in the system. Ex: u R(main memory) = 128 MB u R(disk drives) = 8 u R(printers) = 5 n The partition is system specific (ex: printers may be further partitioned...)

29 29 Symbol summary for this section n R(i) total amount of resource i in system n V(i) total available amount of resource i n W(i) temporary vector: available vector n U(i) total unclaimed amount of resource i n C(k,i) total claim of res. i by process k n A(j,i) amount of res. i allocated to proc. j n N(j,i) amount of res. i needed by proc. j n Q(j,i) amt. of res. i currently req. by proc j

30 30 Allocation denial n Let C(k,i) be the amount of resource type i claimed by process k. n To be admitted in the system, process k must show C(k,i) for all resource types i n C(k,i) is the maximum value of resource type i permitted for process k. n Let U(i) be the total amount of resource type i unclaimed in the system:  U(i) = R(i) -  _ k C(k,i)

31 31 Process initiation denial n A new process n is admitted in the system only if C(n,i) <= U(i) for all resource type i n This policy ensures that deadlock is always avoided since a process is admitted only if all its requests can always be satisfied (no matter what will be their order) n A sub optimal strategy since it assumes the worst: that all processes will make their maximum claims together at the same time

32 32 Resource allocation denial: the banker’s algorithm n Processes are like customers wanting to borrow money (resources) to a bank... n A banker should not allocate cash when it cannot satisfy the needs of all its customers n At any time the state of the system is defined by the values of R(i), C(j,i) for all resource type i and process j and the values of other vectors and matrices.

33 33 The banker’s algorithm n We also need the amount allocated A(j,i) of resource type i to process j for all (j,i) The total amount available of resource i is given by: V(i) = R(i) -  _k A(k,i) n We also use the need N(j,i) of resource type i required by process j to complete its task: N(j,i) = C(j,i) - A(j,i) n To decide if a resource request made by a process should be granted, the banker’s algorithm test if granting the request will lead to a safe state: u If the resulting state is safe then grant request u Else do not grant the request

34 34 The banker’s algorithm n A state is safe iff there exist a sequence {P1..Pn} where each Pi is allocated all of its needed resources to be run to completion u ie: we can always run all the processes to completion from a safe state n The safety algorithm is the part that determines if a state is safe n Initialization: u all processes are said to be “unfinished” u set the work vector to the amount resources available: W(i) = V(i) for all i;

35 35 The banker’s algorithm n REPEAT: Find a unfinished process j such that N(j,i) <= W(i) for all i. u If no such j exists, goto EXIT u Else: “finish” this process and recover its resources: W(i) = W(i) + A(j,i) for all i. Then goto REPEAT n EXIT: If all processes have “finished” then this state is safe. Else it is unsafe.

36 36 The banker’s algorithm n Let Q(j,i) be the amount of resource type i requested by process j. n To determine if this request should be granted we use the banker’s algorithm: u If Q(j,i) <= N(j,i) for all i then continue. Else raise error condition (claim exceeded). u If Q(j,i) <= V(i) for all i then continue. Else wait (resource not yet available) u Pretend that the request is granted and determine the new resource-allocation state:

37 37 The banker’s algorithm F V(i) = V(i) - Q(j,i) for all i F A(j,i) = A(j,i) + Q(j,i) for all i F N(j,i) = N(j,i) - Q(j,i) for all i u If the resulting state is safe then allocate resource to process j. Else process j must wait for request Q(j,i) and restore old state.

38 38 Example of the banker’s algorithm n This state is safe with sequence {P2, P1, P3, P4}. After P2, we have W = (6,2,3) which enables the other processes to finish. Hence: request granted. Claimed Allocated Available 3 2 2 6 1 3 3 1 4 4 2 2 1 0 0 6 1 2 2 1 1 0 0 2 0 1 1P1 P2 P3 P4 The resulting state would be: R1 R2 R3

39 39 Example of the banker’s algorithm n However, if from the same initial state, P1 request Q = (1,0,1). The resulting state would be: Claimed Allocated Available 3 2 2 6 1 3 3 1 4 4 2 2 2 0 1 5 1 1 2 1 1 0 0 2 0 1 1P1 P2 P3 P4 Which is not a safe state since any process to finish would need an additional unit of R1. Request refused: P1 is blocked. R1 R2 R3

40 40 Banker’s algorithm: comments n A safe state cannot be deadlocked. But an unsafe state is not necessarily deadlocked. u Ex: P1 from the previous (unsafe) state could release temporarily a unit of R1 and R3 (returning to a safe state) u some process may need to wait unnecessarily u sub optimal use of resources n All deadlock avoidance algorithms assume that processes are independent: free from any synchronization constraint

41 41 Deadlock Detection n Resource access are granted to processes whenever possible. The OS needs: u an algorithm to check if deadlock is present u an algorithm to recover from deadlock n The deadlock check can be performed at every resource request n Such frequent checks consume CPU time

42 42 Trivial example of detection in our leading example n After each process has obtained 1M, the need of each process exceeds available memory (0M!). So no process can complete and there is a deadlock between P1 and P2.

43 43 A deadlock detection algorithm n Makes use of previous resource-allocation matrices and vectors n Marks each process not deadlocked. Initially all processes are unmarked. Then perform: u Mark each process j for which: A(j,i) = 0 for all resource type i. (since these are not deadlocked) u Initialize work vector: W(i) = V(i) for all i u REPEAT F Find an unmarked process j such that Q(j,i) <= W(i) for all i. Stop if such j does not exist. F If such j exists: mark process j and set W(i) = W(i) + A(j,i) for all i // Hoping that j will terminate u At the end: each unmarked process is deadlocked

44 44 Deadlock detection: comments n Process j is not deadlocked when Q(j,i) <= W(i) for all i. n Then we are optimistic and assume that process j will require no more resources to complete its task n It will thus soon return all of its allocated resources. Thus: W(i) = W(i) + A(j,i) for all i n If this assumption is incorrect, a deadlock may occur later n This deadlock will be detected the next time the deadlock detection algorithm is invoked

45 45 Deadlock detection: example n Mark P4 since it has no allocated resources n Set W = (0,0,0,0,1) n P3’s request <= W. So mark P3 and set W = W + (0,0,0,1,0) = (0,0,0,1,1) n Algorithm terminates. P1 and P2 are deadlocked R1 R2 R3 R4 R5 P1 P2 P3 P4 Request Allocated Available R1 R2 R3 R4 R5 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1

46 46 Deadlock Recovery: after deadlock has been detected n Needed when deadlock is detected. The following approaches are possible: u Abort all deadlocked processes (one of the most common solutions adopted in OS!!) u Rollback each deadlocked process to some previously defined checkpoint and restart them (original deadlock may reoccur) u Successively abort deadlock processes until deadlock no longer exists (each time we need to invoke the deadlock detection algorithm)

47 47 Deadlock Recovery (cont.) u Successively preempt some resources from processes and give them to other processes until deadlock no longer exists F a process that has a resource preempted must be rolled back prior to its acquisition n For the 2 last approaches: a victim process needs to be selected according to (for ex.): u least amount of CPU time consumed so far u least total resources allocated so far u least amount of “work” produced so far...

48 48 An integrated deadlock strategy n We can combine the previous approaches into the following way: u Group resources into a number of different classes and order them. Ex: F Swappable space (secondary memory) F Process resources (I/O devices, files...) F Main memory... u Use prevention of circular wait to prevent deadlock between resource classes u Use the most appropriate approach for each class for deadlocks within each class

49 49 Important concepts of Chapter 6 n What is deadlock, what are its causes n Circular wait n Necessary conditions n Deadlock prevention: methods n Deadlock avoidance: methods n Deadlock detection: methods n Resource allocation graphs n Wait graphs


Download ppt "1 Concurrency: Deadlock and Starvation Chapter 6."

Similar presentations


Ads by Google