Processes Synchronization - Part II PDF
Document Details
Uploaded by RaptQuasimodo
KTH Royal Institute of Technology
2023
Amir H. Payberah
Tags
Related
- deadlock.pdf
- Ch 7 Process Coordination (Deadlocks) - Mac 2024 (2).pdf
- Deadlock - Resource Allocation and Management - PDF
- The Deadlock of Democracy in Brazil (2001) PDF - Introduction
- Artificial Intelligence & Data Science S.E. Operating Systems Past Paper PDF - 2019 Pattern
- Process Synchronization and Deadlocks PDF
Summary
This document presents a lecture on processes synchronization, covering concepts like deadlocks, prevention, avoidance, and detection. It discusses different algorithms used in deadlocks.
Full Transcript
Processes Synchronization - Part II Amir H. Payberah [email protected] Oct. 3, 2023 Deadlocks 1 / 55 Motivation ▶ Multiprogramming environment: several processes compete for a finite number of resources. ▶ A process requests resources: if the resources are not available at that time, the proc...
Processes Synchronization - Part II Amir H. Payberah [email protected] Oct. 3, 2023 Deadlocks 1 / 55 Motivation ▶ Multiprogramming environment: several processes compete for a finite number of resources. ▶ A process requests resources: if the resources are not available at that time, the process enters a waiting state. ▶ What if the requests resources are held by other waiting processes? ▶ This situation is called a deadlock. 2 / 55 Deadlock System Model ▶ System consists of m resources: R1 , R2 , · · · , Rm ▶ Resource types: CPU cycles, memory space, I/O devices ▶ Each resource type Ri has Wi instances. ▶ Each process utilizes a resource as follows: • • • Request Use Release 3 / 55 Deadlock Characterization (1/3) ▶ Deadlock can arise if four conditions hold simultaneously: • • • • Mutual exclusion Hold and wait No preemption Circular wait 4 / 55 Deadlock Characterization (2/3) ▶ 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. 5 / 55 Deadlock Characterization (3/3) ▶ No preemption • ▶ A resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait • • • • • A set processes: {P0 , P1 , · · · , Pn } P0 is waiting for a resource that is held by P1 P1 is waiting for a resource that is held by P2 ... Pn is waiting for a resource that is held by P0 6 / 55 Deadlock Example (1/2) /* Create and initialize the mutex locks */ pthread_mutex_t first_mutex; pthread_mutex_t second_mutex; pthread_mutex_init(&first_mutex, NULL); pthread_mutex_init(&second_mutex, NULL); 7 / 55 Deadlock Example (2/2) void *thread_one(void *args) { pthread_mutex_lock(&first_mutex); pthread_mutex_lock(&second_mutex); // do some work pthread_mutex_unlock(&second_mutex); pthread_mutex_unlock(&first_mutex); pthread_exit(0); } void *thread_two(void *args) { pthread_mutex_lock(&second_mutex); pthread_mutex_lock(&first_mutex); // do some work pthread_mutex_unlock(&first_mutex); pthread_mutex_unlock(&second_mutex); pthread_exit(0); } 8 / 55 Resource-Allocation Graph 9 / 55 Resource-Allocation Graph (1/2) ▶ A set of vertices V and a set of edges E . ▶ Vertices • • ▶ All the processes in the system: P = P1 , P2 , · · · , Pn All resource types in the system: R = R1 , R2 , · · · , Rm Edges • • Request edge: directed edge Pi → Rj Assignment edge: directed edge Rj → Pi 10 / 55 Resource-Allocation Graph (2/2) ▶ Process (vertices) ▶ Resource type with 4 instances (vertices) ▶ Pi requests instance of Rj (edge) ▶ Pi is holding an instance of Rj (edge) 11 / 55 Resource-Allocation Graph Example (1/3) ▶ Example of a resource allocation graph. 12 / 55 Resource-Allocation Graph Example (2/3) ▶ Resource allocation graph with a deadlock. 13 / 55 Resource-Allocation Graph Example (3/3) ▶ Resource allocation graph with a cycle but no deadlock. 14 / 55 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. 15 / 55 Methods for Handling Deadlocks ▶ Ensure that the system will never enter a deadlock state: • • ▶ Deadlock prevention Deadlock avoidance Allow the system to enter a deadlock state and then recover. 16 / 55 Deadlock Prevention 17 / 55 Deadlock Prevention (1/3) ▶ Deadlock can arise if four conditions hold simultaneously: • • • • ▶ Mutual exclusion Hold and wait No preemption Circular wait Restrain the ways requests can be made. 18 / 55 Deadlock Prevention (2/3) ▶ Mutual exclusion • • ▶ Not required for sharable resources, e.g., read-only files. Must hold for non-sharable resources. Hold and wait • • • • • Must guarantee that whenever a process requests a resource, it does not hold any other resources. Solution 1: require a process to request and be allocated all its resources before it begins execution. Solution 2: allows a process to request resources only when it has none. Low resource utilization Starvation possible 19 / 55 Deadlock Prevention (3/3) ▶ No preemption • • ▶ If a process that is holding some resources, requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. 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. 20 / 55 Deadlock Avoidance 21 / 55 Basic Facts ▶ If a system is in the safe state • ▶ If a system is in the unsafe state • ▶ No deadlock Possibility of deadlock Avoidance • Ensure that a system will never enter an unsafe state. 22 / 55 Safe State (1/2) ▶ Safe state: there exists a sequence ⟨P1 , P2 , · · · , Pn ⟩ of all the processes in the systems such that for each Pi , the resources that Pi can still request be satisfied by: currently available resources + resources held by all the Pj , with j < i. ▶ When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. 23 / 55 Safe State (2/2) ▶ If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. ▶ When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. ▶ When Pi terminates, Pi+1 can obtain its needed resources, and so on. 24 / 55 Avoidance Algorithms ▶ Single instance of a resource type • ▶ Use a resource-allocation graph Multiple instances of a resource type • Use the banker’s algorithm 25 / 55 Resource-Allocation Graph Algorithm 26 / 55 Resource-Allocation Graph Scheme ▶ Claim edge Pi → Rj : indicates that process Pi may request resource Rj ; represented by a dashed line ▶ Claim edge converts to request edge when a process requests a resource. ▶ Request edge converted to an assignment edge when the resource is allocated to the process. ▶ When a resource is released by a process, assignment edge reconverts to a claim edge. ▶ Resources must be claimed a priori in the system. 27 / 55 Resource-Allocation Graph 28 / 55 Unsafe State In Resource-Allocation Graph 29 / 55 Resource-Allocation Graph Algorithm ▶ Suppose that process Pi requests a resource Rj . ▶ The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph. 30 / 55 Banker’s Algorithm 31 / 55 Banker’s Algorithm ▶ Multiple instances ▶ Each process must a priori claim of the maximum use. ▶ When a process requests a resource it may have to wait. ▶ When a process gets all its resources, it must return them in a finite amount of time. 32 / 55 Data Structures for Banker’s Algorithm ▶ n = number of processes, and m = number of resources types ▶ Available: vector of length m. • ▶ Max: n × m matrix. • ▶ If Max[i, j] = k, then process Pi may request at most k instances of resource type Rj . Allocation: n × m matrix. • ▶ If Available[j] = k, there are k instances of resource type Rj available. If Allocation[i, j] = k then Pi is currently allocated k instances of Rj . Need: n × m matrix. • If Need[i, j] = k, then Pi may need k more instances of Rj to complete its task Need[i, j] = Max[i, j] − Allocation[i, j] 33 / 55 Safety Algorithm 1. Let Finish be vector of length n. Initialize: Finish[i] = false for i = 0, 1, · · · n − 1 2. Find an i such that both: 1. Finish[i] = false 2. Needi ≤ Available If no such i exists, go to step 4. 3. Available = Available + Allocationi Finish[i] = true Go to step 2 4. If Finish[i] == true for all i, then the system is in a safe state. 34 / 55 Resource-Request Algorithm for Process Pi (1/2) ▶ Requesti = request vector for process Pi . If Requesti [j] = k, then process Pi wants k instances of resource type Rj . ▶ 1. If Requesti ≤ Needi , go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. ▶ 2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not available. 35 / 55 Resource-Request Algorithm for Process Pi (2/2) ▶ 3. Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available − Requesti Allocationi = Allocationi + Requesti Needi = Needi − Requesti • If safe: the resources are allocated to Pi • If unsafe: Pi must wait, and the old resource-allocation state is restored 36 / 55 Banker’s Algorithm Example (1/3) ▶ 5 processes: P0 through P4 ▶ 3 resource types: • ▶ A (10 instances), B (5 instances), and C (7 instances) Snapshot at time T0 37 / 55 Banker’s Algorithm Example (2/3) ▶ The content of the matrix Need is defined to be Max − Allocation ▶ Is the system safe? ⟨P1 , P3 , P4 , P2 , P0 ⟩ satisfies safety criteria. 38 / 55 Banker’s Algorithm Example (3/3) ▶ P1 Request (1, 0, 2) ▶ Check that Request ≤ Available: (1, 0, 2) ≤ (3, 3, 2) ⇒ true ▶ Executing safety algorithm shows that sequence ⟨P1 , P3 , P4 , P0 , P2 ⟩ satisfies safety requirement. ▶ Can request for (3, 3, 0) by P4 be granted? ▶ Can request for (0, 2, 0) by P0 be granted? 39 / 55 Deadlock Detection 40 / 55 Deadlock Detection ▶ Allow system to enter deadlock state ▶ Detection algorithm ▶ Recovery scheme 41 / 55 Single Instance of Each Resource Type ▶ Maintain wait-for graph. • • Nodes are processes. Pi → Pj if Pi is waiting for Pj . ▶ Periodically invoke an algorithm that searches for a cycle in the graph. ▶ If there is a cycle, there exists a deadlock. 42 / 55 Resource-Allocation Graph and Wait-for Graph Resource-allocation graph Corresponding Wait-for graph 43 / 55 Data Structures for Deadlock Detection ▶ Available: vector of length m, indicates the number of available resources of each type. ▶ Allocation: n × m matrix, defines the number of resources of each type currently allocated to each process. ▶ Request: n × m matrix, indicates the current request of each process. • If Request[i, j] = k, then Pi requesting k more instances of resource type Rj . 44 / 55 Detection Algorithm (1/2) ▶ 1. Let Finish be vector of length n. Initialize: Finish[i] = false for i = 0, 1, · · · n − 1 ▶ 2. Find an i such that both: 1. Finish[i] = false 2. Requesti ≤ Available If no such i exists, go to step 4. 45 / 55 Detection Algorithm (2/2) ▶ 3. Available = Available + Allocationi Finish[i] = true go to step 2 ▶ 4. If Finish[i] == false, for some i, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked. 46 / 55 Detection Algorithm Example (1/2) ▶ 5 processes: P0 through P4 ▶ 3 resource types: • A (7 instances), B (2 instances), and C (6 instances) ▶ Snapshot at time T0 ▶ Deadlock? Sequence ⟨P0 , P2 , P3 , P1 , P4 ⟩ will result in Finish[i] = true for all i 47 / 55 Detection Algorithm Example (2/2) ▶ P2 requests an additional instance of type C ▶ Can reclaim resources held by process P0 , but insufficient resources to fulfill other processes; requests ▶ Deadlock exists, consisting of processes P1 , P2 , P3 , and P4 48 / 55 Recovery From Deadlock 49 / 55 Recovery from Deadlock ▶ Process termination ▶ Resource preemption 50 / 55 Process Termination ▶ Abort all deadlocked processes. ▶ Abort one process at a time until the deadlock cycle is eliminated ▶ In which order should we choose to abort? 1. 2. 3. 4. 5. 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. 51 / 55 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. 52 / 55 Summary 53 / 55 Summary ▶ Deadlock ▶ Four simultaneous conditions: mutual exclusion, hold and wait, no preemption, circular wait ▶ Deadlock prevention ▶ Deadlock avoidance: resource-allocation algorithm, banker’s algorithm ▶ Deadlock detection: Wait-for graph ▶ Deadlock recovery: process termination, resource preemption 54 / 55 Questions? Acknowledgements Some slides were derived from Avi Silberschatz slides. 55 / 55