“Deadlock Management” Deadlock - 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. Because all the processes are waiting, none of them will ever cause any of the even events ts that that coul could d wake wake up any any of the the othe otherr memb member ers s of the the set, set, and and all all the the processes processes continue to wait forever. For this model, we assume that processes have only a single thread and that there are no interrupts possible to wake up a blocked process. The no-interrupts condition is needed to prevent an otherwise deadlocked process from being awakened by, say, an alarm, and then causing events that release other processes in the set. In most cases, the event that each process is waiting for is the release of some resource currently possessed by another member of the set. In other words, each member of the set of deadlocked processes is waiting for a resource that is owned by a deadlocked process. None of the processes can run, none of them can release any resources, and none of them can be awakened. The number of processes and the number and kind of resources possessed and requested are unimportant. This result holds for any kind of resource, including both hardware and software.
Preemptable resource A preemptable resource is one that can be taken away from the process with no ill effects. Memory is an example of a preemptable resource. Example: process swapping form main memory
Non-preemtable resource A nonpreemptable resource, in contrast, is one that cannot be taken away from its current owner without causing the computation to fail (printer, floppy disk). If a process has begun to burn a CD-ROM, suddenly taking the CD recorder away from it and giving it to another process will result in a garbled CD. Example: process swapping form main memory
OS003 – Assignment #1 (Finals)
Deadlock Modeling •
Modeled with directed graphs
–
resource
R
assigned
to
process A –
process B is requesting/waiting for resource S
–
process C and D are in deadlock over resources T and U
A
B
How deadlock occur
OS003 – Assignment #1 (Finals)
C
(4)
(5)
(6)
How Deadlock can be avoided Methods for Handling Deadlock
Just ignore the problem altogether
Prevention Ensure that the system will never enter a deadlock state Requires negating one of the four necessary conditions
Dynamic avoidance Require careful resource allocation
Detection and recovery Allow the system to enter a deadlock state and then recover We need some methods to determine whether or not the system has entered into deadlock. We also need algorithms to recover from the deadlock.
Banker’s Algorithm •
Work Work for for a reso resourc urce-a e-allo llocat cation ion system system with with multi multiple ple insta instance nces s of each each resource type
•
Each process must declare a priori claim maximum use
•
When When a proc proces ess s requ reques ests ts a reso resour urce ce it may may have have to wait wait even even if the the requested resources are available
•
When a process gets all its resources it must return them in a finite amount of time
Data Structures for the Banker’s Algorithm OS003 – Assignment #1 (Finals)
•
n = number of processes, m = number of resources types
•
Availab Available: le: Vector Vector of length m. If Available Available[j] [j] = k, there there are k instances instances of resource type Rj available.
•
Max: n x m matrix. matrix. If Max [i,j] [i,j] = k, then proce process ss Pi may request request at most most k instances of resource type Rj.
•
Allocation: Allocation: n x m matrix. matrix. If Allocation[i,j] Allocation[i,j] = k then Pi Pi is currently currently allocated allocated k instances of Rj.
•
Need: n x m matrix. If Need[i,j] Need[i,j] = k, then Pi may need k more instances instances of Rj to complete its task. •
Need [i,j] = Max[i,j] – Allocation [i,j].
Example of Banker’s algorithm: •
5 processes P 0 through P 4; 3 resource resource types A (10 instances), B (5 instances) and C (7 instances).
•
Snapshot at time T 0: Allocation
Max
Available
ABC
ABC
ABC
0 1 0
7 5 3
33 2
P 1
2 0 0
3 2 2
P 2
3 0 2
9 0 2
P 3
2 1 1
2 2 2
P 4
0 0 2
4 3 3
P 0
Safe State •
A state is safe if the system can allocate resources resources to each process (up to its maximum) in some order and still avoid a deadlock.
•
System is in safe state only if there is a safe sequence of all processes.
OS003 – Assignment #1 (Finals)
•
Sequence
is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j
If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished.
–
When Pj-1 is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.
–
•
When Pi terminates, Pi+1 can obtain its needed resources, etc.
When When a proce process ss reque request sts s an avail availabl able e resou resourc rce, e, syste system m must must decid decide e if immediate allocation leaves the system in a safe state.
Demonstration that the state in (a) is safe
Unsafe State
Unsafe state Is a state that is not safe
OS003 – Assignment #1 (Finals)
Demonstration that the state in (b) is not safe Basic Facts: •
If a system is in safe state
•
If a system is in unsafe state
•
Avoidance
è
è
no deadlocks. è
possibility of deadlock.
ensure that a system will never enter an unsafe state.
* “Deadlock” Modern Operating System 2 nd Edition, Andrew S. Tanenbaum http://ls4-www.informatik.uni-dortmund.de/RVS/ -dortmund.de/RVS/V-BSRVS/C V-BSRVS/Chapter-03.ppt hapter-03.ppt . * “Deadlock Modeling” http://ls4-www.informatik.uni * "Bankers Algorithm" http://www.cc.nctu.edu.tw/~claven/course/os/ch8.ppt * "Safe and Unsafe State" http://www.cc.nctu.edu.tw/~claven/course/os/ch8.ppt
OS003 – Assignment #1 (Finals)