Deadlock Notes-Os
Deadlock Notes-Os
t resource mgt)
Deadlock is a situation where a set of processes are blocked
because each process is holding a resource and waiting for
another resource acquired by some other process. Deadlock is a
situation which involves the interaction of more than one resources
and processes with each other.
OR
Example of Deadlock
Note:
All the 4 conditions are necessary for the deadlock to occur. If
anyone is prevented or resolved, the deadlock is resolved.
Methods for handling deadlock
There are four ways to handle deadlock
Most of the operating systems are not able to prevent the deadlocks to
occur. When the condition of deadlock occurs then different operating
systems respond to them in different and non-standard way. Most
approaches work by preventing one of the four Coffman conditions from
occurring, especially for the Current Wait. Major approaches are
1) Ignoring Deadlock : This approach assumed that a deadlock will
never occur which is one of the most important application of
Ostrich algorithm. The Ignoring deadlock is commonly used by
MINIX and UNIX. When the time interval between the occurrences of
deadlocks are seems to be large in addition to the increment in data
loss incurred each time is tolerable then in such case Ignoring
deadlock is used.
2) Deadlock prevention :
3) Detection and recovery :In this approach, deadlocks are allowed
to occur. Then the state of the system is examined to detect that a
deadlock has occurred and subsequently it is corrected. In this case,
algorithm is employed that tracks resource allocation and process states,
it rolls back and restarts one or more of the processes in order to remove
the detected deadlock.
4.Deadlock Avoidance
Deadlock can be avoided if some of the information about the processes
is well known by the Operating System before the allocation of resources
starts. For every resource request, the system sees whether granting the
request will mean that the system will enter an unsafe state, meaning a
state that could result in deadlock. The system then only grants requests
that will lead to safe states.
Pi requests an instance of Rj
Pi is holding an instance of Rj
If a resource-allocation graph contains no cycles, then the system
is not deadlocked. (When looking for cycles, remember that these
are directed graphs)
If a resource-allocation graph does contain cycles AND each
resource category contains only a single instance, then a deadlock
exists.
If a resource category contains more than one instance, then the
presence of a cycle in the resource-allocation graph indicates
the possibility of a deadlock, but does not guarantee one.
Resource allocation graph with a deadlock
Deadlock Prevention
Preemption
P1 ---- R3, R5
Deadlock Avoidance
Safe State
Banker's Algorithm
For resource categories that contain more than one instance the
resource-allocation graph method does not work, and more
complex (and less efficient) methods must be chosen.
The Banker's Algorithm gets its name because it is a method that
bankers could use to assure that when they lend out resources
they will still be able to satisfy all their clients. (A banker won't loan
out a little money to start building a house unless they are assured
that they will later be able to loan out the rest of the money to finish
the house.)
When a process starts up, it must state in advance the maximum
allocation of resources it may request, up to the amount available
on the system.
When a request is made, the scheduler determines whether
granting the request would leave the system in a safe state. If not,
then the process must wait until the request can be granted safely
1. Safety Algorithm
2. Resource Request algorithm
Consider a system with 5 processes (P0 ... P4) and 3 resources types
P0 0 1 0 7 5 3 7 4 3 3 3 2
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
ii) Is the system in a safe state? If so, which sequence satisfies the
safety criteria?
Go in increasing order and check NEED matrix which process can be
granted the resource
Available 3 3 2
+2 0 0 (P1 is the first process to be granted its
resources and after its work is over it will release
its allocated resources and that would be added
in available)
= 5 3 2 ----New available
+ 2 1 1 (P3 would be granted)
= 7 4 3 ----New Available
+0 0 2 (P4 would be granted and it will release its
allocated resources)
= 7 4 5 ----New Available
+0 1 0 (P0 would be granted and it will release its
allocated resources)
=7 5 5 ----New Available
+3 0 2 (P2 would be granted and it will release its
allocated resources)
= 10 5 7 (total available from which we started)
iii) If P1 comes with the request A=0, B=1, C=1then that request would
be granted or not? If yes then tell whether the system will go into
deadlock or not?
We know that no process can request more than its NEED matrix
Step 1: check if request <= need of that process and request <= available
0 1 1 <= 3 3 2(available)
That means it is ok to give and fulfil the request but still the system won’t give
the resources immediately. System will pretend as if it has given or granted the
resource and would check and apply banker’s algorithm’s safety algorithm and
see if safe state arises or not
Step 2: So in above case the updation would occur in NEED matrix of P1 as well
as Available resources and Allocation because if request of 0 1 1 is granted the
NEED of P1 would be 1 2 2 – 0 1 1= 1 1 1 and Available would be 3 3 2 – 0 1 1 =
3 2 1 and Allocation = 2 0 0 + 0 1 1 = 2 1 1
Step 3: Apply the safety algorithm again and check if any safe sequence is
made or not if there is a safe sequence then the request can be immediately
granted otherwise not.
Process Termination
1. Process priorities.
2. How long the process has been running, and how close it is
to finishing.
3. How many and what type of resources is the process
holding. (Are they easy to preempt and restore?)
4. How many more resources does the process need to
complete.
5. How many processes will need to be terminated
6. Whether the process is interactive or batch.
7. (Whether or not the process has made non-restorable
changes to any resource.)
Resource Pre-emption