Similar presentations

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

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

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;

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

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

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

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

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

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