4.3 Deadlock (Autosaved)
4.3 Deadlock (Autosaved)
4.3 Deadlock (Autosaved)
This strategy involves designing a system that violates one of the four
necessary conditions required for the occurrence of deadlock.
This ensures that the system remains free from the deadlock.
1. Mutual Exclusion-
To violate this condition, all the system resources must be such that they
can be used in a shareable mode.
In a system, there are always some resources which are mutually exclusive
by nature.
So, this condition can not be violated.
The mutual-exclusion condition must hold for non-sharable resources.
Sharable resources, do not require mutually exclusive access and thus cannot
be involved in a deadlock.
Read-only files are a good example of a sharable resource. If several processes
attempt to open a read-only file at the same time , they can be granted
simultaneous access to the file.
A process never needs to wait for a sharable resource. In general, however, we
cannot prevent deadlocks by denying the mutual-exclusion condition, because
some resources are intrinsically non-sharable.
2. Hold and Wait-
Approach-01:
In this approach,
A process has to first request for all the resources it requires for execution.
Once it has acquired all the resources, only then it can start its execution.
This approach ensures that the process does not hold some resources and
wait for other resources.
Drawbacks-
In this approach,
•A process is allowed to acquire the resources it desires at the current moment.
•After acquiring the resources, it start its execution.
•Now before making any new request, it has to compulsorily release all the
resources that it holds currently.
•This approach is efficient and implementable.
Approach-03:
In this approach,
•A timer is set after the process acquires any resource.
•After the timer expires, a process has to compulsorily release the resource.
To ensure that the hold-and-wait condition never occurs in the system, we
must guarantee that, whenever a process requests a resource, it does not hold
any other resources.
• One protocol that can be used requires each process to request and be
allocated all its resources before it begins execution.
• An alternative protocol allows a process to request resources only when it
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.
• we consider a process that copies data from a DVD drive to a file on disk,
sorts the file, and then prints the results to a printer.
• If all resources must be requested at the beginning of the process, then the
process must initially request the DVD drive, disk file, and printer. It will
hold the printer for its entire execution, even though it needs the printer
only at the end.
• The second method allows the process to request initially only the DVD
drive and disk file. It copies from the DVD drive to the disk and then
releases both the DVD drive and the disk file. The process must then again
request the disk file and the printer. After copying the disk file to the printer,
it releases these two resources and terminates.
Both these protocols have two main disadvantages.
First, resource utilization may be allocated, since resources may be allocated but
unused for a long period.
Second, starvation is possible. A process that needs several popular resources
may have to wait indefinitely, because at least one of the resources that it needs
is always allocated to some other process.
3. No preemption
We will see two approaches here. If a process request for a
resource which is held by another waiting resource, then the
resource may be preempted from the other waiting resource. In
the second approach, if a process request for a resource which are
not readily available, all other resources that it holds are
preempted.
The challenge here is that the resources can be preempted only if
we can save the current state can be saved and processes could be
restarted later from the saved state.
To ensure that no preemption condition does not hold, we can use the following
protocol.
If a process is holding some resources and requests another resource that cannot be
immediately allocated to it (that is, the process must wait), then all resources
currently being held are preempted.
The preempted resources are added to the list of resources for which the process is
waiting. The process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting.
Alternatively, if a process requests some resources, we first check whether they are
available. If they are, we allocate them.
If they are not, we check whether they are allocated to some other process that is
waiting for additional resources.
P2(waiting
P1(requesting process)
process for printer)
Holds Waiting for
Holds DVD printer disk file.
If so, we preempt the desired resources from the waiting process and allocate them
to the requesting process.
If the resources are neither available nor held by a waiting process, the
requesting process must wait. While it is waiting, some of its resources may
be preempted, but only if another process requests them.
A process can be restarted only when it is allocated the new resources it is
requesting and recovers any resources that were preempted while it was
waiting.
This protocol is often applied to resources whose state can be easily saved
and restored later, such as CPU registers and memory space. It cannot
generally be applied to such resources as printers and tape drives.
4. Circular Wait-
This condition can be violated by not allowing the processes to wait for
resources in a cyclic manner.
To violate this condition, the following approach is followed-
Approach-
Safe state: A state is safe if the system can allocate all resources requested by
all processes without entering a deadlock state.
A state is safe if the system can allocate resources to each process (up
to its maximum) in some order and still avoid a deadlock.
a system is in a safe state only if there exists a safe sequence.
A sequence of processes < P1,P2,P3…Pn> is a safe sequence for the
current allocation state it for each Pi the resource requests can be
satisfied by the currently available resources plus the resources held
by all Pj, with j<i.
In this situation, if the resources that 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 such sequence exists, then the system state is said to be unsafe.
An unsafe state may lead to a deadlock. As long as the state is safe,
the operating system can avoid unsafe (and deadlocked) states.
The behavior of the processes controls unsafe states. To illustrate, we
consider a system with 12 magnetic tape drives and three processes:
Po, P1, and P2 . Process Po requires 10 tape drives, process P1 may
need as many as 4 tape drives, and process P2 may need up to 9 tape
drives. Suppose that, at time to, process Po is holding 5 tape drives,
process P1 is holding 2 tape drives, and process P2 is holding 2 tape
drives. (Thus, there are 3 free tape drives.)
At time to, the system is in a safe state. The sequence < P1, Po, P2 > satisfies the safety
condition. Process P1 can immediately be allocated all its tape drives and then return them
(the system will then have 5 available tape drives); then process Po can get all its tape
drives and return them (the system will then have 10 available tape drives); and finally
process P2 can get all its tape drives and return them (the system will then have all 12 tape
drives available).
A system can go from a safe state to an unsafe state. Suppose that, at
time t1, process P2 requests and is allocated one more tape drive. The
system is no longer in a safe state. At this point, only process P1can be
allocated all its tape drives. When it returns them, the system will have
only 4 available tape drives. Since process Po is allocated 5 tape drives
but has a maximum of 10, it may request 5 more tape drives. Since they
are unavailable, process Po must wait. Similarly, process P2 may request
an additional 6 tape drives and have to wait, resulting in a deadlock.
Our mistake was in granting the request from process P2 for one more
tape drive. If we had made P2 wait until either of the other processes
had finished and released its resources, then we could have avoided the
deadlock.
<p2(1+2=3), p1>
Banker’s Algorithm in Operating System
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] =
k means process Pi wants k instances of resource type Rj.
When a request for resources is made by process Pi, the
following actions are taken:
R0<=N0 3<5
R0<=A 3<=4