Deadlock and Starvation n Deadlock two or more

  • Slides: 17
Download presentation
Deadlock and Starvation n Deadlock – two or more processes are waiting indefinitely for

Deadlock and Starvation n Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. n Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(S); wait(Q); wait(S); � � signal(S); signal(Q) signal(S); n Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. Operating System Concepts 8. 1 Silberschatz, Galvin and Gagne 2002

Deadlock Characterization n Deadlock can arise if four conditions hold simultaneously. F Mutual exclusion:

Deadlock Characterization n Deadlock can arise if four conditions hold simultaneously. F Mutual exclusion: only one process at a time can use a resource. F Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. F No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. F Circular wait: there exists a set {P 0, P 1, …, Pn} of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, Pn– 1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P 0. Operating System Concepts 8. 2 Silberschatz, Galvin and Gagne 2002

System Model for OS Solution n Resource types R 1, R 2, . .

System Model for OS Solution n Resource types R 1, R 2, . . . , Rm CPU cycles, memory space, I/O devices n Each resource type Ri has Wi instances. n Each process utilizes a resource as follows: F request F use F release Operating System Concepts 8. 3 Silberschatz, Galvin and Gagne 2002

Resource-Allocation Graph n A set of vertices V and a set of directed edges

Resource-Allocation Graph n A set of vertices V and a set of directed edges E n V is partitioned into two types: F P = {P 1, P 2, …, Pn}, the set consisting of all the processes in the system. F R = {R 1, R 2, …, Rm}, the set consisting of all resource types in the system. n Request edge – directed edge P 1 Rj n Assignment edge – directed edge Rj Pi n Resource-Allocation Graph F Process F Resource Type with 4 instances F Pi requests instance of Rj F Pi is holding an instance of Rj Pi Pi Rj Rj Operating System Concepts 8. 4 Silberschatz, Galvin and Gagne 2002

Basic Facts n If graph contains no cycles no deadlock. n If graph contains

Basic Facts n If graph contains no cycles no deadlock. n If graph contains a cycle F if only one instance per resource type, then deadlock. F if several instances per resource type, possibility of deadlock. Operating System Concepts 8. 5 Silberschatz, Galvin and Gagne 2002

RAG with No Cycles (no deadlock) Operating System Concepts 8. 6 Silberschatz, Galvin and

RAG with No Cycles (no deadlock) Operating System Concepts 8. 6 Silberschatz, Galvin and Gagne 2002

RAG with a Deadlock Operating System Concepts 8. 7 Silberschatz, Galvin and Gagne 2002

RAG with a Deadlock Operating System Concepts 8. 7 Silberschatz, Galvin and Gagne 2002

RAG with a Cycle but No Deadlock Operating System Concepts 8. 8 Silberschatz, Galvin

RAG with a Cycle but No Deadlock Operating System Concepts 8. 8 Silberschatz, Galvin and Gagne 2002

Methods for Handling Deadlocks n Deadlock prevention - Ensure that the system will never

Methods for Handling Deadlocks n Deadlock prevention - Ensure that the system will never enter a deadlock state by constraining possible requests n Deadlock avoidance - Stop the system from entering a deadlock state by examining current request and maximal requirement for process (declared in advance) n Deadlock detection and recovery - Allow the system to enter a deadlock state and then recover. n Deadlock acceptance - Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. Operating System Concepts 8. 9 Silberschatz, Galvin and Gagne 2002

Deadlock Prevention n Mutual Exclusion – not required for sharable resources; must hold for

Deadlock Prevention n Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. n Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. F Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. E. g. Network->Disk->Printer F Low resource utilization (early requests); starvation possible (always one resource unavailable). n No Preemption - if a process requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. F Preempted resources are added to the list of resources for which the process is waiting. F Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. n Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. F Least valuable claimed first Operating System Concepts 8. 10 Silberschatz, Galvin and Gagne 2002

Deadlock Avoidance n When a resource request is made F Pretend to allocate the

Deadlock Avoidance n When a resource request is made F Pretend to allocate the resources F Use deadlock detection to check if the allocation and maximal future requirements lead to a deadlock state F If “yes”, delay the allocation n Expensive running the algorithm so often Deadlock Detection n Allow system to enter deadlock state n Periodically run a deadlock detection algorithm n Recover if deadlock is detected Operating System Concepts 8. 11 Silberschatz, Galvin and Gagne 2002

Single Instance of Each Resource Type n Maintain wait-for graph - a cycle implies

Single Instance of Each Resource Type n Maintain wait-for graph - a cycle implies deadlock (but not vice versa) n Cycle detection is O(n 2) Resource-Allocation Graph Operating System Concepts 8. 12 Corresponding wait-for graph Silberschatz, Galvin and Gagne 2002

Several Instances of a Resource Type n A cycle in the wait-for graph does

Several Instances of a Resource Type n A cycle in the wait-for graph does not imply deadlock n Detection algorithm data structures F Available: A vector of length m indicates the number of available resources of each type. F Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. F Request: An n x m matrix indicates the current request of each process. If Request [ij] = k, then process Pi is requesting k more instances of resource type. Rj. F Work: A vector length m F Finish: A vector length n n Algorithm is O(mn 2) Operating System Concepts 8. 13 Silberschatz, Galvin and Gagne 2002

Detection Algorithm Work = Available; //---Copy what is available now For i = 1,

Detection Algorithm Work = Available; //---Copy what is available now For i = 1, 2, …, n, if Allocationi 0, then Finish[i] = false; //---Pi needs more resources else Finish[i] = true; do { Find an index i such that (Finish[i] == false && Request[i] Work) If i exists { //----Pi can get its resources Work = Work + Allocation[i] Finish[i] = true } while (i exists); If there exists i such that Finish[i] == false the system is in deadlock state Pi is deadlocked. Operating System Concepts 8. 14 Silberschatz, Galvin and Gagne 2002

Example of Detection Algorithm n Five processes P 0 through P 4; three resource

Example of Detection Algorithm n Five processes P 0 through P 4; three resource types A (7 instances), B (2 instances), and C (6 instances). n Snapshot at time T 0: Allocation Request Available ABC ABC P 0 010 000 P 1 200 202 P 2 303 000 P 3 211 100 P 4 002 n Sequence <P 0, P 2, P 3, P 1, P 4> will result in Finish[i] = true for all i. Operating System Concepts 8. 15 Silberschatz, Galvin and Gagne 2002

Example (Cont. ) n P 2 requests an additional instance of type C. Request

Example (Cont. ) n P 2 requests an additional instance of type C. Request ABC P 0 000 P 1 201 P 2 001 P 3 100 P 4 002 n State of system? F Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. F Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4. Operating System Concepts 8. 16 Silberschatz, Galvin and Gagne 2002

Deadlock Recovery n Abort all deadlocked processes. n Abort one process at a time

Deadlock Recovery n Abort all deadlocked processes. n Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort? F Priority of the process. F How long process has computed, and how much longer to F F completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch? n Resource preemption F Selecting a victim – minimize cost. F Rollback – return to some safe state, restart process for that state. F Starvation – same process may always be picked as victim include number of rollback in cost factor. Operating System Concepts 8. 17 Silberschatz, Galvin and Gagne 2002