Operating Systems: Chapter 3: Deadlocks
Operating Systems: Chapter 3: Deadlocks
Operating Systems: Chapter 3: Deadlocks
Chapter 3: Deadlocks
Instr: Yusuf Altunel IKU Department of Computer Engineering (212) 498 42 10 [email protected]
1
Content
3.1.Sharing Resources
3.2. Introduction to deadlocks 3.3. The ostrich algorithm
Sharing Resources
Computer systems have resources
shared between processes can only be used by one process at a time
printers, tape drives, slots in the systems internal tables
Deadlock
Two processes each want to record
a scanned document on a CD.
Both processes are blocked and will remain so forever. This situation is called a deadlock.
4
Resources
a resource is
anything that can be used
by only a single process at any instant of time
Types of resources:
hardware device
e.g., a tape drive, printers
piece of information
e.g., a record in a database
A computer has many different resources some resources have several identical instances
such as three tape drives. any one of them can be used
to satisfy requests for the resource.
Resources
Preemptable Resources
can be taken away from the process owning it
with no ill effects.
Nonpreemptable Resources
cannot be taken away from its current owner
without causing the computation to fail
Using a Resource
Sequence of events required to use a resource:
request the resource use the resource release the resource
Introduction to Deadlocks
Formal definition :
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 release of a currently held resource
2.
3.
No preemption condition.
Resources previously granted
cannot be forcibly taken away from a process. They must be explicitly released by the process holding them.
4.
Deadlock Modeling
Using directed graphs
(a) Holding a resource by a process (b) Requesting a resource (c) Deadlock (C-T-D-U-C)
An arc from a resource to a process The resource is held by that process
An arc from a process to a resource The process is currently blocked waiting the resource to be available
A cycle in the graph means a deadlock
10
11
12
just ignore the problem altogether detection and recovery dynamic avoidance
4.
prevention
negating one of the four necessary conditions
13
Reasonable if
deadlocks occur very rarely cost of prevention is high
Instead
lets them occur tries to detect when this happens and then takes some action to recover
15
Condition:
There is only one resource from each type
Example:
1. 2. 3. 4. 5. 6. 7. Process Process Process Process Process Process Process A holds R and wants S. B holds nothing but wants T. C holds nothing but wants S. D holds U and wants S and T. E holds T and wants V. F holds W and wants S. C holds V and wants U.
16
Example
Note the resource ownership and requests A cycle can be found within the graph, denoting deadlock
17
Algorithm
1. For each node, N in the graph,
perform the following 5 steps
with N as the starting node.
If it does, the graph contains a cycle (listed in L) and the algorithm terminates.
18
Algorithm
5. Pick an unmarked outgoing arc at random and mark it
Then follow it to the new current node and go to step 3.
19
20
R1
R2
R3
R4
R1
R2
R3
R4
P1 P2 P3
R1 R2 R3 R4
R1 R2 R3 R4
22
Invariants
Every resource is either allocated or available If we add up all allocated resources j (Example: Printer)
To the available resources The result is the number of instances for the resource class
C
i 1
ij
Aj Ej
23
24
It is then marked as completed. If all the processes are ultimately able to run
none of them are deadlocked
If some of them can never run, they are deadlocked. the algorithm is nondeterministic
because it may run the processes in any feasible order,
25
Process 2 has two tape drives (4-2=2) and a CD-ROM drive (1-1=0). Process 3 has a plotter (2-1=1) and two scanners (1-1=0).
A=(2, 1 , 0, 0)
A=(2, 2 , 1, 0)
26
The second cannot be satisfied either, because there is no scanner free. the third one can be satisfied, so process 3 runs: A=(0, 0 , 0, 0) After execution eventually returns all its resources
A = (2 2 2 0)
Now the remaining process can run. There is no deadlock in the system.
27
DDA Structures
P1
P2 P3
28
An alternative strategy is
to check every k minutes,
or perhaps only when the CPU utilization has dropped below some threshold.
29
30
31
Deadlock Avoidance
The system must be able
to decide whether
granting a resource is safe or not only make the allocation when it is safe.
Question:
Is there an algorithm
can always avoid deadlock by making the right choice all the time?
Unsafe state:
Guarantee to run to completion Even if the process request its maximum number of resources immediately
Dealing with a group of customers Check to see if granting credit request is a safe state
33
Only 10 units are (b) Select B and apply reserved! (c) B completes and frees the States of processes: resources A,B,C (a) A has 3 instances of (d) Select C and apply the resources
B has 2 but need 4 C has 2 but need 7
May need as much as 9
34
(b) A request and gets another resource (c) Select B and apply (d) B completes and frees the resources Only 4 free resources and both processes wants 5
35
Four customers A,B,C,& D Total reserved amount is 10 units (a) the reserved amounts for individual customers (b) after using some credits (safe condition) (c) B granted 1 more units of credits (unsafe condition)
37
Deadlock Prevention
1.
2. 3.
No preemption condition.
Resources previously granted
cannot be forcibly taken away from a process. They must be explicitly released by the process holding them.
4.
Principle:
avoid assigning resource when not absolutely necessary as few processes as possible actually claim the resource
39
Problems
may not know required resources at start of run also ties up resources other processes could be using
Variation:
process must give up all resources then request all immediately needed
40
No Preemption Condition
Attacking the third condition (no preemption)
is even less promising than attacking the second one.
but all requests must be made in numerical order. first a printer and then a tape drive, but it may not request first a plotter and then a printer. it may be impossible to find an ordering satisfies everyone.
44
Two-Phase Locking
Useful in updating
large number of records in a table
Phase One
process tries to lock all records it needs, one at a time if needed record was locked, start over (no real work done in phase one)
Note: similarity to requesting all resources at once Algorithm works where programmer can arrange
To start program stop and restart later
45
End of Chapter 3
Deadllocks
46