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:
Implementation

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:
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:

Semaphores can be used to implement the waiting in the hierarchical algorithm.

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:
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 Given this sequence of requests and frees: which of the above REQUESTS will be REFUSED by the hierarchical algorithm?

Answer:


Return to Contents
back