chapter 6 concurrency: deadlock and starvation i zdeadlock zconditions for deadlock zdeadlock...

20
Vanderbilt Institute for Clinical and Translational Research (VICTR) VICTR Application VICTR Application and Review and Review Process Process

Upload: whitney-mcdonald

Post on 18-Dec-2015

241 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Chapter 6 Concurrency: Deadlock and Starvation I

DeadlockConditions for DeadlockDeadlock PreventionDeadlock AvoidanceBanker’s AlgorithmDeadlock DetectionStrategies once Deadlock DetectedDeadlock Detection Algorithm UNIX Concurrency Mechanisms

Page 2: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock

DeadlockDeadlock can be defined as the permanent blocking of a set of processes that either compete for system resources or communicate with each other

All deadlocks Involve conflicting needs for resources by two or more processes

Page 3: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Example of Deadlock (1)Process P Process Q...... ......Get A Get B...... ......Get B Get A...... ......Release A Release B...... ......Release B Release A...... ......

Page 4: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Progress of processes-Example of Deadlock (2)

Progressof Q

ReleaseA

ReleaseB

Get A

Get B

Get A Get B Release A Release B

Progressof P

ARequired

B Required

ARequired

BRequired

deadlockinevitable

1 2

3

4

5

6

P and Qwant B

P and Qwant A

Page 5: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Execution Paths -Example of Deadlock (3)1. Q acquires B and then A, and then

releases B and A. When P resumes execution, it will be able to acquire both resources.

2. Q acquires B and then A. P executes and blocks on a request for A. Q releases B and A. When P resumes execution, it will be able to acquire both resources.

5. P acquires A and then B. Q executes and blocks on a request for B. P releases A and B. When Q resumes execution, it will be able to acquire both resources.

6. P acquires A and then B, and then releases A and B. When Q resumes execution, it will be able to acquire both resources.

Page 6: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock Paths -Example of Deadlock (4)

3. Q acquires B and then P acquires A. Deadlock is inevitable, because as execution process Q will block on A and P will block on B.

4. P acquires A and then Q acquires B. Deadlock is inevitable, because as execution process Q will block on A and P will block on B.

Page 7: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Example of No Deadlock (1)Process P Process Q...... ......Get A Get B...... ......Release A Get A...... ......Get B Release B...... ......Release B Release A...... ......

Page 8: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Example of No Deadlock (2)

Progressof Q

ReleaseA

ReleaseB

Get A

Get B

Get A Release A Get B Release B

Progressof P

A Required B Required

ARequired

BRequired

1 2 3

4

5

6

P and Qwant A

P and Qwant B

Page 9: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Question 1 - Exercise/Home Work (1)

For Figure of No Deadlock, provide a narrative description of each of the six depicted paths.

Page 10: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Reusable Resources

A reusable resource is one that can be safely used by one process at a time and not depleted by that use

Processes obtain resources that they later release for reuse by other processes

Examples of reusable resources: processor time, I/O channels, main and secondary memory, files, databases, and semaphores

Deadlock occurs if each process holds one resource and requests the other

Page 11: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Example of Deadlock

Space is available for allocation of 200K bytes, and the following sequence of events occur

Deadlock occurs if both processes progress to their second request

P1

. . .

. . .Request 80K bytes;

Request 60K bytes;

P2

. . .

. . .Request 70K bytes;

Request 80K bytes;

Page 12: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Consumable Resources

A consumable resource is one that can be created (produced) and destroyed (consumed) by a process

Examples of consumable resources: interrupts, signals, messages, and information in I/O buffers

Deadlock may occur if a Receive message is blocking

May take a rare combination of events to cause deadlock

Page 13: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Example of Deadlock

Deadlock occurs if receive is blocking

P1 is blocked for waiting message from P2P2 is blocked for waiting message from P1

P1

. . .

. . .Receive(P2);

Send(P2);

P2

. . .

. . .Receive(P1);

Send(P1);

Page 14: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Conditions for DeadlockThree conditions of policy must be present for a deadlock to be possible:

Mutual exclusion only one process may use a resource at a

timeHold-and-wait

a process may hold allocated resources while awaiting assignment of others

No preemption no resource can be forcibly removed from

a process holding it

Page 15: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Conditions for Deadlock

Circular waita closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

Page 16: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Circular Wait

ResourceB

ResourceA

ProcessP1

ProcessP2

RequestsHeld by

RequestsHeld By

Page 17: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock Prevention (1)Mutual Exclusion

cannot be disallowedHold-and-Wait

The hold-and-wait condition can be prevented by: require that a process request all its required

resources at one time block the process until all requests can be

granted simultaneously process may be held up for a long time

waiting for all its requests resources allocated to a process may remain

unused for a long time. These resources could be used by other processes

Page 18: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock Prevention (2)No preemptionThis condition can be prevented in several

ways: if a process is denied a further request, the

process must release the original resources if a process cannot obtain a resource, the

process may have to release its resources. Must have capability to restore to current state.

This approach is practical only when the state can be easily saved and restored later, such as the processor.

Page 19: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock Prevention (3)

Circular waitThe circular-wait condition can be

prevented by : define a linear ordering for resources once a resource R is obtained, then it

may subsequently request only those resources of types following R in the ordering.

Page 20: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock Avoidance (1)

Do not start a process if its demands might lead to deadlock

Do not grant an incremental resource request to a process if this allocation might lead to deadlock

Page 21: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock Avoidance (2)

Maximum resource requirement must be stated in advance

Processes under consideration must be independent; no synchronization requirements

There must be a fixed number of resources to allocate

No process may exit while holding resources

Page 22: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Banker’s Algorithm - Deadlock Avoidance (1)

Total amount of each resource in the systemResource = ( R1, R2, ......, Rm )

Total amount of each resource not allocated to a processAvailable = ( V1, V2, ......, Vm )

Page 23: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Banker’s Algorithm - Deadlock Avoidance (2)

Requirement of each process for each resourceC11 C12 ...... C1m

Claim = C21 C22 ...... C2m

.......................... Cki: process k

Cn1 Cn2 ...... Cnm for resource jCurrent allocation

A11 A12 ...... A1m

Allocation = A21 A22 ...... A2m

.......................... Aki: process k

An1 An2 ...... Anm for resource j

Page 24: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Relationships - Banker’s Algorithm (3)

n

Ri = Vi + Aki, for all i: All resources are k=1 either available or allocated.Cki <= Ri, for all k, i: No process can claim

more than the total amount of resources in the system.

Aki <= Cki, for all k, i: No process is allocated more resources of

any type than the process originally claimed to need.

Page 25: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Deadlock Avoidance Policy- Banker’s Algorithm (4)

n

Ri >= C(n+1)i + Sum Cki, for all i k=1

A new process Pn+1 is only started if the maximum claim of all current processes plus those of the new process can be met.

Page 26: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Safe State -Banker’s Algorithm (5)

StateThe state of the system is simply the current allocation of resources to processes. Thus the state consists of the two vectors, Resource and Available, and two matrices, Claim and Allocation.

Safe StateA safe state is a state in which there is at least one sequence in which all of the processes can be run to completion that does not result in a deadlock.

Page 27: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Determination of a Safe State-Banker’s Algorithm (6)

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 3 2 2 P1 1 0 0 9 3 6P2 6 1 3 P2 6 1 2 Resource

VectorP3 3 1 4 P3 2 1 1P4 4 2 2 P4 0 0 2 R1 R2 R3 0 1 1Claim Matrix Allocation Matrix Available

Vector

(a) Initial State - A Safe State?

Page 28: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Determination of a Safe State-Banker’s Algorithm (7)

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 3 2 2 P1 1 0 0 9 3 6P2 0 0 0 P2 0 0 0 Resource

VectorP3 3 1 4 P3 2 1 1P4 4 2 2 P4 0 0 2 R1 R2 R3 6 2 3Claim Matrix Allocation Matrix Available

Vector

(b) P2 Runs to Completion

Page 29: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Determination of a Safe State-Banker’s Algorithm (8)

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 0 0 0 P1 0 0 0 9 3 6P2 0 0 0 P2 0 0 0 Resource

VectorP3 3 1 4 P3 2 1 1P4 4 2 2 P4 0 0 2 R1 R2 R3 7 2 3Claim Matrix Allocation Matrix Available

Vector

(c) P1 Runs to Completion

Page 30: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Determination of a Safe State-Banker’s Algorithm (9)

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 0 0 0 P1 0 0 0 9 3 6P2 0 0 0 P2 0 0 0 Resource

VectorP3 3 1 4 P3 2 1 1P4 4 2 2 P4 0 0 2 R1 R2 R3 9 3 4Claim Matrix Allocation Matrix Available

Vector

(d) P3 Runs to Completion

Page 31: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Banker’s Algorithm - Deadlock Avoidance (10).....

while ((possible) and (rest != null)) dofind a Pk in rest such that

claim[k, *] - allocation[K, *] <= currentavail;if found then currentavail:=currentavail + allocation[K, *];

rest:= rest - {Pk}else

possible:=falseend

end;......

Page 32: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Determination of an Unsafe State-Banker’s Algorithm (11)

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 3 2 2 P1 1 0 0 9 3 6P2 6 1 3 P2 5 1 1 Resource

VectorP3 3 1 4 P3 2 1 1P4 4 2 2 P4 0 0 2 R1 R2 R3 1 1 2Claim Matrix Allocation Matrix Available

Vector

(a) Initial State - A Safe State?

Page 33: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Determination of an Unsafe State-Banker’s Algorithm (12)

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 3 2 2 P1 2 0 1 9 3 6P2 6 1 3 P2 5 1 1 Resource

VectorP3 3 1 4 P3 2 1 1P4 4 2 2 P4 0 0 2 R1 R2 R3 0 1 1Claim Matrix Allocation Matrix Available Vector

(b) P1 Requests One unit each of R1 and R3- An Unsafe State

Page 34: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Question 2 - Exercise/Home Work (1)

R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4

P1 0 0 1 2 0 0 1 2

P2 2 7 5 0 2 0 0 0

P3 6 6 5 6 0 0 3 4

P4 4 3 5 6 2 3 5 4

P5 0 6 5 2 0 3 3 2

Claim Matrix Allocation Matrix Still Needs

6 7 12 12 2 1 0 0

Resource Vector Available Vector

Page 35: Chapter 6 Concurrency: Deadlock and Starvation I zDeadlock zConditions for Deadlock zDeadlock Prevention zDeadlock Avoidance zBanker’s Algorithm zDeadlock

Question 2- Exercise/Home Work (2)

Consider the system given:a. Compute what each process still might request and

display in the columns labeled “still needs.”b. Is this system currently in a safe or unsafe state? Why?c. Is this system currently deadlocked? Why or Why not?d. Which processes, if any, are or may become

deadlocked?e. If a request from P3 arrives for (0, 1, 3, 4), can that

request to be safely granted immediately? In what state (deadlocked, safe, unsafe) would immediately granting that whole request leave the system? Which processes, if any, are or may become deadlocked if this whole request is granted immediately?