• No results found

Methods for Handling Deadlocks

In document INTRODUCTION TO OPERATING SYSTEMS (Page 152-155)

There are three different methods for dealing with the deadlock problem:

• We can use a protocol to ensure that the system will never enter a deadlock state.

• We can allow the system to enter a deadlock state and then recover.

• We can ignore the problem all together, and pretend that deadlocks never occur in the system. This solution is the one used by most operating systems, including UNIX.

Deadlock prevention is a set of methods for ensuring that at least one of the necessary cannot hold. These methods prevent deadlocks by constraining how requests for resources can be made.

Deadlock avoidance requires the operating system to have, in advance, additional information concerning which resources a process will request and use during its lifetime. With this additional knowledge, we can decide for each request whether or not the process should wait. Each request requires that the system considers the resources currently available, the resources currently allocated to each process, and the future requests and releases of each process, to decide whether the current request can be satisfied or must be delayed.

For a deadlock to occur, each of the four necessary-conditions must hold.

1. Mutual Exclusion

The mutual-exclusion condition must hold for non-sharable resources. For example, a printer cannot be simultaneously shared by several processes. Sharable resources, on the other hand, do not require mutually exclusive access, and thus cannot be involved in a deadlock.

2. Hold and Wait

1. When a process requests a resource, it does not hold any other resources. One protocol that be used requires each process to request and be allocated all its resources before it begins execution.

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.

There are two main disadvantages to these protocols. First, resource utilization may be low, since many of the resources may be allocated but unused for a long period. In the example given, for instance, we can release the tape drive and disk file, and then again request the disk file and printer, only if we can be sure that our data will remain on the disk file.

If we cannot be assured that they will, then we must request all resources at the beginning for both protocols. Second, starvation is possible.

3. 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 preempted. That is this resources are implicitly released. The 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.

4. Circular Wait

Circular-wait condition never holds is to impose a total ordering of all resource types, and to require that each process requests resources in an increasing order of enumeration.

Let R = {R1, R2, ..., Rn} be the set of resource types. We

assign to each resource type a unique integer number, which allows to compare two resources and to determine whether one

precedes another in ordering. Formally, we define a one-to-one function F: RN, where N is the set of natural numbers.

13.2 Deadlock Avoidance

Prevent deadlocks requests can be made. The restraints ensure that at least one of the necessary conditions for deadlock to not occur, and, hence, those deadlocks cannot hold. Possible side effects of preventing deadlocks by this, melted, however, are Tow device utilization and reduced system throughput.

An alternative method for avoiding deadlocks is to require additional information about how resources are to be requested. For example, in a system with one tape drive and one printer, we might be told that process P will request the tape drive first and later the printer, before releasing both resources. Process Q on the other hand, will request the printer first and then the tape drive. With this knowledge of the complete sequence of requests and releases for each process we can decide for each request whether or not the process should wait.

A deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure there can never be a circular wait condition. The resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

1. Safe State

A state is safe if the system allocates resources to each process (up to its maximum) in some order and still avoid a deadlock. More formally, a system is in a safe state only if there of processes < P1, P2, ..., Pn > is in a safe sequence for the current

allocation state if, for each Pi the resources that Pj can still request

can be satisfied by the currently available resources plus the resources held by all the Pj, with j<i. In this situation, if the

resources that process Pi needs are not immediately available, then

Pi can wait until all Pj have finished. When they have finished, Pi

can obtain all of its needed resources, complete its designated task, return its allocated resources, and terminate. When Pi

terminates, Pi+1 can obtain its needed resources, and so on. If no

Fig 13.5 Safe, Unsafe deadlock state

In document INTRODUCTION TO OPERATING SYSTEMS (Page 152-155)