Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With...

23
Lecture 11: Deadlock TIE-02500 Rinnakkaisuus TIE-02506 Concurrency

Transcript of Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With...

Page 1: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Lecture 11: DeadlockTIE-02500 Rinnakkaisuus

TIE-02506 Concurrency

Page 2: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

| 1

Page 3: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

| 2

Page 4: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

What is deadlock?

• Deadlock is the state of indefinite waiting that processes may reach when competing for system resources

• The following sequence of steps describes how a process acquires and uses resources:1. Request one or more resource instances

2. Acquire the resource instances if they are available. The operating system allocates an available resource when it is requested by a process. If the resource instances are not available, the process has to wait until the resource instances become available.

3. Use the resource instance for a finite time interval

4. Release the resource instances. The operating system deallocates the resource instances and makes them available for other requesting processes.

| 3

Page 5: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Deadlock exampleSuppose there are two processes, P1 and P2, and two resource types, R1 and R2. The processes are attempting to complete the following sequence of steps in their executions:

1. Process P1 requests resource R1

2. Process P2 requests resource R2

3. Process P1 acquires resource R1

4. Process P2 acquires resource R2

5. Process P1 requests resource R2

6. Process P1 is suspended because resource R2 is not available

7. Process P2 requests resource R1

8. Process P2 is suspended because resource R1 is not available

9. The executions of both processes have been suspended

| 4

Page 6: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Resource Allocation Graph• Resource allocation graph is a directed graph that is

very useful in showing the state of a system

• Two types of nodes that are included in the graph:

• Set of processes, P = {P1, P2, P3, …, Pn}

• Set of resource types, R = {R1, R2, R3, …, Rm}

• There are two types of edges drawn as arrows:

• An edge from a process to a resource type indicates that the process is requesting one instance of the resource type

• An edge from a resource type to a process indicates that one instance of the resource type has been allocated to the process

| 5

Page 7: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Resource Allocation Graph ExampleA loop in the graph is not automatically a deadlock

| 6

Page 8: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Four NECESSARY and SUFFICIENT conditionsfor deadlock

1. Mutual exclusion• Resources involved are unshareable

2. Hold and wait• Thread must hold the resources it already has allocated

3. Circular wait• A cycle in resource graph is necessary for deadlock to occur

4. No pre-emption of resources• Only owner can release recourses

| 7

Page 9: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

What to do?

• IGNORE• The ostrich algorithm

•Deadlock prevention (design)• Make it impossible by denying at least one of the conditions

•Deadlock detection• Periodically check for deadlock and resolve it (but what to do?)

•Deadlock avoidance (runtime)• Do not grant a resource, which would lead to deadlock

| 8

Page 10: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Deadlock Prevention

• Deadlock prevention methods ensure that at least one of the four conditions is never met (always false)1. Mutual exclusion

2. Hold and wait

3. Circular wait

4. No pre-emption

• We assume that all resources must be accessed in a mutually exclusive manner, and that once a process has acquired a resource, it will not be forced to release the resource• This assumption implies that conditions 1 and 4 will be considered true

• Deadlock prevention techniques focus on the other two conditions: hold and wait, and circular wait

| 9

Page 11: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Deadlock Prevention:Disallowing Hold and Wait

• The strategy is to preclude a process from holding one or more resources and at the same time requesting other resources

• There are two techniques to accomplish this:

• A process must acquire all the resources it needs before starting to use acquired resources

• A process must release all resources it has acquired before requesting any new resources

• Dining philosophers example:

• Deadlock prevention is carried out using the first technique

• A philosopher process can acquire its left and right forks only if both forks are available

• The checking for the availability of the two forks and the acquisition of both forks are carried out in a critical section

• Only one philosopher process can be carrying out these activities at any time

| 10

Page 12: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Deadlock Prevention: Disallowing Circular Wait• The circular wait condition exists in a collection of processes P1 to Pn, if process P1 is

waiting for a resource held by P2, process P2 is waiting for a resource held by P3, and process Pn is waiting for a resource held by P1

• A technique for disallowing (preventing) the circular wait condition is to assign a total ordering to all the resource types in the system

• Each resource type can be assigned a unique integer number, or an index number

• A simple application of this ordering is to only allow a process to acquire a resource, Rk, if Rk > Rjfor some resource Rj that the process already holds

• Dining philosophers example:• Fork with index 0 (f0) must be acquired before the fork with index 1 (f1)

• Fork with index 1 (f1) must be acquired before acquiring the fork with index 2 (f2)

• The fifth philosopher must first acquire the right fork, with index 0 (f0), then the left fork, with index 4 (f4)

| 11

Page 13: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Deadlock Detection and Recovery

| 12

• With the deadlock detection method, the operating system allocates resources to the requesting processes whenever sufficient resources are available

• This may lead to deadlock• The operating system must periodically execute a

deadlock detection algorithm

• Detection• The operating system can check for deadlock every time a

resource is allocated• Other algorithms are used to detect cycles in the resource

allocation graph• The techniques are based on incremental changes to the

system state• The processes that are in deadlock are identified, and a

procedure is initiated to stop deadlock

• Recovery• Aborting processes• Rollback

Page 14: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Deadlock Avoidance• Avoidance techniques allow a system to change state by allocating resources only when it

is certain that deadlock will not occur by subsequent resource allocations

• The system analyzes the current resource allocation state to determine that it is a safe state

• Each time there is a resource request, the system analyzes the current state by examining the allocation status of all resources and the current resource needs of the processes up to their maximum claim

• The maximum resource claim of a process is the total number of resource instances that a process will ever request

• The goal of avoidance techniques is to determine if there is some sequence of resource requests, allocations, and deallocations that allow every process to eventually complete

• If this sequence cannot be found, then the resource allocation state is said to be an unsafe state

| 13

Page 15: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Deadlock Avoidance: Banker’s Algorithm

•Loans (resources) have a maximum

•Everyone (processes) must get the loan, but not at the same time

•Repay (of loans) is immediate

| 14

Page 16: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Banker’s Algorithm: Software

•Multiple resources of the same type

•Thread has to tell beforehand the number of requested resources

•When thread asks for allocation, it might have to wait

•System keeps ALWAYS enough free resource so that at least one thread can continue

| 15

Page 17: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Banker’s Algorithm: Implementation

•n Threads and m resources

•Available[j] how many resources of type j are available at the moment

•Max[i,j] thread i maximum usage of resource j

•Allocation[ n x m ] How many resources of type j thread i uses right now

•Need[ n x m ] How many resources of type j thread ineeds to continue

•Need[i,j] = Max[i,j] - Allocation[i,j]

| 16

Page 18: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Banker’s Algorithm: Example

| 17

Page 19: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Banker’s Algorithm: Resource Request

| 18

1. IF Request > Need[i] THEN ERROR

2. WHILE Request > Available DO WAIT

3. TRY:

Available = Available - Request

Allocation[i] = Allocation[i] + Request

Need[i] = Need[i] - Request

IF SAFE THEN grant resources to thread i

IF UNSAFE THEN

thread i has to WAIT

ROLLBACK_TRY

rerun with some other thread

Page 20: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Banker’s Algorithm: Safety Algorithm

| 19

init:

Work = Available

Finish = { false, false, false, false, ... }

LOOP

FIND any i for which holds:

// can we give everything i needs?

Finish[i] == false AND Need[i] <= Work

IF not found: break

ELSE

// any granted allocation will come back later

Work = Work + Allocation[i]

Finish[i] = true

END LOOP

IF all(i): Finish[i] == true THEN

SAFE

ELSE

UNSAFE

Page 21: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Banker’s Algorithm: Safe/Unsafe

| 20

Page 22: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Limitations of Banker’s Algorithm

•Fixed number of resources and threads

•Max resource needs have to be known beforehand

| 21

Page 23: Lecture 11: Deadlockrinn/slides2020/Lecture 11.pdf · Deadlock Detection and Recovery | 12 •With the deadlock detection method, the operating system allocates resources to the requesting

Task

A system has four resource types with (5, 3, 5, 3) and five processes with a maximum resource claim: P 1 with (2, 2, 1, 1), P 2 with (1, 2, 1, 2), P 3 with (1, 1, 2, 1), P 4 with (3, 1, 2, 0), and P 5 with (2, 1, 1, 0). The resource allocations are P 1 with (1, 1, 0, 0), P 2 with (0, 1, 1, 0), P 3 with (1, 0, 1, 1), P 4 with (2, 1, 2, 0), and P 5 with (1, 0, 1, 0). Is this system in a safe state? Give good arguments.

| 22