Professional Documents
Culture Documents
Deadlock
•Examples: Traffic Jam :
•Dining Philosophers
•Device allocation
–process 1 requests tape drive 1 & gets it
–process 2 requests tape drive 2 & gets it
–process 1 requests tape drive 2 but is blocked
–process 2 requests tape drive 1 but is blocked
1
Operating Systems: Deadlock
•Resource:
–used by a single process at a single point in time
• Pre-emptible:
–can be taken away from a process without ill effect
•Non-Pre-emptible:
–cannot be taken away without problems
–deadlock possible
3
Operating Systems: Deadlock
•Hold and wait : processes currently holding resources granted earlier can
request new resources
4
Operating Systems: Deadlock
resource allocated :
5
Operating Systems: Deadlock
6
Operating Systems: Deadlock
7
Operating Systems: Deadlock
•Deadlock :
8
Operating Systems: Deadlock
Possible Strategies :
9
Operating Systems: Deadlock
•Deadlock Prevention
–negate one of the necessary conditions
•negating Mutual Exclusion :
–example: shared use of a printer
»give exclusive use of the printer to each user in turn wanting to print?
»deadlock possibility if exclusive access to another resource also allowed
»better to have a spooler process to which print jobs are sent
complete output file must be generated first
–example: file system actions
»give a process exclusive access rights to a file directory
»example: moving a file from one directory to another
possible deadlock if allowed exclusive access to two directories simultaneously
should write code so as only to need to access one directory at a time
–solution?
»make resources concurrently sharable wherever possible e.g. read-only access
»most resources inherently not sharable!
10
Operating Systems: Deadlock
11
Operating Systems: Deadlock
12
Operating Systems: Deadlock
–example: a process which copies a file from tape to disc, sorts the file on
disc, and then prints the results on a printer
»could request all three resources at the start
wasteful of printer first, then tape drive later
»could initially request tape and disc together, do the read and sort, then
release both and finally request disc and printer together to do the printing
efficient
more
ensure data stays intact on disc between phases
must
14
Operating Systems: Deadlock
•negating Non-Pre-emption
–difficult to achieve in practice
»cannot take a printer away from a process in the middle of printing
»cannot take a semaphore away from a process arbitrarily
might be in the middle of updating a shared area
»cannot take open streams, pipes and sockets away
process
would need to be written very carefully, probably using signals
undesirable if possible at all
very
–occasionally possible :
»processes resident in main memory
»some deadlock occurs such as failure to allocate a page
»one or more processes can be swapped out to disc to release their pages
and allow remaining processes to continue
aslong as they release any other resources they also hold on the way out
»put back on the scheduling queues to be re-admitted to memory later
15
Operating Systems: Deadlock
–example:
»1: tape drive
»2: disc drive
»3: printer
»4: plotter
»5: typesetter
–example: semaphores
»semaphores identified by
number claimed in numerical order
16
Operating Systems: Deadlock
•Deadlock Avoidance
–deadlock possible but avoided by careful allocation of resources
–avoid entering unsafe states
17
Operating Systems: Deadlock
18
Operating Systems: Deadlock
–when the future request is actually made, convert this to an allocation arc
•Single resource
–at each request, consider whether granting will lead to an unsafe state - if so,
deny
•Multiple resources
–m types of resource, n processes
–vector comparison :
»A B if Ai Bi for 0 i m
21
Operating Systems: Deadlock
C R
–processes rarely know in advance how many resources they will need
–processes may only get their resources after an arbitrarily long delay
23
Operating Systems: Deadlock
24
Operating Systems: Deadlock
25
Operating Systems: Deadlock
26
Operating Systems: Deadlock
–two-hops :
node(i) -> node(j) :
node(i) -> node(1) and node(1) -> node(j)
or node(i) -> node(2) and node(2) -> node(j)
or node(i) -> node(3) and node(3) -> node(j)
.....
or node(i) -> node(N) and node(N) -> node(j)
27
Operating Systems: Deadlock
–2 hops :
0 1 0 0 0 1 0 0
0 0 1 1 0 0 1 1
1 0 0 1 * 1 0 0 1
0 0 0 0 0 0 0 0
–3 hops :
0 0 1 1 0 1 0 0
1 0 0 1 0 0 1 1
0 1 0 0 * 1 0 0 1
0 0 0 0 0 0 0 0
28
Operating Systems: Deadlock
29
Operating Systems: Deadlock
30
Operating Systems: Deadlock
•Multiple resources
–apply equivalent of Banker’s algorithm using current resource requests
–any processes unsatisfied are deadlocked
31
Operating Systems: Deadlock
•Process Termination
–drastic ultimate solution
–abort all processes involved in the deadlock
»all resources returned for re-use