34
What is deadlock? Deadlock is the permanent blocking of a set of processes that either compete for system resources or communicate with each other. A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. c b d a 4 4 1 1 3 3 2 2

What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

What is deadlock?

• Deadlock is the permanent blocking of a set of processes thateither compete for system resources or communicate witheach other.

• A set of processes is deadlocked if each process in the set iswaiting for an event that only another process in the set cancause.

c b

d a4 4

1

1

3

32 2

Page 2: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Resources

Resources are a general name given to concrete objects that canbe granted to individual processes, e.g., hardware devices (a tapedrive for example), files, semaphores, etc.

• A preemptable resource is one that can be taken away fromthe process owning it with no ill e�ects. Memory is anexample of a preemptable resource (take away memory from aprocess means swap the process out).

• A nonpreemptable resource, in contrast, is one that cannot betaken away from its current owner without causing thecomputation to fail. For example, burning a CD-ROM isnonpreemptable.

In general, deadlocks involve nonpreemptable resources. Thus ourtreatment will focus on nonpreemptable resources.

Page 3: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

The dining philosophers problem

Another classic interprocess communication (IPC) problem:1 Several (usually 5) silent philosophers sit at a table around a

bowl of spaghetti. Their activities consist of thinking andeating alternatively.

2 A fork is placed between each pair of adjacent philosophers. Aphilosopher can only eat while holding both the fork to theleft and the fork to the right. This implies a philosopher canpick up a second fork while holding the first one.

3 Eating is not limited by the amount of spaghetti left: assumean infinite supply.

4 The major constraint in the problem is that forks must bepicked up and put down one by one.

The problem is to design a set of dining rules (i.e., a concurrentalgorithm) such that each philosopher won’t starve.

Page 4: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

A solution that has deadlock potential

1 semaphore fork[5]={1,1,1,1,1};

2

3 philosopher(int i) {

4 Loop {

5 think();

6 wait(fork[i]);

7 wait(fork[(i+1) mod 5]);

8 eat();

9 signal(fork[(i+1) mod 5]);

10 signal(fork[i]);

11 }

12 }

P3

P0

P2

P4

P1

How a deadlock is possible in this solution?

Page 5: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

The Co�man conditions for deadlock

Co�man et al. (1971) showed that four conditions must hold forthere to be a deadlock:

1 Mutual exclusion condition: Each resource is either currentlyassigned to exactly one process or is available.

2 Hold and wait condition: Processes currently holdingresources that were granted earlier can request new resources.

3 No preemption condition: Resources previously grantedcannot be forcibly taken away from a process. They must beexplicitly released by the process holding them.

4 Circular wait condition: There must be a circular chain of twoor more processes, each of which is waiting for a resource heldby the next member of the chain.

Page 6: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Resource allocation graphs

P1 Ra

Resouce is requested

P1 Ra

Resource is held

Requests Held by

Page 7: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Resource allocation graphs

P1 P2

Rb

Ra

Requests

Requests

Held by

Held by

Circular wait

P1 P2

Rb

Ra

Requests

Requests

Held by

Held by

No deadlock

Page 8: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Resource allocation graphs

p1 p2 p3 p4 p5

1. p1 requests left fork

2. p2 requests left fork

3. p3 requests left fork

4. p4 requests left fork

5. p5 requests left fork

6. p1 requests right fork

7. p2 requests right fork

8. p3 requests right fork

9. p4 requests right fork

10.p5 requests right fork

p1 p2 p3 p4 p5

Page 9: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

How to deal with deadlocks?

1 Just ignore the problem altogether. Maybe if you ignore it, itwill ignore you.

2 Prevention, by structurally negating one of the four conditionsnecessary to cause a deadlock.

3 Dynamic avoidance by careful resource allocation.4 Detection and recovery, let deadlocks occur, detect them, and

take actions.

Page 10: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

The ostrich algorithm

The ostrich algorithm: stick your head in the sand and pretendthere is no problem at all.

• Theorists: Deadlocks are completely unacceptable and mustbe 100% eliminated.

• Engineers: How often deadlock is expected to cause problem,and how serious it is? How often the system crashes for otherreasons? What if deadlocks occur on the average once everyfive years, but system crashes due to hardware failures and OSbugs occur once a week?

Eliminating deadlocks completely is extremely costly, if notimpossible, and will often place inconvenient restrictions on thesystem. Most OS, including Unix and Windows, just ignore theproblem on the assumption that most users would prefer anoccasional deadlock to a rule restricting all users to one process,one open file, and one of everything.

Page 11: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Deadlock prevention

This strategy seeks to impose suitable restrictions on processes sothat deadlocks are structurally impossible.

The four Co�man conditions provide a clue to some possiblesolutions.

Page 12: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Attacking the mutual exclusion condition

If no resources were ever assigned exclusively to a single process,we would never have deadlocks. For example, we can spool printeroutput so that only one printer daemon requests the physicalprinter. However:

1 Not all devices can be spooled.2 Competition for spooling bu�er can itself lead to deadlock.

Conclusion: generally not feasible.

Page 13: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Attacking the hold and wait condition

1 All processes request all their resources together in a singlestep.

2 When requesting a resource, a process first temporarily releaseall the resources it currently holds, then it tries to geteverything it needs all at once.

However:1 One may not know the type and total amount of resources it

will need at the beginning.2 Resource usage is not optimal as multiple resources may be

unnecessarily tied up together for a long time.

Conclusion: too restrictive and non-optimal performance.

Page 14: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Attacking the no preemption condition

Always allow the OS to preempt resources from low priorityprocesses.

This could be costly and could have ill e�ects: e.g., forcibly takingaway the CD-ROM from a process in the middle of CD burning istricky at best and impossible at worst.

Conclusion: the cost is too high.

Page 15: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Attacking the circular wait condition

A possible way to remove circular wait is to provide a linear orderfor all the resources. All resource requests must follow the definedorder.

In other words, if resource i ranks before resource j , then whenmaking requests for both of them, a process must request i first,followed by j .

However it may be impossible to find an ordering that satisfieseveryone. Also, dynamic resource request is hard (what if p1 has r1and r5, and wants r3 dynamically, which cannot be predicted, thenp1 needs to release r5 and requests r3 and then r5).

Conclusion: may not be feasible and non-optimal performance.

Page 16: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Breaking circular wait in the dining philosophers solution

1 Remove one philosopher from the dining room (use anothersemaphore to only allow at most 4 philosophers in the diningroom):

p1 p2 p3 p5

2 Define a linear order for all the forks and make requestsfollowing the order: first get the low-ranked fork and then thehigh-ranked fork:

p1 p2 p3 p5p4

< < < <

Page 17: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Deadlock avoidanceMake dynamic resource allocation decision carefully so that nodeadlock is possible. The following definitions are used:

1 R: a vector denotes the total amount of resources in thesystem for each type, e.g., R = (R1, R2, . . . , Rm)= (3, 15, . . . , 8).

2 V : a vector denotes the currently available resources for eachtype.

3 C : a matrix denotes the requirement of each type of resourceby each process, e.g., Cij denotes the requirement of process ifor resource j . This must be declared in advance by eachprocess. E.g., qm

j=1 Cij denotes the total resource requirementof process i , and qn

i=1 Cij denotes the total requirement ofresource i from all processes.

4 A: a matrix denotes the current allocation of resources toprocesses (similar to C).

Thus C ≠ A is a matrix represents the current need of each type ofresource from each process.

Page 18: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

An example initial state

R1 R2 R3 R1 R2 R3 R1 R2 R3P1 3 2 2 P1 1 0 0 P1 2 2 2

P2 6 1 3 P2 6 1 2 P2 0 0 1

P3 3 1 4 P3 2 1 1 P3 1 0 3P4 4 2 2 P4 0 0 2 P4 4 2 0

Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R39 3 6 0 1 1Resource vector R Available vector V

Page 19: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Deny process initiation

The following conditions must hold:1 Rj = Vj +

qni=1 Aij : total amount equals available ones plus

used ones.2 Cij Æ Rj : one cannot claim more than available.3 Aij Æ Cij : allocation cannot exceed claimed.

Deny a process (pn+1) initiation entirely if Rj < Cn+1,j +qn

i=1 Cij ,for all j . This is to check whether enough resources are available.

Page 20: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Deny resource request

Once a process makes request for a resource, use banker’s

algorithm to determine if such a request shall be granted or not.

Safe state: a state (R, V , C , A) in which there exists at least onesequence of resource allocation that does not result in deadlock.

A process pi can run to its completion if all its resource requestscan be satisfied, i.e., Cij ≠ Aij Æ Vij , for all j (need does not exceedavailable). After pi completes, it will release all its allocatedresources.

Determining if a state is safe amounts to finding a possibleexecution in which all processes can complete.

Page 21: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of banker’s algorithm

R1 R2 R3 R1 R2 R3 R1 R2 R3P1 3 2 2 P1 1 0 0 P1 2 2 2

P2 6 1 3 P2 6 1 2 P2 0 0 1

P3 3 1 4 P3 2 1 1 P3 1 0 3P4 4 2 2 P4 0 0 2 P4 4 2 0

Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R39 3 6 0 1 1Resource vector R Available vector V

Is this a safe state?

Page 22: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of banker’s algorithm

R1 R2 R3 R1 R2 R3 R1 R2 R3P1 3 2 2 P1 1 0 0 P1 2 2 2

P2 0 0 0 P2 0 0 0 P2 0 0 0P3 3 1 4 P3 2 1 1 P3 1 0 3

P4 4 2 2 P4 0 0 2 P4 4 2 0Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R39 3 6 6 2 3Resource vector R Available vector V

p2 runs to completion

Page 23: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of banker’s algorithm

R1 R2 R3 R1 R2 R3 R1 R2 R3P1 0 0 0 P1 0 0 0 P1 0 0 0P2 0 0 0 P2 0 0 0 P2 0 0 0P3 3 1 4 P3 2 1 1 P3 1 0 3P4 4 2 2 P4 0 0 2 P4 4 2 0

Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R39 3 6 7 2 3Resource vector R Available vector V

p1 runs to completion

Page 24: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of banker’s algorithm

R1 R2 R3 R1 R2 R3 R1 R2 R3P1 0 0 0 P1 0 0 0 P1 0 0 0P2 0 0 0 P2 0 0 0 P2 0 0 0P3 0 0 0 P3 0 0 0 P3 0 0 0P4 4 2 2 P4 0 0 2 P4 4 2 0

Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R39 3 6 9 3 4Resource vector R Available vector V

p3 runs to completion, p4 can also complete

Thus it is a safe state

Page 25: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of banker’s algorithm

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 3 2 2 P1 1 0 0 P1 2 2 2

P2 6 1 3 P2 5 1 1 P2 1 0 2

P3 3 1 4 P3 2 1 1 P3 1 0 3

P4 4 2 2 P4 0 0 2 P4 4 2 0

Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R3

9 3 6 1 1 2

Resource vector R Available vector V

Now suppose p1 requests 1 unit each of R1 and R3, should wegrant the request?

Page 26: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of banker’s algorithm

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 3 2 2 P1 2 0 1 P1 1 2 1

P2 6 1 3 P2 5 1 1 P2 1 0 2

P3 3 1 4 P3 2 1 1 P3 1 0 3

P4 4 2 2 P4 0 0 2 P4 4 2 0

Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R3

9 3 6 0 1 1

Resource vector R Available vector V

Suppose the request is granted, is this state safe?

Page 27: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of banker’s algorithm

R1 R2 R3 R1 R2 R3 R1 R2 R3

P1 3 2 2 P1 2 0 1 P1 1 2 1

P2 6 1 3 P2 5 1 1 P2 1 0 2

P3 3 1 4 P3 2 1 1 P3 1 0 3

P4 4 2 2 P4 0 0 2 P4 4 2 0

Claim matrix C Allocation matrix A C – A

R1 R2 R3 R1 R2 R3

9 3 6 0 1 1

Resource vector R Available vector V

We cannot find a successful execution sequence to finish allprocesses, thus the state is unsafe and we should deny the requestfrom p1.

Page 28: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Some comments about deadlock avoidance

1 An unsafe state is NOT a deadlock state, it merely has thepotential to deadlock. Thus the strategies presented areconservative.

2 Processes are assumed to be independent.3 The maximum resource requirement must be known in

advance; there must be fixed number of resources; therequirement from each process must be stated in advance.These requirements are too restrictive in real applications.

Page 29: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Deadlock detection

Deadlock detection: always grant resources requests, the OSperiodically performs check to detect if there is deadlock and takesactions in case of yes.

Q: a matrix denotes the process requests for each type of resource,e.g., Qij represents the request by process i for resource j .A possible detection algorithm:

1 Mark each process that has all 0 in A.2 Let W = V .3 Find unmarked process pi such that Qij Æ Wj , for all j . If no

such process can be found, then there is a deadlock.4 If found, mark pi , let Wk = Wk + Aik for all k, then goto 3

Page 30: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of the detection algorithm

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

P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1

P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector

P3 0 0 0 0 1 P3 0 0 0 1 0

P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5

Request matrix Q Allocation matrix A 0 0 0 0 1

Available vector

Mark p4 (since A4j = 0, for all j).

Page 31: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of the detection algorithm

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

P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1

P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector

P3 0 0 0 0 1 P3 0 0 0 1 0

P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5

Request matrix Q Allocation matrix A 0 0 0 0 1

Available vector

W = (0, 0, 0, 0, 1).

Page 32: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of the detection algorithm

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

P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1

P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector

P3 0 0 0 0 1 P3 0 0 0 1 0

P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5

Request matrix Q Allocation matrix A 0 0 0 0 1

Available vector

Mark p3 (since Q3j Æ Wj , for all j).W = W + (0, 0, 0, 1, 0) = (0, 0, 0, 1, 1).

Page 33: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

Example run of the detection algorithm

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

P1 0 1 0 0 1 P1 1 0 1 1 0 2 1 1 2 1

P2 0 0 1 0 1 P2 1 1 0 0 0 Resource vector

P3 0 0 0 0 1 P3 0 0 0 1 0

P4 1 0 1 0 1 P4 0 0 0 0 0 R1 R2 R3 R4 R5

Request matrix Q Allocation matrix A 0 0 0 0 1

Available vector

Terminate (since no one else can be marked), deadlock detected!

Page 34: What is deadlock? - web.cse.msstate.eduweb.cse.msstate.edu/~ioana/Courses/CSE6733/SLIDES/6_deadlock.p… · What is deadlock? • Deadlock is the permanent blocking of a set of processes

What to do if deadlock is detected?

1 Abort all deadlocked processes.2 Restart (may deadlock again).3 Successively abort deadlocked processes (can be based on

process priority).4 Successively preempt resources (can be based on priority).