CSC 4103: Ch.7 Deadlocks

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/79

encourage image

There are no tags or description

Looks like no tags are added yet.

80 Terms

1
New cards

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.

2
New cards

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.

3
New cards

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.

4
New cards

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.

5
New cards

Four necessary conditions for deadlock

  1. Mutual exclusion; 2. Hold and wait; 3. No preemption; 4. Circular wait. ALL four conditions must hold simultaneously for deadlock to occur.


6
New cards

Mutual exclusion (Deadlock condition)

Only one process at a time can use a particular non-sharable resource.

7
New cards

Hold and wait

A process holds at least one resource while waiting to acquire additional resources held by other processes.

8
New cards

No preemption

A resource cannot be forcibly taken away from a process. It can only be released voluntarily by the process holding it.

9
New cards

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.

10
New cards

How does a process utilize a resource?

  1. Request the resource; 2. Use the resource; 3. Release the resource.


11
New cards

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.

12
New cards

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.

13
New cards

Components of a Resource-Allocation Graph

  1. Circles represent processes; 2. Rectangles represent resource types; 3. Dots inside rectangles represent individual resource instances; 4. Directed arrows represent requests or assignments.


14
New cards

Request edge (Pi → Rj)

A directed arrow FROM a process TO a resource, indicating that the process is requesting an instance of that resource.

15
New cards

Assignment edge (Rj → Pi)

A directed arrow FROM a resource instance TO a process, indicating that the process currently holds that resource.

16
New cards

What does a RAG with no cycles indicate?

There is NO deadlock.

17
New cards
<p>What does a RAG with a cycle AND only one instance per resource type indicate?</p>

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

Deadlock exists.

18
New cards
<p>What does a RAG with a cycle and multiple instances per resource type indicate?</p>

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.

19
New cards

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.

20
New cards

Methods for handling deadlocks

  1. Prevention or avoidance: Ensure the system never enters a deadlocked state;

  2. Detection and recovery: Allow deadlock to occur, detect it, and recover;

  3. Ignorance: Ignore the possibility of deadlock.


21
New cards

Deadlock Prevention

Restricts how processes request and acquire resources so that at least ONE of the four necessary conditions for deadlock can never hold.

22
New cards

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.

23
New cards

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.

24
New cards

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.

25
New cards

Preventing Circular Wait

Assign a numerical ordering to all resource types and require processes to request resources in increasing numerical order.

26
New cards

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.

27
New cards

Disadvantage of deadlock prevention

It can result in low resource or device utilization because restrictions may prevent processes from using otherwise available resources.

28
New cards

Deadlock Avoidance

Dynamically examines the current resource-allocation state before granting requests to ensure that the system remains in a safe state.

29
New cards

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.

30
New cards

Resource-allocation state

Defined by:

1. Number of available resources;

2. Number of allocated resources;

3. Maximum resource demands of each process.

31
New cards

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

32
New cards

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.

33
New cards

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.

34
New cards

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.

35
New cards
<p>Unsafe State</p>

Unsafe State

A state in which no safe sequence exists. The system cannot guarantee that all processes will be able to complete execution.

36
New cards

Does an unsafe state necessarily mean deadlock?

No. An unsafe state means deadlock is possible, but the system may not currently be deadlocked.

37
New cards

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.

38
New cards

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.

39
New cards

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.

40
New cards

Life cycle of a RAG claim edge

  1. Claim edge becomes a request edge when the resource is requested;

  2. Request edge becomes an assignment edge when the resource is allocated;

  3. Assignment edge becomes a claim edge again when the resource is released.


41
New cards

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.

42
New cards

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.

43
New cards

Requirements of Banker's Algorithm

  1. Each process declares its maximum resource requirements in advance;

  2. Requests are granted only if the system remains safe;

  3. A process that receives all its resources must return them within a finite amount of time.


44
New cards

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.

45
New cards

Banker's Algorithm data structures

  1. Available;

  2. Max;

  3. Allocation;

  4. 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.


46
New cards

Available (Banker's Algorithm)

A vector of length [m]representing how many instances of each resource type are currently free and available for allocation.

47
New cards

Max (Banker's Algorithm)

A n x m matrix representing the maximum number of instances of each resource type that each process may require.

48
New cards

Allocation (Banker's Algorithm)

A matrix representing how many instances of each resource type are currently assigned to each process.

49
New cards

Need (Banker's Algorithm)

A matrix representing how many ADDITIONAL instances of each resource type a process may still require to finish execution.

50
New cards

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.

51
New cards

Calculating Available resources

Available = Total resources - Sum of currently allocated resources. Calculate this separately for each resource type.

52
New cards

Work vector (Safety Algorithm)

A temporary vector of length m representing the resources available during the simulated execution of processes. Initially, Work = Available.

53
New cards

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.

54
New cards

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.

55
New cards

Safety Algorithm (Steps)

  1. Initialize Work = Available and Finish[i] = false for all processes;

  2. Find an unfinished process where Need[i] <= Work;

  3. Simulate its completion by setting Work = Work + Allocation[i] and Finish[i] = true;

  4. Repeat until all processes finish or no eligible process remains. If all Finish values are true, the system is safe.


56
New cards

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.

57
New cards

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.

58
New cards

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.

59
New cards

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.

60
New cards

Resource-Request Algorithm

Determines whether a new resource request from a process can be granted without placing the system in an unsafe state.

61
New cards

Request vector (Banker's Algorithm)

A vector representing the number of additional instances of each resource type that a particular process is currently requesting.

62
New cards

Resource-Request Algorithm (Steps)

  1. Check Request[i] <= Need[i]. If false, raise an error;

  2. Check Request[i] <= Available. If false, the process must wait;

  3. Temporarily allocate the requested resources;

  4. Run the Safety Algorithm. If safe, grant the request. If unsafe, restore the original state and make the process wait.


63
New cards

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].

64
New cards

What happens if Request > Need?

An error occurs because the process has exceeded its previously declared maximum resource requirements.

65
New cards

What happens if Request > Available?

The process must wait because there are not enough currently available resources to satisfy the request.

66
New cards

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.

67
New cards

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.

68
New cards

Deadlock Detection

Allows the system to enter a deadlocked state, detects the deadlock, and then performs recovery.

69
New cards

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.

70
New cards

Methods of recovering from deadlock

  1. Process termination;

  2. Resource preemption.

  3. Complete Abortion


71
New cards

Recovery through Process Termination

Terminate one or more deadlocked processes so that their allocated resources are released and the deadlock cycle is eliminated.

72
New cards

Types of Process Termination

  1. 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).


73
New cards

Factors when selecting a process to terminate

  1. 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.


74
New cards

Recovery through Resource Preemption

Forcibly remove resources from selected processes and allocate them to other processes until the deadlock is resolved.

75
New cards

Three issues with Resource Preemption

  1. Victim selection;

  2. Rollback;

  3. Starvation.


76
New cards

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.

77
New cards

Rollback

Returning a process to a previous safe state and restarting execution from that state. Determining an appropriate safe state can be difficult.

78
New cards

Starvation during Resource Preemption

Occurs when the same process is repeatedly selected as the victim and cannot complete execution.

79
New cards

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.

80
New cards

Order to which we should choose to abort

  1. Priority of the process

  2. How long process has computed, and how much longer to completion

  3. Resources the process has used

  4. Resources process needs to complete

  5. How many processes will need to be terminated

  6. Is process interactive or batch?