Saturday, April 7, 2012

Deadlock prevention




Deadlock prevention

Removing the mutual exclusion condition means that no process will have exclusive(not shared) access to a resource
Algorithms that avoid mutual exclusion are called non-blocking synchronization algorithms.


The hold and wait or resource holding conditions may be removed by requiring processes to request all the resources they will need before starting up
Another way is to require processes to request resources only when it has none.
(These algorithms, such as serializing tokens, are known as the all-or-none algorithms.


The no preemption condition may also be difficult or impossible to avoid as a process has to be able to have a resource for a certain amount of time,
Algorithms that allow preemption include lock-free and wait-free algorithms and optimistic concurrency control.


Approaches that avoid circular waits include disabling interrupts during critical sections and using a hierarchy to determine a partial ordering of resources.
The Dijkstra's solution can also be used.


http://en.wikipedia.org/wiki/Deadlock#Distributed_deadlock_prevention

















all four of the conditions are necessary for deadlock to occur, it follows that deadlock might be prevented by denying any one of the conditions.

Elimination of “Mutual Exclusion” Condition
several processes cannot simultaneously share a single resource
This condition is difficult to eliminate because some resources, such as the tap drive and printer, are inherently non-shareable

Elimination of “Hold and Wait” Condition
The first alternative is that a process request be granted all of the resources it needs at once, prior to execution.
The second alternative is to disallow a process from requesting resources whenever it has previously allocated resources.

Elimination of “No-preemption” Condition
When a process release resources the process may lose all its work to that point. One serious consequence of this strategy is the possibility of indefinite postponement (starvation). A process might be held off indefinitely as it repeatedly requests and releases the same resources.

Elimination of “Circular Wait” Condition
The last condition, the circular wait, can be denied by imposing a total ordering on all of the resource types and than forcing, all processes to request the resources in order (increasing or decreasing)


http://www.personal.kent.edu/~rmuhamma/OpSystems/Myos/deadlockPrevent.htm










Preventing Deadlock (by denying one of the conditions)

Denying Mutual Exclusion

Not desirable to deny - we want dedicated resources

Denying Hold-And-Wait

Force processes to request and gain all their resources in one go.

Disadvantages are other processes are made to wait.

A resource which will not be used until the end of a process still has to be requested at the beginning - poor resource utilisation

Denying No Pre-emption

Some non-shareable resources can be pre-empted if the hardware/software saves the context of the interrupted process - but what about printers?

Only partial denial of no pre-emption is possible.

Denying Circular Waiting

Resources are grouped into `Frequency of Use' classes, C1, C2, C3, etc.

All required resources belonging to the same class are requested at the same time in class order

No circular wait because either all required resources one class are held, or process waits until all required resources within a class are available.

Disadvantages are that resources must be requested in an artifical order - not necessarily the order in which they will be used.

http://staff.um.edu.mt/csta1/courses/lectures/csm202/os7.html#2

Ostrich algorithm

Solutions to deadlock

There are several ways to address the problem of deadlock in an operating system.

Just ignore it and hope it doesn't happen
Detection and recovery - if it happens, take action
Dynamic avoidance by careful resource allocation. Check to see if a resource can be granted, and if granting it will cause deadlock, don't grant it.
Prevention - change the rules


Ignore deadlock

The text refers to this as the Ostrich Algorithm. Just hope that deadlock doesn't happen. In general, this is a reasonable strategy. Deadlock is unlikely to occur very often; a system can run for years without deadlock occurring. If the operating system has a deadlock prevention or detection system in place, this will have a negative impact on performance (slow the system down) because whenever a process or thread requests a resource, the system will have to check whether granting this request could cause a potential deadlock situation.




http://www.cs.rpi.edu/academics/courses/fall04/os/c10/







Ostrich algorithm


n computer science, the ostrich algorithm is a strategy of ignoring potential problems on the basis that they may be exceedingly rare - "to stick your head in the sand and pretend that there is no problem". This assumes that it is more cost-effective to allow the problem to occur than to attempt its prevention.

This approach may be used in dealing with deadlocks in concurrent programming if deadlocks are believed to be very rare, and if the cost of detection or prevention is high


http://en.wikipedia.org/wiki/Ostrich_algorithm

Necessary conditions for deadlock

Conditions for Deadlock

Mutual exclusion: resources cannot be shared.
Hold and wait: processes request resources incrementally, and hold on to what they've got.
No preemption: resources cannot be forcibly taken from processes.
Circular wait: circular chain of waiting, in which each process is waiting for a resource held by the next process in the chain.

http://www1bpt.bridgeport.edu/sed/projects/cs503/Spring_2001/kode/os/deadlock.htm#conditions






four (4) conditions that must hold simultaneously for there to be a deadlock.

1. Mutual Exclusion Condition
The resources involved are non-shareable.
Explanation: At least one resource (thread) must be held in a non-shareable mode, that is, only one process at a time claims exclusive control of the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released.

2. Hold and Wait Condition
Requesting process hold already, resources while waiting for requested resources.
Explanation: There must exist a process that is holding a resource already allocated to it while waiting for additional resource that are currently being held by other processes.

3. No-Preemptive Condition
Resources already allocated to a process cannot be preempted.
Explanation: Resources cannot be removed from the processes are used to completion or released voluntarily by the process holding it.

4. Circular Wait Condition
The processes in the system form a circular list or chain where each process in the list is waiting for a resource held by the next process in the list.

http://www.personal.kent.edu/~rmuhamma/OpSystems/Myos/deadlockCondition.htm







Necessary conditions for deadlock

A deadlock situation can arise if and only if the following four conditions hold simultaneously in a system-

Mutual Exclusion: At least one resource is held in a non-sharable mode that is only one process at a time can use the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released.

Hold and Wait:There must exist a process that is holding at least one resource and is waiting to acquire additional resources that are currently being held by other processes.

No Preemption: Resouces cannot be preempted; that is, a resource can only be released voluntarily by the process holding it, after the process has completed its task.

Circular Wait: There must exist a set {p0, p1,.....pn} of waiting processes such that p0 is waiting for a resource which is held by p1, p1 is waiting for a resource which is held by p2,..., pn-1 is waiting for a resource which is held by pn and pn is waiting for a resource which is held by p0.


http://wikieducator.org/Necessary_conditions_for_deadlock

The Bounded-Buffer semaphores

The Bounded-Buffer semaphores

We assume that the pool consists of $ n$ buffers, each capable of holding one item. The mutex semaphore provides mutual exclusion for accesses to the buffer pool and is initialized to the value 1.

The empty and full semaphores count the number of empty and full buffers.

The semaphore empty is initialized to the value n.
The semaphore full is initialized to the value 0.



http://siber.cankaya.edu.tr/ozdogan/OperatingSystems/ceng328/node147.html#6.8