Return to Contents
Deadlock Prevention Algorithms
Note: the textbook does not name these algorithms and has very short descriptions of them.
I use a description adapted from
R. A. Finkel, An Operating Systems Vade Mecum,
Prentice-Hall, 1988.
One-shot Algorithm
Given a request from process P for resources R1, R2, ..., Rn,
the resource manager follows these rules:
if process P has ever acquired any resources before, then
refuse the request
else if any resource R1, ... Rn does not exist then
refuse the request
else
{
if any resource R1, ... Rn is not free, then
wait until all resources R1, ... Rn are free
end if
grant process P exclusive access to resources R1, ... Rn
}
end if
Silberschatz and Galvin, section 7.4.2:
``One protocol ... requires each process to request and be
allocated all its resources before it begins execution'' (p.214)
Simple Example:
- Suppose that a person needs both a knife and fork to eat.
- Person P1 needs knife and fork and person P2 needs knife and fork.
- Person P1 requests knife and fork, person P2 also requests knife and fork.
- Only one of P1 or P2 will be granted the resources. Suppose it is P1. P2 is forced to wait.
- Person P1 uses the knife and fork until finished.
- Person P1 releases the knife and fork.
- Since the resources that P2 was waiting for are free, P2 is granted both the knife and fork
- Person P2 uses the knife and fork until finished.
- Person Ps releases the knife and fork.
Implementation
- To implement using semaphores,
the system must provide a means of waiting on multiple
resources at the same time.
- Wait succeeds if all resources are granted
- Otherwise, process must continue waiting
- Example: Windows 2000 WaitForMultipleObjects system call
Repeated One-shot (Multishot) Algorithm
Given a request from process P for resources R1, R2, ..., Rn,
the resource manager follows a similar rule to that for one-shot.
if process P currently has any resources, then
refuse the request
else if any resource R1, ... Rn, does not exist, then
refuse the request
else
{
if any resource R1, ... Rn is not free, then
wait until all resources R1, ... Rn are free
end if
grant process P exclusive access to resources R1, ... Rn
}
end if
If a process P wants to request resources while holding
resources, they follow these steps:
-
P frees all resources being held
-
P requests all resources previously held plus the new resources it wants to acquire
Silberschatz and Galvin, section 7.4.2:
``An alternative protocol allows a process to request resources
only when the process has none. A process may
request some resources and use them.
Before it can request any additional resources, however,
it must release all the resources that it
is currently allocated.'' (p.215)
Hierarchical Algorithm
Assume the resources have unique priorities (i.e., all priorities
are different).
Given a request from process P for resource R,
the resource manager follows these rules:
if process P currently has any resources with equal or higher priority
than resources R, then
refuse the request
else if resource R1 does not exist, then
refuse the request
else
{
if the resource R is not free, then
wait until resource R is free
end if
grant process P exclusive access to resources R
}
end if
Simple Example:
- Suppose that a person needs both a knife and fork to eat.
- Knife is given priority 2 and fork priority 1, assuming that zero is the highest priority.
- Person P1 needs knife and fork and person P2 needs knife and fork.
- Person P1 requests knife first because it is lower priority and then fork.
- Person P2 also requests knife and then fork.
- Only one of P1 or P2 will be granted the knife. Suppose it is P1.
- Person P2 will wait for the knife to be free.
- Then only P1 will request the fork.
- The request will be granted.
- Person P1 will use the knife and fork until finished.
- Person P1 will release the knife and fork.
- Since person P2 is waiting for the knife, the request can now be granted.
- Then Person P2 will immediately request the fork and this request
will also be granted.
- Person P2 will use the knife and fork until finished.
- Person P2 will release the knife and fork.
Semaphores can be used to implement the waiting in the
hierarchical algorithm.
-
wait = used for a request
-
signal = used for a free
Hierarchical Algorithm (with queue)
An interesting variation on the Hierarchical Algorithm can
be created by ensuring that the waiting done by the processes
is done on a series of queues, one per resource.
Given a request from process P for resource R,
the resource manager follows these rules:
if process P currently has any resources with equal or higher priority
than resources R, then
refuse the request
else if resource R does not exist, then
refuse the request
else
{
if the resource R is not free, then
put process P in a queue waiting for resource R
and when process P reaches the front of the queue,
grant process P exclusive access to resource R
end if
}
end if
Simple Example:
- Suppose that a person needs both a knife and fork to eat.
- Knife is given priority 2 and fork priority 1, assuming that zero is the highest priority.
- Person P1 needs knife and fork and person P2 needs knife and fork.
- Person P1 requests knife first because it is lower priority and then fork.
- Person P2 also requests knife and then fork.
- Only one of P1 or P2 will be granted the knife. Suppose it is P1.
- Person P2 will be put in the queue for the knife.
- Then only P1 will request the fork.
- The request will be granted.
- Person P1 will use the knife and fork until finished.
- Person P1 will release the knife and fork.
- Since person P2 is at the front of the queue waiting for
the knife; person P2 will be granted the knife.
- Then Person P2 will immediately request the fork and this request
will be granted.
- Person P2 will use the knife and fork until finished.
- Person P2 will release the knife and fork.
Many semaphore implementations use queues.
If this is done, the above algorithm will look
identical to the standard Hierarchical Algorithm.
When a process must wait,
it is placed at the back of the queue for the
particular resource.
When the resource becomes free,
the process at the front of the queue is allowed to succeed at its WAIT,
i.e., leave the wait queue.
Silberschatz and Galvin, section 7.4.4:
``Each process can request resources only in
an increasing order of enumeration.
...
If several instances of the same resource type are needed,
a single request for all of them must be issued.'' (p.216)
Note: Silberschatz and Galvin assign higher numbers
to higher priorities, instead of lower numbers.
The crucial point is that if several lower priority resources
must be obtained before higher priority resources
Sample Question:
In the hierarchical algorithm for preventing deadlock,
each resource type is assigned a unique integer as a priority,
with 2 as lowest priority and 0 as highest priority.
Suppose
-
resource R1 has priority 2,
-
resource R2 has priority 1,
-
resource R3 has priority 0.
Given this sequence of requests and frees:
- P1 requests R1,
- P3 requests R3,
- P1 requests R2,
- P1 frees R2,
- P2 requests R2,
- P2 frees R2,
- P3 requests R2,
- P3 frees R2
which of the above REQUESTS will be REFUSED
by the hierarchical algorithm?
Answer:
- P1 requests R1 ==> P1 is granted R1
- P3 requests R3 ==> P3 is granted R3
- P1 requests R2 ==> P1 is granted R2 because its priority is higher than R1
- P1 frees R2 ==> afterwards, P1 is still holding R1
- P2 requests R2 ==> P2 is granted R2
- P2 frees R2 ==> afterwards, P2 is holding nothing,
- P3 requests R2 ==> refused because P3 has a higher priority resource R3,
- P3 frees R2 ==> no change because its was never granted
Return to Contents
back