1/79
Looks like no tags are added yet.
Deadlock
A set of blocked processes where each process holds a resource and waits to acquire another resource held by another process in the set. None of the deadlocked processes can continue execution.
Deadlock example (Semaphores)
Two semaphores A and B are initialized to 1. P0 acquires A and P1 acquires B. P0 then requests B while P1 requests A. Neither process can continue because each is waiting for the other to release its resource.
Does acquiring mutex locks in opposite orders ALWAYS cause deadlock?
No. Deadlock depends on the order of execution. If one thread acquires and releases both mutexes before the other thread acquires either, deadlock does not occur.
Deadlock vs. Starvation
Deadlock: A group of processes cannot continue because they are waiting on each other. Starvation: A particular process is repeatedly denied resources while other processes continue executing.
Four necessary conditions for deadlock
Mutual exclusion; 2. Hold and wait; 3. No preemption; 4. Circular wait. ALL four conditions must hold simultaneously for deadlock to occur.
Mutual exclusion (Deadlock condition)
Only one process at a time can use a particular non-sharable resource.
Hold and wait
A process holds at least one resource while waiting to acquire additional resources held by other processes.
No preemption
A resource cannot be forcibly taken away from a process. It can only be released voluntarily by the process holding it.
Circular wait
A circular chain of processes where each process waits for a resource held by the next process in the chain, and the last process waits for a resource held by the first.
How does a process utilize a resource?
Request the resource; 2. Use the resource; 3. Release the resource.
Resource type vs. Resource instance
A resource type is a category of resources (CPU cycles, memory, I/O devices). An instance is an individual unit of that resource type.
Resource-Allocation Graph (RAG)
A directed graph consisting of vertices V (processes (P) and resource (R) types) and edges E that represent which resources processes are requesting or currently holding.
Components of a Resource-Allocation Graph
Circles represent processes; 2. Rectangles represent resource types; 3. Dots inside rectangles represent individual resource instances; 4. Directed arrows represent requests or assignments.
Request edge (Pi → Rj)
A directed arrow FROM a process TO a resource, indicating that the process is requesting an instance of that resource.
Assignment edge (Rj → Pi)
A directed arrow FROM a resource instance TO a process, indicating that the process currently holds that resource.
What does a RAG with no cycles indicate?
There is NO deadlock.

What does a RAG with a cycle AND only one instance per resource type indicate?
Deadlock exists.

What does a RAG with a cycle and multiple instances per resource type indicate?
There is a POSSIBILITY of deadlock, but deadlock is not guaranteed.
Why can a RAG contain a cycle without deadlock?
When multiple resource instances exist, another process outside the cycle may finish executing and release an instance, allowing a waiting process within the cycle to continue.
Methods for handling deadlocks
Prevention or avoidance: Ensure the system never enters a deadlocked state;
Detection and recovery: Allow deadlock to occur, detect it, and recover;
Ignorance: Ignore the possibility of deadlock.
Deadlock Prevention
Restricts how processes request and acquire resources so that at least ONE of the four necessary conditions for deadlock can never hold.
Preventing Mutual Exclusion
Allow resources to be shared whenever possible. Mutual exclusion must still exist for non-sharable resources, so unnecessary synchronization should be avoided.
Preventing Hold and Wait
Require processes to either: 1. Request and receive ALL necessary resources before execution begins; OR 2. Request resources only when they currently hold no resources.
Preventing No Preemption
If a process requests an unavailable resource, force it to release its currently held resources. The process restarts only when it can acquire both its old resources and the newly requested resources.
Preventing Circular Wait
Assign a numerical ordering to all resource types and require processes to request resources in increasing numerical order.
How can deadlock be prevented when two threads acquire two mutex locks?
Require both threads to acquire the mutexes in the SAME order. This prevents circular wait.
Disadvantage of deadlock prevention
It can result in low resource or device utilization because restrictions may prevent processes from using otherwise available resources.
Deadlock Avoidance
Dynamically examines the current resource-allocation state before granting requests to ensure that the system remains in a safe state.
What information is required for deadlock avoidance?
Each process must declare in advance (a priori) the maximum number of instances of each resource type it may need.
Resource-allocation state
Defined by:
1. Number of available resources;
2. Number of allocated resources;
3. Maximum resource demands of each process.
How is the Deadlock-avoidance algorithm optimal?
It dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition
Deadlock Prevention vs. Deadlock Avoidance
Prevention restricts resource requests to eliminate at least one necessary deadlock condition.
Avoidance examines each resource request and grants it only if the resulting system state is safe.
Safe State
A system state in which at least one sequence exists that allows ALL processes to obtain their remaining resources, complete execution, and release their resources.
Safe Sequence
An ordering of processes where each process's remaining resource needs can be satisfied by currently available resources PLUS the resources released by processes that finish before it.

Unsafe State
A state in which no safe sequence exists. The system cannot guarantee that all processes will be able to complete execution.
Does an unsafe state necessarily mean deadlock?
No. An unsafe state means deadlock is possible, but the system may not currently be deadlocked.
Safe State vs. Unsafe State vs. Deadlock
Safe: All processes can complete in some order. Unsafe: Completion of all processes cannot be guaranteed. Deadlock: Processes are already indefinitely blocked waiting for each other.
Resource-Allocation Graph Scheme (Deadlock Avoidance)
A deadlock avoidance method designed for systems with only ONE instance of each resource type. Uses claim edges to represent possible future resource requests.
Claim edge (Pi → Rj)
A DASHED arrow from a process to a resource indicating that the process MAY request that resource in the future. Claim edges must be declared before the process begins execution.
Life cycle of a RAG claim edge
Claim edge becomes a request edge when the resource is requested;
Request edge becomes an assignment edge when the resource is allocated;
Assignment edge becomes a claim edge again when the resource is released.
Resource-Allocation Graph Algorithm
A resource request can only be granted if converting the request edge into an assignment edge does NOT create a cycle in the graph, including its claim edges.
Banker's Algorithm
A deadlock avoidance algorithm designed for systems with MULTIPLE instances of resource types. It determines whether granting a resource request will leave the system in a safe state.
Requirements of Banker's Algorithm
Each process declares its maximum resource requirements in advance;
Requests are granted only if the system remains safe;
A process that receives all its resources must return them within a finite amount of time.
RAG Algorithm vs. Banker's Algorithm
RAG avoidance algorithm: Used when each resource type has only one instance. Banker's Algorithm: Used when resource types can have multiple instances.
Banker's Algorithm data structures
Available;
Max;
Allocation;
Need. If n = number of processes and m = number of resource types, Available is a vector of length m and the remaining three are n × m matrices.
Available (Banker's Algorithm)
A vector of length [m]representing how many instances of each resource type are currently free and available for allocation.
Max (Banker's Algorithm)
A n x m matrix representing the maximum number of instances of each resource type that each process may require.
Allocation (Banker's Algorithm)
A matrix representing how many instances of each resource type are currently assigned to each process.
Need (Banker's Algorithm)
A matrix representing how many ADDITIONAL instances of each resource type a process may still require to finish execution.
Calculating the Need matrix
Need[i,j] = Max[i,j] - Allocation[i,j].
Subtract the currently allocated resources from the maximum resource requirements for each process and resource type.
Calculating Available resources
Available = Total resources - Sum of currently allocated resources. Calculate this separately for each resource type.
Work vector (Safety Algorithm)
A temporary vector of length m representing the resources available during the simulated execution of processes. Initially, Work = Available.
Finish vector (Safety Algorithm)
A Boolean vector of length n indicating whether each process has been able to finish during the simulation. Initially, Finish[i] = false for every process.
Safety Algorithm (Purpose)
Determines whether the current resource-allocation state is safe by attempting to find a sequence in which ALL processes can complete execution.
Safety Algorithm (Steps)
Initialize Work = Available and Finish[i] = false for all processes;
Find an unfinished process where Need[i] <= Work;
Simulate its completion by setting Work = Work + Allocation[i] and Finish[i] = true;
Repeat until all processes finish or no eligible process remains. If all Finish values are true, the system is safe.
How do you compare Need and Work?
Compare each resource type individually. EVERY value in the Need vector must be less than or equal to the corresponding value in Work. Do not compare the sums of the vectors.
Why is Allocation added to Work instead of Max when a process finishes?
The process temporarily receives its remaining Need from Work and returns both its Need and previously allocated resources upon completion. The net increase in Work is therefore its original Allocation.
What happens if no unfinished process has Need <= Work?
If all processes have finished, the system is safe. If unfinished processes remain, the system is unsafe because no safe sequence has been found.
Can a system have multiple safe sequences?
Yes. More than one ordering of processes may satisfy the safety algorithm. Only one valid sequence is necessary to establish that the system is safe.
Resource-Request Algorithm
Determines whether a new resource request from a process can be granted without placing the system in an unsafe state.
Request vector (Banker's Algorithm)
A vector representing the number of additional instances of each resource type that a particular process is currently requesting.
Resource-Request Algorithm (Steps)
Check Request[i] <= Need[i]. If false, raise an error;
Check Request[i] <= Available. If false, the process must wait;
Temporarily allocate the requested resources;
Run the Safety Algorithm. If safe, grant the request. If unsafe, restore the original state and make the process wait.
How do you temporarily allocate resources in the Resource-Request Algorithm?
Available = Available - Request[i]; Allocation[i] = Allocation[i] + Request[i]; Need[i] = Need[i] - Request[i].
What happens if Request > Need?
An error occurs because the process has exceeded its previously declared maximum resource requirements.
What happens if Request > Available?
The process must wait because there are not enough currently available resources to satisfy the request.
If Request <= Need and Request <= Available, can the request automatically be granted?
NO. The Safety Algorithm must still be performed to determine whether granting the request would leave the system in a safe state.
What happens if a temporary resource allocation results in an unsafe state?
Restore the original Available, Allocation, and Need values. The requesting process must wait.
Deadlock Detection
Allows the system to enter a deadlocked state, detects the deadlock, and then performs recovery.
Deadlock Ignorance
Ignores the possibility of deadlock and does not implement general deadlock handling. Many operating systems use this approach because deadlock handling introduces overhead.
Methods of recovering from deadlock
Process termination;
Resource preemption.
Complete Abortion
Recovery through Process Termination
Terminate one or more deadlocked processes so that their allocated resources are released and the deadlock cycle is eliminated.
Types of Process Termination
Abort ALL deadlocked processes (high expense because useful work is discarded); 2. Abort ONE process at a time until deadlock is eliminated (high overhead because detection must be repeated after each termination).
Factors when selecting a process to terminate
Process priority; 2. Time already spent executing and time remaining; 3. Resources already used; 4. Resources still needed; 5. Number of processes that must be terminated; 6. Whether the process is interactive or batch.
Recovery through Resource Preemption
Forcibly remove resources from selected processes and allocate them to other processes until the deadlock is resolved.
Three issues with Resource Preemption
Victim selection;
Rollback;
Starvation.
Victim selection
Selecting which process will have its resources taken away while minimizing the cost of recovery, such as resources held and time already spent executing.
Rollback
Returning a process to a previous safe state and restarting execution from that state. Determining an appropriate safe state can be difficult.
Starvation during Resource Preemption
Occurs when the same process is repeatedly selected as the victim and cannot complete execution.
How can starvation during Resource Preemption be prevented?
Include the number of previous rollbacks in the cost of selecting a victim so that the same process is not repeatedly selected indefinitely.
Order to which we should 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?