Download presentation
Presentation is loading. Please wait.
Published byAlvin Rich Modified over 9 years ago
1
Silberschatz, Galvin and Gagne 2002 8.1 Operating System Concepts Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(S);wait(Q); wait(Q);wait(S); signal(S);signal(Q); signal(Q)signal(S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.
2
Silberschatz, Galvin and Gagne 2002 8.2 Operating System Concepts Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait: there exists a set {P 0, P 1, …, P n } 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, …, P n–1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0.
3
Silberschatz, Galvin and Gagne 2002 8.3 Operating System Concepts System Model for OS Solution Resource types R 1, R 2,..., R m CPU cycles, memory space, I/O devices Each resource type R i has W i instances. Each process utilizes a resource as follows: request use release
4
Silberschatz, Galvin and Gagne 2002 8.4 Operating System Concepts Resource-Allocation Graph A set of vertices V and a set of directed edges E V is partitioned into two types: P = {P 1, P 2, …, P n }, the set consisting of all the processes in the system. R = {R 1, R 2, …, R m }, the set consisting of all resource types in the system. Request edge – directed edge P 1 R j Assignment edge – directed edge R j P i Resource-Allocation Graph Process Resource Type with 4 instances P i requests instance of R j P i is holding an instance of R j PiPi PiPi RjRj RjRj
5
Silberschatz, Galvin and Gagne 2002 8.5 Operating System Concepts Basic Facts If graph contains no cycles no deadlock. If graph contains a cycle if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock.
6
Silberschatz, Galvin and Gagne 2002 8.6 Operating System Concepts RAG with No Cycles (no deadlock)
7
Silberschatz, Galvin and Gagne 2002 8.7 Operating System Concepts RAG with a Deadlock
8
Silberschatz, Galvin and Gagne 2002 8.8 Operating System Concepts RAG with a Cycle but No Deadlock
9
Silberschatz, Galvin and Gagne 2002 8.9 Operating System Concepts Methods for Handling Deadlocks Deadlock prevention - Ensure that the system will never enter a deadlock state by constraining possible requests Deadlock avoidance - Stop the system from entering a deadlock state by examining current request and maximal requirement for process (declared in advance) Deadlock detection and recovery - Allow the system to enter a deadlock state and then recover. Deadlock acceptance - Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX.
10
Silberschatz, Galvin and Gagne 2002 8.10 Operating System Concepts Deadlock Prevention Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. 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 Low resource utilization (early requests); starvation possible (always one resource unavailable). No Preemption - if a process requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. Preempted resources are added to the list of resources for which the process is waiting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. Least valuable claimed first
11
Silberschatz, Galvin and Gagne 2002 8.11 Operating System Concepts Deadlock Avoidance When a resource request is made Pretend to allocate the resources Use deadlock detection to check if the allocation and maximal future requirements lead to a deadlock state If “yes”, delay the allocation Expensive running the algorithm so often Deadlock Detection Allow system to enter deadlock state Periodically run a deadlock detection algorithm Recover if deadlock is detected
12
Silberschatz, Galvin and Gagne 2002 8.12 Operating System Concepts Single Instance of Each Resource Type Maintain wait-for graph - a cycle implies deadlock (but not vice versa) Cycle detection is O(n 2 ) Resource-Allocation GraphCorresponding wait-for graph
13
Silberschatz, Galvin and Gagne 2002 8.13 Operating System Concepts Several Instances of a Resource Type A cycle in the wait-for graph does not imply deadlock Detection algorithm data structures Available: A vector of length m indicates the number of available resources of each type. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. Request: An n x m matrix indicates the current request of each process. If Request [i j ] = k, then process P i is requesting k more instances of resource type. R j. Work: A vector length m Finish: A vector length n Algorithm is O(mn 2 )
14
Silberschatz, Galvin and Gagne 2002 8.14 Operating System Concepts Detection Algorithm Work = Available; //---Copy what is available now For i = 1,2, …, n, if Allocation i 0, then Finish[i] = false; //---P i needs more resources else Finish[i] = true; do { Find an index i such that (Finish[i] == false && Request[i] Work) If i exists { //----P i 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 P i is deadlocked.
15
Silberschatz, Galvin and Gagne 2002 8.15 Operating System Concepts Example of Detection Algorithm Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances). Snapshot at time T 0 : AllocationRequestAvailable A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 30 0 0 P 3 2 1 1 1 0 0 P 4 0 0 2 0 0 2 Sequence will result in Finish[i] = true for all i.
16
Silberschatz, Galvin and Gagne 2002 8.16 Operating System Concepts Example (Cont.) P 2 requests an additional instance of type C. Request A B C P 0 0 0 0 P 1 2 0 1 P 2 0 0 1 P 3 1 0 0 P 4 0 0 2 State of system? Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.
17
Silberschatz, Galvin and Gagne 2002 8.17 Operating System Concepts Deadlock Recovery Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch? Resource preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process for that state. Starvation – same process may always be picked as victim - include number of rollback in cost factor.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.