june 11, 2002serguei a. mokhov, [email protected] 1 deadlock comp346 - operating systems...
TRANSCRIPT
![Page 1: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/1.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
1
Deadlock
COMP346 - Operating Systems
Tutorial 5
Edition 1.1, June 15, 2002
![Page 2: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/2.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
2
Topics• Deadlock Debrief
– Simplest Example– Necessary Deadlock Conditions– Resource Allocation Graph– Deadlock Handling
• Deadlock and Starvation with Semaphores
• Examples
![Page 3: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/3.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
3
Deadlock
• Simplest example:– Two processes require two resources to
complete (and release the resources)– There are only two instances of these resources– If they acquire one resource each, they block
indefinitely waiting for each other to release the other resource =>
• DEADLOCK, one of the biggest problems in multiprogramming.
R1
P1 P2
R2
![Page 4: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/4.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
4
Necessary Deadlock Conditions
• Recall which conditions must hold (p. 245):– ME: at least one process exclusively uses a
resource– Hold and wait: a process possesses at least one
resources and requires more, which are held by others
– No preemption: resources are released only in voluntary manner by processes holding them
– Circular wait: P1P2 P3 … PN P1
![Page 5: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/5.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
5
Resource-Allocation Graph
• An easy way to illustrate resource allocation and visually detect the deadlock situation(s)
• Example:– N+1 resources
– N processes
– Every process needs 2 resources
– Upon acquiring 2 resources, a process releases them
– Is there a deadlock?
• • • •
P1 P2 P3?
![Page 6: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/6.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
6
More Examples
• Example from Salsa Theory Review
• Resource Allocation Graph
• Is the following true or false?– Deadlock cannot happen in a system with two
processes.• One CPU
• Two CPUs
![Page 7: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/7.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
7
Handling Deadlocks
• Deadlock Ignorance
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection and Recovery
![Page 8: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/8.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
8
Deadlock Ignorance
• As system designers we may pretend that deadlocks don’t happen :-).
• If they do happen, they don’t happen very often.• Most common approach because it’s cheap (in
terms of human labor, complexity of the system and system’s performance).
• Highest resource utilization.• Sysadmin can simply kill deadlocked processes or
restart the system if it’s not responding.
![Page 9: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/9.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
9
Deadlock Prevention
• Recall necessarily deadlock conditions• Deadlock prevention algorithms assure at
least one these conditions do not hold, thus preventing occurrence of the deadlock.
• Possible Disadvantages:– Low device utilization– Reduced system’s throughput
• [1] p. 250
![Page 10: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/10.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
10
Deadlock Avoidance
• Unlike in deadlock prevention, deadlock avoidance algorithms do not watch for necessary deadlock conditions, but use additional a priori info about future resource requests.
• This info will help the system to avoid entering the unsafe state.
• Disadvantages: again, lower resource utilization (because resources may not be granted sometimes even if they are unused).
• [1] P. 253
![Page 11: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/11.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
11
Deadlock Detection and Recovery
• Instead of preventing or avoiding deadlocks we allow them to happen and also provide mechanisms to recover.
• Problems:– How often do we run detection algorithms?– How do we recover?– What is the cost of detection and recovery?
• [1] P. 260, p. 264
![Page 12: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/12.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
12
Deadlocks and Starvation with Semaphores
• Semaphores act like resources in this case, which can be acquired and (never) released.
• One example as book suggests:
• [1] P. 204
P1
wait(sem1)
wait(sem2)
…
signal(sem1)
signal(sem2)
P2
wait(sem2)
wait(sem1)
…
signal(sem2)
signal(sem1)
![Page 13: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/13.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
13
Deadlocks and Starvation with Semaphores (2)
• Starvation, or indefinite blocking, is when a process is waiting on a semaphore where nobody is ever going to release it.
• Another example of both deadlock and starvation:process A { process B { wait(mutex) wait(mutex) ... ... wait(synch) signal(synch) ... ... wait(synch) ... ... ... signal(mutex) signal (mutex)} }
For this case assume:
mutex = 1 synch = 0
Try starting processesin any order and seewhere they block.
![Page 14: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/14.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
14
Deadlocks and Starvation with Semaphores (3)
• Yet another example :-)a) semaphore S = -2;P1 { P2 { P3 { <phase1> <phase1> <phase1> V(S) V(S) V(S) P(S) P(S) P(S) <phase2> <phase2> <phase2>} } }
b) semaphore S = -2, S1 = 0, S2 = 0;P1 { P2 { P3 { <phase1> <phase1> <phase1> V(S) V(S) V(S) P(S) P(S) P(S) V(S1) P(S1) P(S2) <phase2> V(S2) <phase2>} <phase2> }
}
![Page 15: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/15.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
15
Deadlocks and Starvation with Semaphores (4)
• Things to note:– Assume the P2, P3, P1 order in the starvation example;– Assume the P1, P3, P2 order in the deadlock example;– Blocking means getting out from the running state to
the waiting state, so a context switch to the next process in the queue happens;
– In these example there are no multiple instances of these processes.
![Page 16: June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Deadlock COMP346 - Operating Systems Tutorial 5 Edition 1.1, June 15, 2002](https://reader036.vdocument.in/reader036/viewer/2022082817/56649dc55503460f94ab945c/html5/thumbnails/16.jpg)
June 11, 2002 Serguei A. Mokhov, [email protected]
16
Deadlocks and Starvation with Semaphores (5)
Starvation scenario (in b):
1) P2: <phase1>
2) V(S) (S = -1)
3) P(S) (blocked->switch to P3)
4) P3: <phase1>
5) V(S) (S = 0)
6) P(S) (blocked->switch to P1)
7) P1: <phase1>
8) V(S) (S = 1)
9) P(S) (S = 0)
10) V(S1) (S1 = 1)
11) <phase2>
12) terminates
13) ???
Deadlock Scenario:
1) P1: <phase1>
2) V(S) (S = -1)
3) P(S) (blocked->switch to P3)
4) P3: <phase1>
5) V(S) (S = 0)
6) P(S) (blocked->switch to P2)
7) P2: <phase1>
8) V(S) (S = 1)
9) P(S1) (blocked)
10) ???