Sie sind auf Seite 1von 6

“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
events that could wake up any of the other members of the set, and all the
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 C

How deadlock occur

OS003 – Assignment #1 (Finals)


(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 for a resource-allocation system with multiple instances of each
resource type

• Each process must declare a priori claim maximum use

• When a process requests a resource it may have to wait even if 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

• Available: Vector of length m. If Available[j] = k, there are k instances of


resource type Rj available.

• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k


instances of resource type Rj.

• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k


instances of Rj.

• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj


to complete its task.

• Need [i,j] = Max[i,j] – Allocation [i,j].

Example of Banker’s algorithm:


• 5 processes P0 through P4; 3 resource types A (10 instances), B (5 instances)
and C (7 instances).
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 7 5 3 33 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3

Safe State
• A state is safe if the system can allocate 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 <P1, P2, …, Pn> 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<I.
– 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 a process requests an available resource, system must decide 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 è no deadlocks.
• If a system is in unsafe state è possibility of deadlock.
• Avoidance è ensure that a system will never enter an unsafe state.

* “Deadlock” Modern Operating System 2nd Edition, Andrew S. Tanenbaum


* “Deadlock Modeling” http://ls4-www.informatik.uni-dortmund.de/RVS/V-BSRVS/Chapter-03.ppt.
* "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)

Das könnte Ihnen auch gefallen