Introduction: - In a multi programming environment, several processes may compete for a finite number of resources. A process requests resources and if the resources are not available at that time, the process enters a waiting state. Sometimes, a waiting process is never again able to change state because the resources it has requested are held by other waiting processes. This situation is called a dead lock.
System Model
A system consists of a finite number of resources to be distributed among a number of competing processes. The resources are partitioned into several types, each consisting of some number of identical instances. Memory space, CPU cycles, files and I/O devices are examples of resource types.
If a process requests an instance of a resource type, the allocation of any instance of the type will satisfy the request. If it will not, then the instances are not identical and the resource type classes have not been defined properly.
A process must request a resource before using it and must release the resource after using it. A process may request as many resources as it requires for carrying out its designated task.
Under the normal mode of operation, a process may utilize a resource in the following sequence:
1. Request ? requesting process must wait until it can acquire the resource
2. Use ? process can operate on the resource
3. Release ? process releases the resource
The request and release of resources are system calls. Examples are the request () and release () device, open () and close () file and allocate () and free () memory system calls. Request and release of resources that are not managed by the OS can be accomplished through the wait () and signal () operations on semaphores or through acquisition () and release of mutex lock. A system table records whether each resource is free or allocated; for each resource that is allocated, the table also records the process to which it is allocated. If a process requests a resource that is currently allocated to another process, it can be added to a queue of processes waiting for this resource.
A set of processes is in a deadlock state when every process in the set is waiting for an event that can be caused only by another process in the set. The events mainly concerned here are resource acquisition and release. The resources may be either physical resources or logical resources.
A programmer developing multithreaded applications must pay particular attention to deadlocks. Multi threaded programs are good candidates for deadlock because multiple threads can compete for shared resources.
Deadlock Prevention
There are two approaches to deadlock prevention. One approach ensures that no cyclic waits can occur by ordering the requests for locks, or requiring all locks to be acquired together. The other approach is closer to deadlock recovery, and performs transaction rollback instead of waiting for a lock, whenever the wait could potentially result in a deadlock.
The simplest scheme under the first approach requires that each transaction locks all its data items before it begins execution. Moreover, either all are locked in one step or none are locked. There are two main disadvantages to this protocol:-
(1) it is often hard to predict, before the transaction begins, what data items need to be locked; (2) data-item utilization may be very low, since many of the data items may be locked but unused for a long time.
Another approach for preventing deadlocks is to impose an ordering of all data items, and to require that a transaction lock data items only in a sequence consistent with the ordering. A variation of this approach is to use a total order of data items, in conjunction with two-phase locking. Once a transaction has locked a particular item, it cannot request locks on items that precede that item in the ordering. This scheme is easy to implement, as long as the set of data items accessed by a transaction is known when the transaction starts execution. There is no need to change the underlying concurrency-control system if two-phase locking is used: All that is needed it to ensure that locks are requested in the right order.
The second approach for preventing deadlocks is to use preemption and transaction rollbacks. In preemption, when a transaction T 2 requests a lock that transaction T 1 holds, the lock granted to T 1 may be preempted by rolling back of T 1 and granting of the lock to T 2 . To control the preemption, we assign a unique timestamp to each transaction. The system uses these time stamps only to decide whether a transaction should wait or roll back. Locking is still used for concurrency control.