Concurrency: Deadlock and Starvation
description
Transcript of Concurrency: Deadlock and Starvation
![Page 1: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/1.jpg)
1
Concurrency: Deadlock and Starvation
Chapter 6
![Page 2: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/2.jpg)
2
DeadlockPermanent blocking of a set of processes that either compete for system resources or communicate with each otherNo efficient solution in general caseInvolve conflicting needs for resources by two or more processes
![Page 3: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/3.jpg)
3
![Page 4: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/4.jpg)
4
![Page 5: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/5.jpg)
5
![Page 6: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/6.jpg)
6
Resource CategoryThere are two general categories of resources:
1. Reusable Resources2. Consumable Resources
![Page 7: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/7.jpg)
7
Reusable ResourcesUsed by one process at a time and not depleted by that useProcesses obtain resources that they later release for reuse by other processesE.g.- Processors, I/O channels, main and secondary memory, files, databases, and semaphoresDeadlock occurs if each process holds one resource and requests the other
![Page 8: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/8.jpg)
8
Example 1Consider two processes that compete for exclusive access to disk file D and tape drive T.The program operations shown in figure 6.4 below.
![Page 9: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/9.jpg)
9
Example 1Deadlock 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
![Page 10: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/10.jpg)
10
Example 2Space is available for allocation of 200K bytes, and the following sequence of events occur
Deadlock occurs if both processes progress to their second requestDeal with this problem is by using virtual memory.
P1
. . .
. . .Request 80K bytes;
Request 60K bytes;
P2
. . .
. . .Request 70K bytes;
Request 80K bytes;
![Page 11: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/11.jpg)
11
Consumable ResourcesCreated (produced) and destroyed (consumed) by a processMay take a rare combination of events to cause deadlockE.g. - Interrupts, signals, messages, and information in I/O buffers
![Page 12: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/12.jpg)
12
Example 1Consider 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);
![Page 13: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/13.jpg)
13
Conditions for DeadlockFour conditions of policy must be present for a deadlock to be possible:
1. Mutual exclusion2. Hold and wait3. No preemption4. Circular wait
![Page 14: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/14.jpg)
14
Conditions for Deadlock1. Mutual exclusion
Only one process may use a resource at a time
2. Hold-and-waitA process request all of its required resources at one time and will hold allocated resources while awaiting for other resources
![Page 15: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/15.jpg)
15
Conditions for Deadlock3. No preemption
No resource can be forcibly removed from a process holding it.
4. Circular WaitA closed chain of processes exists, such that each process holds at least once resources needed by the next process in the chain.
![Page 16: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/16.jpg)
16
Conditions for Deadlock
![Page 17: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/17.jpg)
17
Conditions for DeadlockDeadlock 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.
![Page 18: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/18.jpg)
18
Approaches for DeadlockThe most important approaches that have been developed:
1. Deadlock Prevention Prevent deadlock by adopting a policy that eliminates one of the conditions.
2. Deadlock AvoidanceAvoid deadlock by making the appropriate dynamic choices based on the current state of resource allocation.
3. Deadlock DetectionAttempt to detect the presence of deadlock and take action to recover.
![Page 19: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/19.jpg)
![Page 20: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/20.jpg)
20
Deadlock PreventionTo design a system in such a way that the possibility of deadlock is excluded.Method falling into two classes:
1. Indirect method – to prevent the occurrence of one of the three
necessary condition listed previously.2. Direct method
– to prevent the occurrence of circular wait.
![Page 21: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/21.jpg)
21
Deadlock Prevention…Techniques related to each four:
1. 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.
![Page 22: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/22.jpg)
22
Deadlock Prevention…2. 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:i. 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.
ii. Resource allocated to a process may remain unused for a considerable period. During which time they are denied to other processes.
![Page 23: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/23.jpg)
23
Deadlock Prevention…iii. 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.
![Page 24: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/24.jpg)
24
Deadlock Prevention…3. No Preemption
– Can be prevented in several ways:i. 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.
ii. 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.
![Page 25: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/25.jpg)
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).
![Page 26: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/26.jpg)
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.
![Page 27: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/27.jpg)
27
Deadlock AvoidanceAllow the 3 policy conditions but make judicious choices to assure that the deadlock point is never reachedAllows more concurrency than preventionA decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlockRequires knowledge of future process request
![Page 28: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/28.jpg)
28
Deadlock Avoidance…Two approaches to deadlock avoidance:
1. Do not start a process if its demands might lead to deadlock
2. 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
![Page 29: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/29.jpg)
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.
![Page 30: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/30.jpg)
30
Deadlock Avoidance…Claim =C = C11, C12,…, C1m
C21, C22,…, C2m
Cn1, Cn2,…, Cnm
Cij = requirement of process i for resource jAllocation = 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
![Page 31: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/31.jpg)
31
Resource Allocation DenialReferred to as the banker’s algorithmState 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 deadlockUnsafe state ?
![Page 32: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/32.jpg)
32
Example 1Figure 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?
![Page 33: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/33.jpg)
33
Determination of a Safe StateInitial State
![Page 34: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/34.jpg)
34
Determination of a Safe StateP2 Runs to Completion
![Page 35: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/35.jpg)
35
Determination of a Safe StateP1 Runs to Completion
![Page 36: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/36.jpg)
36
Determination of a Safe StateP3 Runs to Completion
![Page 37: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/37.jpg)
37
Example 2Figure 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?
![Page 38: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/38.jpg)
38
Determination of an Unsafe State
![Page 39: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/39.jpg)
39
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 advanceProcesses under consideration must be independent; no synchronization requirementsThere must be a fixed number of resources to allocateNo process may exit while holding resources
![Page 40: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/40.jpg)
40
Deadlock DetectionDo 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 presentan algorithm to recover from deadlock
![Page 41: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/41.jpg)
41
Deadlock Detection…The deadlock check can be performed at every resource request.Checking at each resource request has two advantages:
i. Leads to early detectionii. Algorithm is simple
– because it is based on incremental changes to the state of the system.
But such frequent checks will consume CPU time.
![Page 42: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/42.jpg)
42
Deadlock Detection AlgorithmThe 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)
![Page 43: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/43.jpg)
43
Deadlock DetectionThen the following steps are performed:
1. Mark each process that has a row in the Allocation matrix of all zeros.
2. Initialize a temporary vector W to equal to Available vector.
3. 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.
4. If exist, mark process i and add the corresponding row of the Allocation matrix to W. Then return to step 3.
![Page 44: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/44.jpg)
44
Deadlock DetectionDeadlock exist if and only if there are unmarked processes at the end of algorithmStrategy 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.
![Page 45: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/45.jpg)
45
Deadlock DetectionThis 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.
![Page 46: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/46.jpg)
46
Example
![Page 47: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/47.jpg)
47
ExampleThe algorithm proceeds as follows:
1. Mark P4, because P4 has no allocated resources.
2. Set W = ( 0 0 0 0 1 )3. 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 )
4. 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.
![Page 48: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/48.jpg)
48
Deadlock Detection RecoveryNeeded when deadlock is detected.The following approaches are possible:
1. Abort all deadlocked processes (one of the most common solution adopted in OS)
2. Back up each deadlocked process to some previously defined checkpoint, and restart all process.
![Page 49: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/49.jpg)
49
Deadlock Detection Recovery3. Successively abort deadlock processes until
deadlock no longer existsAfter each abortion, need to reinvoke the deadlock detection algorithm to see either deadlock still exists or not.
4. 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.
![Page 50: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/50.jpg)
50
Deadlock Detection RecoveryFor approaches 3 and 4: a victim process needs to be selected according to:
Least amount of processor time consumed so farLeast number of lines of output produced so farMost estimated time remainingLeast total resources allocated so farLowest priority
![Page 51: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/51.jpg)
51
Integrated Deadlock StrategyCombine the previous approaches into the following way:
Group resources into a number of different resource classesUse the linear ordering strategy (prevention of circular wait) to prevent deadlock between resource classesWithin a resource class, use the algorithm that is most appropriate for that class.
![Page 52: Concurrency: Deadlock and Starvation](https://reader036.fdocuments.us/reader036/viewer/2022062316/56816743550346895ddbf8b7/html5/thumbnails/52.jpg)
52
Integrated Deadlock StrategyExample of resource classes:
Swappable spaceBlocks of memory on secondary storage for use in swapping processes.
Process resourcesAssignable devices, such as tape drives and files
Main memoryAssignable to processes in pages and segments
Internal resourcesSuch as I/O channels