Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency: Deadlock and Starvation

Similar presentations


Presentation on theme: "Concurrency: Deadlock and Starvation"— 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

4

5

6 Resource Category There are two general categories of resources:
Reusable Resources Consumable Resources

7 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

8 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.

9 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

10 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. P1 . . . Request 80K bytes; Request 60K bytes; P2 Request 70K bytes;

11 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

12 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 P1 . . . Receive(P2); Send(P2, M1); P2 Receive(P1); Send(P1, M2);

13 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

14 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

15 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.

16 Conditions for Deadlock

17 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.

18 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.

19

20 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.

21 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.

22 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.

23 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.

24 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.

25 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 Ra precedes Rc in the ordering if a< c (using alphabetical order).

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

27 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

28 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

29 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.

30 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 . . . .

31 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 ?

32 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?

33 Determination of a Safe State Initial State

34 Determination of a Safe State P2 Runs to Completion

35 Determination of a Safe State P1 Runs to Completion

36 Determination of a Safe State P3 Runs to Completion

37 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?

38 Determination of an Unsafe State

39 Deadlock Avoidance… Advantage: But has a number of restrictions:
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

40 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

41 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.

42 Deadlock Detection Algorithm
The Allocation matrix and Available vector will be used. Request matrix Q is define such that Qij represent the 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)

43 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.

44 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.

45 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.

46 Example

47 Example The algorithm proceeds as follows:
Mark P4, because P4 has no allocated resources. Set W = ( ) The request of P3 is less than equal to W, so mark P3 and set W = W + ( ) = ( ) 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.

48 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.

49 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.

50 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

51 Integrated Deadlock Strategy
Combine the previous approaches into the following way: Group resources into a number of different resource classes Use the linear ordering strategy (prevention of circular wait) to prevent deadlock between resource classes Within a resource class, use the algorithm that is most appropriate for that class.

52 Integrated Deadlock Strategy
Example of resource classes: Swappable space Blocks of memory on secondary storage for use in swapping processes. Process resources Assignable devices, such as tape drives and files Main memory Assignable to processes in pages and segments Internal resources Such as I/O channels


Download ppt "Concurrency: Deadlock and Starvation"

Similar presentations


Ads by Google