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 Permanent blocking of a set of processes that either compete for system resources or communicate with each other No efficient solution Involve conflicting needs for resources by two or more processes

3 3

4 4

5 5 Reusable Resources Used by only one process at a time and not depleted by that use Processes obtain resources that they later release for reuse by other processes Processors, I/O channels, main and secondary memory, devices, and data structures such as files, databases, and semaphores Deadlock occurs if each process holds one resource and requests the other

6 6 Example of Deadlock

7 7 Another Example of Deadlock Space is available for allocation of 200Kbytes, and the following sequence of events occur Deadlock occurs if both processes progress to their second request P1... Request 80 Kbytes; Request 60 Kbytes; P2... Request 70 Kbytes; Request 80 Kbytes;

8 8 Example of Deadlock Deadlock occurs if receive is blocking P1... Receive(P2); Send(P2, M1); P2... Receive(P1); Send(P1, M2);

9 9 Resource Allocation Graphs Directed graph that depicts a state of the system of resources and processes

10 10 Resource Allocation Graphs

11 11 Conditions for Deadlock 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 form a process holding it

12 12 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

13 13

14 14 Resource Allocation Graph No cycles No deadlocks P1P2P3...…. R1 R2 R4 R3 P1P2P3.... R1 R2 R3 Here are two cycles: P1->R1->P2->R3->P3->R2->P1 P2->R3->P3->R2->P2 So P1,P2,P3 are deadlocked

15 15 Resource Allocation Graph P1 : R1 : P2 P3 P4 R2 Here is acycle : P1->R1->P2->R2->P1 But No Deadlock (P4 can finish and break the cycle)

16 16 Possibility of Deadlock Mutual Exclusion No preemption Hold and wait

17 17 Existence of Deadlock Mutual Exclusion No preemption Hold and wait Circular wait

18 18 Deadlock Prevention Mutual Exclusion –Must be supported by the operating system Hold and Wait –Require a process request all of its required resources at one time

19 19 Deadlock Prevention No Preemption –Process must release resource and request again –Operating system may preempt a process to require it releases its resources Circular Wait –Define a linear ordering of resource types

20 20 Deadlock Avoidance 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

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

22 22 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 is a state that is not safe

23 23 Determination of a Safe State Initial State

24 24 Determination of a Safe State P2 Runs to Completion

25 25 Determination of a Safe State P1 Runs to Completion

26 26 Determination of a Safe State P3 Runs to Completion

27 27 Determination of an Unsafe State

28 28 Determination of an Unsafe State

29 29 Example: Consider a system with 12 tape drives and 3 processes P 0,P 1,P 2. Assume that we know the maximum No. of tape drives each process may request as: P 0 requires 10 tap drives, P 1 requires 4 tap drives, P 2 requires as many as 9. Suppose at time t o 9 tape drivers are allocated as follow: P 0  5, P 1  2, P 2  2, So, we have: Safe/Unsafe States ProcessCurrent NeedMax. Need P0P0 510 P1P1 24 P2P2 29 Note: At the time 0, the total No of allocated tapes is 5+2+2=9 So the free tapes are 12-9=3 The sequence is a safe sequence.

30 30 Safe/Unsafe States Note: It is possible to go from a safe state to an un safe state. Consider the last example. Assume at time t 1, P 2 requests one more tape drive and gets it. The system is in an unsafe state, because only P 1 can be allocated all its tape dives, finishes and return all 4 tape drives, but P 0 is allocated 5, may request 5 more  has to wait. but P 1 is allocated 3, may request 6 more  has to wait. Then P 0,P 2 are deadlocked!

31 31 Banker’s Algorithm [A Deadlock Avoidance Algorithm] Let m be the No. of resource types, and n be the No. of Processes. The following data structures are used in the algorithm: Available[m]: A vector of length m indicates the number of available resources if each type. If Available[i]=k, then there are k instances of resource type Ri available. Max[n,m]: Tow dimensional array of size nxm. It defines the maximum demand of each process from each resource type. For example, if Max[i,j]=k, then Pi may request at most k instances of resource type Rj.

32 32 Banker’s Algorithm [A Deadlock Avoidance Algorithm] Allocation[n,m]: Two dimensional array of size nxm. It defines the number of resources of each type currently located to each process. If Allocation[i,j]=k, then Pi is currently allocated k instances of resource type Rj. Need[n,m]: Two dimensional array of size nxm. It indicates the remaining resource needed for each process. If Need[i,j]=k then Pi may need k more instances of Rj to complete. Note that Need[i,j]=Max[i,j]-Allocation[i,j].

33 33 Banker’s Algorithm [A Deadlock Avoidance Algorithm] The Algorithm: 1.Process i makes requests for resources. Let Request(i) be the corresponding request vector. 2.If Request(i)>Need(i), then there is an error. 3.Otherwise, if Request(i)>Available, then Pi must wait. 4.Other wise, modify the data structure as follows: Available=Available-Request(i). Allocation (i)=Allocation(i)+Request(i). Need(i)=Need(i)-Request(i). 5.Check whether the resulting state is safe (Use Safety Algorithm) 6.If the state is safe, do the allocation. Otherwise Pi must wait for Request(i).

34 34 Safety Algorithm Let Work and Finish be vectors of length m,n respectively. A1: Initialize Work =Available, Finish[i]=False for all i. A2: Find an I such that a) Finish[i]=False b) Need(i)<=Work If no such I found then Go to step A4. A3: If an I is found, then for that i do: work=work+Allocation(i) Finish[i]=True Go to step A2 A4: If finish[i] =True For all I, then the system is in safe state

35 35 Example Suppose a system with three resource types is supporting two processes. The state of the system is as follows: Available=[1 4 1] MaxAllocationRequestNeed (Max-Alloction) R1 R2 R3 P11 3 10 0 01 2 01 3 1 P21 4 10 0 00 2 11 4 1 Suppose Reuest(1) is to be processed: Available=[1 4 1]-[1 2 0]=[0 2 1] Allocation(1)=[1 2 0], Need(1)=[0 1 1]

36 36 Example (Cont.) Now Apply the safety algorithm Work=[0 2 1], Finish= i=1: Need(1)=[0 1 1]<=Work?? Yes Work=Work+Allocation(1)=[1 4 1] Finish[1]=True i=2: Need(2)=[1 4 1]<=Work?? Yes Work=Work+Allocation(2)=[1 4 1] Finish[2]=True Hence Finish=True for all I then the system is in safe state, an d we can do the allocation False

37 37 Deadlock Avoidance 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

38 38 Deadlock Detection

39 39 Data structure used: Available[m]: As in banker’s algorithm Allocation[n,m]: As in Banker’s algorithm Request[n,m]: Two dimensional array of size nxm which indicates the current requests of each process. –If Request[i,j]= k, then Pi requests k instances of resource type Rj. Work and Finish are vectors of length m and n, as the safety algorithm Deadlock Detection Algorithm (Shoshani & Coffman)

40 40 A1: Initialize Work=Available For i=1 to n do If Allocation(i)=0 then Finish[i]=True else Finish[i]=False A2: Find an I such that a) Finish[i]=False b) Request(i)<=Work If no such I can be found then go to A4 A3: For that I found in A2 do Work=Work+Allocation(i) Finish[i]=True Goto A2 A4: If Finish[i]≠True for all i, then the system is in a deadlock state and for those i’s foe which Finish[i]=False, Pi’s are deadlocked. The Algorithm

41 41 Example Suppose that at time To we have the following resource allocation state: The sequence will not lead the system into a deadlock state. Allocation R1 R2 R3 Request R1 R2 R3 Available R1 R2 R3 P1 0 1 0 0 0 0 P2 2 0 02 0 2 P3 3 0 30 0 0 P4 2 1 11 0 0 P5 0 0 2

42 42 A1: Work=[0 0 0], Finish[i]=False for i=1,2,3,4,5 A2: P1: Finish[1]=False, Request[1]<=Work( i.e.[000]<=[000]) A3: Work=[0 0 0]+[0 1 0]=[0 1 0] Finish[1]=True (1: P1) A2: P3: Finish[3]=False, Request[3]<=Work (i.e. [000]<=[010]) A3: Work=[0 1 0]+[3 0 3]=[3 1 3] Finish[3]=True (2:P3) A2: P4: Finish[4]=False, Request[4]<=Work (i.e. [100]<=[313]) A3: Work =[3 1 3]+[2 1 1]=[5 2 4] Finish[4]=True (3:P4) A2: P2: Finish[2]=False, Request[2]<=Work (i.e. [202]<=[524]) A3: Work [5 2 4]+[2 0 0]=[7 2 4] Finish[2]=True (4:P2) Example: Cont.

43 43 A2: P5: Finish[5]=False, Request[5]<=Work (i.e. [002]<=[726]) A3: Work [7 2 4]+[0 0 2]=[7 2 6] Finish[5]=True (5:P5) A4: Finish[i]=True for all I So No deadlock at this moment Example: Cont.

44 44 Strategies once Deadlock Detected Abort all deadlocked processes Back up each deadlocked process to some previously defined checkpoint, and restart all process –Original deadlock may occur Successively abort deadlocked processes until deadlock no longer exists Successively preempt resources until deadlock no longer exists

45 45 Selection Criteria Deadlocked Processes 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


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

Similar presentations


Ads by Google