Lessson 5 6
Lessson 5 6
College of Science
Computer Dept.
Higher Diploma
By
Aree Ali M.
2007
Deadlock I
Deadlock Problem
A set of blocked processes each holding a resource and waiting
to acquire a resource held by another process in the set.
Example
– System has 2 disk drives.
– P1 and P2 each hold one disk drive and each needs another
one.
– P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.
– R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
request edge – directed edge P1 Rj Pi requests instance of Rj
assignment edge – directed edge Rj Pi
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Resource-Allocation Graph
Example Graph
Graph With
With A
A Deadlock
Deadlock
Graph
Graph With
With A
A Cycle
Cycle
But
But No
No Deadlock
Deadlock
Basic Facts
Mutual Exclusion – not required for sharable resources; must hold for nonsharable
resources.
Hold and Wait – must guarantee that whenever a process requests a resource, it
does not hold any other resources.
– Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has
none.
– Low resource utilization; starvation possible.
No Preemption –
– If a process that is holding some resources requests another resource that
cannot be immediately allocated to it, then all resources currently being held
are released.
– Preempted resources are added to the list of resources for which the process
is waiting.
– Process will be restarted only when it can regain its old resources, as well as
the new ones that it is requesting.
Circular Wait – impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration.
Deadlock Avoidance
System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
processes is the systems such that for each P i, the resources that Pi can still
request can be satisfied by currently available resources + resources held by all the
Pj, with j < i.
That is:
– If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished.
– When Pj is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate.
– When Pi terminates, Pi +1 can obtain its needed resources, and so on.
If a system is in safe state no deadlocks.
Request = request vector for process Pi. If Requesti [j] = k then process Pi
wants k instances of resource type Rj.
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi must wait, since
resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe the resources are allocated to Pi.
If unsafe Pi must wait, and the old resource-allocation state is
restored
Example of Banker’s Algorithm
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria.
Example: P1 Request (1,0,2)
An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph
Resource-Allocation Graph and Wait-for
Graph