Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Concurrency: Deadlock and Starvation I

Similar presentations


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

1 Chapter 6 Concurrency: Deadlock and Starvation I
Conditions for Deadlock Deadlock Prevention Deadlock Avoidance Banker’s Algorithm Deadlock Detection Strategies once Deadlock Detected Deadlock Detection Algorithm UNIX Concurrency Mechanisms

2 Deadlock Deadlock Deadlock can be defined as the permanent blocking of a set of processes that either compete for system resources or communicate with each other All deadlocks Involve conflicting needs for resources by two or more processes

3 Example of Deadlock (1) Process P Process Q ...... ...... Get A Get B
Get A Get B Get B Get A Release A Release B Release B Release A

4 Progress of processes-Example of Deadlock (2)
of Q 1 2 Release A P and Q want A A Required Release B Get A 3 deadlock inevitable P and Q want B B Required 5 Get B 4 6 Progress of P Get A Get B Release A Release B A Required B Required

5 Execution Paths -Example of Deadlock (3)
1. Q acquires B and then A, and then releases B and A. When P resumes execution, it will be able to acquire both resources. 2. Q acquires B and then A. P executes and blocks on a request for A. Q releases B and A. When P resumes execution, it will be able to acquire both resources. 5. P acquires A and then B. Q executes and blocks on a request for B. P releases A and B. When Q resumes execution, it will be able to acquire both resources. 6. P acquires A and then B, and then releases A and B. When Q resumes execution, it will be able to acquire both resources.

6 Deadlock Paths -Example of Deadlock (4)
3. Q acquires B and then P acquires A. Deadlock is inevitable, because as execution process Q will block on A and P will block on B. 4. P acquires A and then Q acquires B. Deadlock is inevitable, because as execution process Q will block on A and P will block on B.

7 Example of No Deadlock (1)
Process P Process Q Get A Get B Release A Get A Get B Release B Release B Release A

8 Example of No Deadlock (2)
Progress of Q 1 2 3 Release A 4 P and Q want A A Required Release B Get A P and Q want B B Required 5 Get B 6 Progress of P Get A Release A Get B Release B A Required B Required

9 Question 1 - Exercise/Home Work (1)
For Figure of No Deadlock, provide a narrative description of each of the six depicted paths.

10 Reusable Resources A reusable resource is one that can be safely 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 Examples of reusable resources: processor time, I/O channels, main and secondary memory, files, databases, and semaphores Deadlock occurs if each process holds one resource and requests the other

11 Example of Deadlock 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 P1 P2 . . . . . . Request 80K bytes; Request 70K bytes; . . . . . . Request 60K bytes; Request 80K bytes;

12 Consumable Resources A consumable resource is one that can be created (produced) and destroyed (consumed) by a process Examples of consumable resources: interrupts, signals, messages, and information in I/O buffers Deadlock may occur if a Receive message is blocking May take a rare combination of events to cause deadlock

13 Example of Deadlock Deadlock occurs if receive is blocking
P1 is blocked for waiting message from P2 P2 is blocked for waiting message from P1 P1 P2 . . . . . . Receive(P2); Receive(P1); . . . . . . Send(P2); Send(P1);

14 Conditions for Deadlock
Three conditions of policy must be present for a deadlock to be possible: Mutual exclusion only one process may use a resource at a time Hold-and-wait a process may hold allocated resources while awaiting assignment of others No preemption no resource can be forcibly removed from a process holding it

15 Conditions for Deadlock
Circular wait a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

16 Circular Wait Resource A Requests Held by Process P1 Process P2

17 Deadlock Prevention (1)
Mutual Exclusion cannot be disallowed Hold-and-Wait The hold-and-wait condition can be prevented by: require that a process request all its required resources at one time block the process until all requests can be granted simultaneously process may be held up for a long time waiting for all its requests resources allocated to a process may remain unused for a long time. These resources could be used by other processes

18 Deadlock Prevention (2)
No preemption This condition can be prevented in several ways: if a process is denied a further request, the process must release the original resources if a process cannot obtain a resource, the process may have to release its resources. Must have capability to restore to current state. This approach is practical only when the state can be easily saved and restored later, such as the processor.

19 Deadlock Prevention (3)
Circular wait The circular-wait condition can be prevented by : define a linear ordering for resources once a resource R is obtained, then it may subsequently request only those resources of types following R in the ordering.

20 Deadlock Avoidance (1) 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

21 Deadlock Avoidance (2) 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

22 Banker’s Algorithm - Deadlock Avoidance (1)
Total amount of each resource in the system Resource = ( R1, R2, , Rm ) Total amount of each resource not allocated to a process Available = ( V1, V2, , Vm )

23 Banker’s Algorithm - Deadlock Avoidance (2)
Requirement of each process for each resource C11 C C1m Claim = C21 C C2m Cki: process k Cn1 Cn Cnm for resource j Current allocation A11 A A1m Allocation = A21 A A2m Aki: process k An1 An Anm for resource j

24 Relationships - Banker’s Algorithm (3)
Ri = Vi + S Aki, for all i: All resources are k= either available or allocated. Cki <= Ri, for all k, i: No process can claim more than the total amount of resources in the system. Aki <= Cki, for all k, i: No process is allocated more resources of any type than the process originally claimed to need.

25 Deadlock Avoidance Policy - Banker’s Algorithm (4)
Ri >= C(n+1)i + Sum Cki, for all i k=1 A new process Pn+1 is only started if the maximum claim of all current processes plus those of the new process can be met.

26 Safe State -Banker’s Algorithm (5)
The state of the system is simply the current allocation of resources to processes. Thus the state consists of the two vectors, Resource and Available, and two matrices, Claim and Allocation. Safe State A safe state is a state in which there is at least one sequence in which all of the processes can be run to completion that does not result in a deadlock.

27 Determination of a Safe State-Banker’s Algorithm (6)
R1 R2 R R1 R2 R R1 R2 R3 P P P P Resource Vector P P P P R1 R2 R3 Claim Matrix Allocation Matrix Available Vector (a) Initial State - A Safe State?

28 Determination of a Safe State-Banker’s Algorithm (7)
R1 R2 R R1 R2 R R1 R2 R3 P P P P Resource Vector P P P P R1 R2 R3 Claim Matrix Allocation Matrix Available Vector (b) P2 Runs to Completion

29 Determination of a Safe State-Banker’s Algorithm (8)
R1 R2 R R1 R2 R3 R1 R2 R3 P P P P Resource Vector P P P P R1 R2 R3 Claim Matrix Allocation Matrix Available Vector (c) P1 Runs to Completion

30 Determination of a Safe State-Banker’s Algorithm (9)
R1 R2 R R1 R2 R3 R1 R2 R3 P P P P Resource Vector P P P P R1 R2 R3 Claim Matrix Allocation Matrix Available Vector (d) P3 Runs to Completion

31 Banker’s Algorithm - Deadlock Avoidance (10)
..... while ((possible) and (rest != null)) do find a Pk in rest such that claim[k, *] - allocation[K, *] <= currentavail; if found then currentavail:=currentavail + allocation[K, *]; rest:= rest - {Pk} else possible:=false end end; ......

32 Determination of an Unsafe State-Banker’s Algorithm (11)
R1 R2 R R1 R2 R3 R1 R2 R3 P P P P Resource Vector P P P P R1 R2 R3 Claim Matrix Allocation Matrix Available Vector (a) Initial State - A Safe State?

33 Determination of an Unsafe State-Banker’s Algorithm (12)
R1 R2 R R1 R2 R3 R1 R2 R3 P P P P Resource Vector P P P P R1 R2 R3 Claim Matrix Allocation Matrix Available Vector (b) P1 Requests One unit each of R1 and R3 - An Unsafe State

34 Question 2 - Exercise/Home Work (1)
R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4 P P P P P Claim Matrix Allocation Matrix Still Needs Resource Vector Available Vector

35 Question 2- Exercise/Home Work (2)
Consider the system given: a. Compute what each process still might request and display in the columns labeled “still needs.” b. Is this system currently in a safe or unsafe state? Why? c. Is this system currently deadlocked? Why or Why not? d. Which processes, if any, are or may become deadlocked? e. If a request from P3 arrives for (0, 1, 3, 4), can that request to be safely granted immediately? In what state (deadlocked, safe, unsafe) would immediately granting that whole request leave the system? Which processes, if any, are or may become deadlocked if this whole request is granted immediately?


Download ppt "Chapter 6 Concurrency: Deadlock and Starvation I"

Similar presentations


Ads by Google