1 / 52

Concurrency: Deadlock and Starvation

Concurrency: Deadlock and Starvation. Chapter 6. Deadlock. Permanent blocking of a set of processes that either compete for system resources or communicate with each other No efficient solution in general case Involve conflicting needs for resources by two or more processes.

guri
Download Presentation

Concurrency: Deadlock and Starvation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Concurrency: Deadlock and Starvation Chapter 6

  2. Deadlock • Permanent blocking of a set of processes that either compete for system resources or communicate with each other • No efficient solution in general case • Involve conflicting needs for resources by two or more processes

  3. Resource Category • There are two general categories of resources: • Reusable Resources • Consumable Resources

  4. Reusable Resources • Used by one process at a time and not depleted by that use • Processes obtain resources that they later release for reuse by other processes • E.g.- Processors, I/O channels, main and secondary memory, files, databases, and semaphores • Deadlock occurs if each process holds one resource and requests the other

  5. Example 1 • Consider two processes that compete for exclusive access to disk file D and tape drive T. • The program operations shown in figure 6.4 below.

  6. Example 1 • Deadlock occurs when the multiprogramming system interleaves the execution of two processes as follows: • Deal with impose system design constraints concerning with the order which resources are requested. p0p1q0q1p2q2

  7. P1 P2 . . . . . . Request 80K bytes; Request 70K bytes; . . . . . . Request 60K bytes; Request 80K bytes; Example 2 • Space is available for allocation of 200K bytes, and the following sequence of events occur • Deadlock occurs if both processes progress to their second request • Deal with this problem is by using virtual memory.

  8. Consumable Resources • Created (produced) and destroyed (consumed) by a process • May take a rare combination of events to cause deadlock • E.g. - Interrupts, signals, messages, and information in I/O buffers

  9. P1 P2 . . . . . . Receive(P2); Receive(P1); . . . . . . Send(P2, M1); Send(P1, M2); Example 1 • Consider the following pair of processes, in which each process attempts to receive a message from the other process and then send a message to other process: • Deadlock occurs if Receive() is blocking

  10. Conditions for Deadlock • Four conditions of policy must be present for a deadlock to be possible: • Mutual exclusion • Hold and wait • No preemption • Circular wait

  11. Conditions for Deadlock • Mutual exclusion • Only one process may use a resource at a time • Hold-and-wait • A process request all of its required resources at one time and will hold allocated resources while awaiting for other resources

  12. Conditions for Deadlock 3. No preemption • No resource can be forcibly removed from a process holding it. • Circular Wait • A closed chain of processes exists, such that each process holds at least once resources needed by the next process in the chain.

  13. Conditions for Deadlock

  14. Conditions for Deadlock • Deadlock can and might not exist with the first three conditions • The fourth condition is required. • The fourth condition is actually a potential consequence of the first three.

  15. Approaches for Deadlock • The most important approaches that have been developed: • Deadlock Prevention • Prevent deadlock by adopting a policy that eliminates one of the conditions. • Deadlock Avoidance • Avoid deadlock by making the appropriate dynamic choices based on the current state of resource allocation. • Deadlock Detection • Attempt to detect the presence of deadlock and take action to recover.

  16. Deadlock Prevention • To design a system in such a way that the possibility of deadlock is excluded. • Method falling into two classes: • Indirect method • to prevent the occurrence of one of the three necessary condition listed previously. • Direct method • to prevent the occurrence of circular wait.

  17. Deadlock Prevention… • Techniques related to each four: • Mutual Exclusion (ME) • Cannot be disallowed = MUST BE ALLOWED!! • If access to resources require ME, then ME must be supported by the OS. • But deadlock still can occur (in some resources): E.g.. Files may allow multiple accesses for reads but only exclusive for writes. Deadlock can occur if more than one process requires write permission.

  18. Deadlock Prevention… • Hold and Wait • Prevented by requiring that a process request all of its required resources at one time and blocked the process until all request can be granted simultaneously. • This approach inefficient because: • Process may be held up for along time waiting for all of its resources to be filled, when in fact it could have been proceeded with only some of the resources. • Resource allocated to a process may remain unused for a considerable period. During which time they are denied to other processes.

  19. Deadlock Prevention… • Process may not know in advance all of the resources that it will require. • Also a practical problem created by the use of modular programming or multithreaded structure for an application • need to be aware of all resources that will requested at all level or in all modules in order to make the simultaneous request.

  20. Deadlock Prevention… 3. No Preemption • Can be prevented in several ways: • If a process holding certain resources is denied a further request, that process must release its original resources and if necessary request again with the additional resource. • If a process request a resource that is currently held by another process, the OS must preempt the second process and require it to release its resource. • Practical only when applied to resources whose state can be easily saved and restored later, e.g. a processor.

  21. Deadlock Prevention… 4. Circular Wait • Can be prevented by defining a linear ordering resource types. • If a process has been allocated resources of type R, then it may subsequently request only those resources of types following R in the ordering. • E.g. • Let us associate an index with a each resource type. Then resource Raprecedes Rcin the ordering if a< c (using alphabetical order).

  22. Deadlock Prevention... • Suppose that two processes, A and B, are deadlocked because A has acquired Raand requested Rc, and B acquired Rcand requested Ra. This condition is impossible because it implies a < cand c < a • circular wait prevention may be inefficient because it will slowing down process and denying resource access unnecessarily.

  23. Deadlock Avoidance • Allow the 3 policy conditions but make judicious choices to assure that the deadlock point is never reached • Allows more concurrency than prevention • A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock • Requires knowledge of future process request

  24. Deadlock Avoidance… • Two approaches to deadlock avoidance: • Do not start a process if its demands might lead to deadlock • Do not grant an incremental resource request to a process if this allocation might lead to deadlock • In both cases: maximum requirements of each resource must be stated in advance

  25. Deadlock Avoidance… • Consider a system of n processes and m different types of resources. • Definition of the vectors and matrices: • Resource = R = (R1, R2,…, Rm) • Total amount of each resource in the system • Available = V = (V1, V2,…, Vm) • Total amount of each resource not allocated to any process.

  26. Deadlock Avoidance… • Claim =C = C11, C12,…, C1m C21, C22,…, C2m Cn1, Cn2,…, Cnm • Cij = requirement of process i for resource j • Allocation = A = A11, A12,…, A1m A21, A22,…, A2m An1, An2,…, Anm • Aij = current allocation to process i of resource j  matrix Claim gives the maximum requirement of each process. . . . . . . . . . . . . matrix Allocation shows current allocation for each process . . . . . . . . . . . .

  27. Resource Allocation Denial • Referred to as the banker’s algorithm • State of the system is the current allocation of resources to process • Safe state is where there is at least one sequence that does not result in deadlock • Unsafe state ?

  28. Example 1 • Figure 6.7a shows the state of a system consisting of four processes and three resources. • Total amount of resources R1, R2 and R3 are 9, 3 and 6 units. (Resource Vector) • In the current state, allocations have been made to the four processes and leaving only 1 unit of R2 and 1 unit of R3 available (Available Vector). • Is this a safe state?

  29. Determination of a Safe StateInitial State

  30. Determination of a Safe StateP2 Runs to Completion

  31. Determination of a Safe StateP1 Runs to Completion

  32. Determination of a Safe StateP3 Runs to Completion

  33. Example 2 • Figure 6.8a shows the state of a system consisting of four processes and three resources. • Total amount of resources R1, R2 and R3 are 9, 3 and 6 units. (Resource Vector) • In the current state, allocations have been made to the four processes and leaving only 1 unit of R1, 1 unit of R2 and 2 unit of R3 available (Available Vector). • Suppose P1 make a request for 1 unit of R1 and 1 unit of R3. • Is this a safe state?

  34. Determination of an Unsafe State

  35. Deadlock Avoidance… • Advantage: • Does not necessarily preempt and rollback processes compared to deadlock detection. • But has a number of restrictions: • Maximum resource requirement must be stated in advance • Processes under consideration must be independent; no synchronization requirements • There must be a fixed number of resources to allocate • No process may exit while holding resources

  36. Deadlock Detection • Do not limit resource access or restrict process actions. • Resource access are granted to process whenever possible. • OS will periodically performs • an algorithm to check if deadlock present • an algorithm to recover from deadlock

  37. Deadlock Detection… • The deadlock check can be performed at every resource request. • Checking at each resource request has two advantages: • Leads to early detection • Algorithm is simple • because it is based on incremental changes to the state of the system. • But such frequent checks will consume CPU time.

  38. Deadlock Detection Algorithm • The Allocation matrix and Available vector will be used. • Request matrix Q is define such that Qijrepresentthe amount of resources of type j requested by process i. • The algorithm begins by marking processes that are not deadlock (Initially all processes are unmarked)

  39. Deadlock Detection • Then the following steps are performed: • Mark each process that has a row in the Allocation matrix of all zeros. • Initialize a temporary vector W to equal to Available vector. • Find an index i such that process i is currently unmarked and ith row of Q <= W. If no such row found terminate the algorithm. • If exist, mark process i and add the corresponding row of the Allocation matrix to W. Then return to step 3.

  40. Deadlock Detection • Deadlock exist if and only if there are unmarked processes at the end of algorithm • Strategy in this algorithm is to find a process whose resource requests can be satisfied with the available resources and the assume that those resources are granted and the process runs to completion and release its resources. • Then the algorithm will look for another process.

  41. Deadlock Detection • This algorithm will not guarantee to prevent deadlock, it will depend on the order in which requested are granted. • It is only to determine either deadlock is currently exist or not.

  42. Example

  43. Example • The algorithm proceeds as follows: • Mark P4, because P4 has no allocated resources. • Set W = ( 0 0 0 0 1 ) • The request of P3 is less than equal to W, so mark P3 and set W = W + ( 0 0 0 1 0) = ( 0 0 0 1 1 ) • No other unmarked process has a row in Q is less than or equal to W. Terminate the algorithm. • The algorithm concludes with P1 and P2 unmarked. This processes are deadlocked.

  44. Deadlock Detection Recovery • Needed when deadlock is detected. • The following approaches are possible: • Abort all deadlocked processes (one of the most common solution adopted in OS) • Back up each deadlocked process to some previously defined checkpoint, and restart all process.

  45. Deadlock Detection Recovery • Successively abort deadlock processes until deadlock no longer exists • After each abortion, need to reinvoke the deadlock detection algorithm to see either deadlock still exists or not. • Successively preempt some resources from processes and give them to other processes until deadlock no longer exists • a process that has a resource preempted must be rolled back prior to its acquisition of that resource.

  46. Deadlock Detection Recovery • For approaches 3 and 4: a victim process needs to be selected according to: • Least amount of processor time consumed so far • Least number of lines of output produced so far • Most estimated time remaining • Least total resources allocated so far • Lowest priority

More Related