0% found this document useful (0 votes)
16 views15 pages

Advanced Os-Unit 1

Uploaded by

Samuel Gochel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views15 pages

Advanced Os-Unit 1

Uploaded by

Samuel Gochel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1|Page

ADVANCED OPERATING SYSTEM


UNIT – 1 : Review of Basic Concepts of Operating
Systems: Process Deadlocks

Introduction to Deadlocks:

Definition and Explanation:

In a multiprogramming environment, several processes compete for resources. A situation may


arise where a process is waiting for a resource that is held by other waiting processes. This
situation is called a deadlock.

A system has a finite set of resources such as memory, I/O devices, etc. It also has a finite set of
processes that need to use these resources. A process that wishes to use any of these resources,
makes a request to use that resource. If the resource is free the process gets it. If it is being used
by another process, it waits for it to become free. The assumption is that the resource will
eventually become free and the waiting process will then use the resource. But in some
situations, the other process may also be waiting for some resource.

"A set of processes is in a deadlock state when every process in the set is waiting for an event
that can only be caused by another process in the set."

Suppose we have two tape drives and two processes. Each process has got one tape drive for
each process, but both need two such tape drives to proceed with execution. Each is waiting for
the other process to release the other tape drive. This will never happen as the other is also
waiting for the same thing. This situation is a deadlock.

Deadlock Examples:

Following are some examples of deadlock.

Example 1:

The following figure is of a traffic deadlock. It makes the nature of deadlock very clear. None of
the cars can pass unless at least one of them backs up or is removed.
2|Page

Example 2:

When two trains approach each other at a crossing, both shall come to a full stop and neither will
start up again until the other has gone.

Example 3:

Suppose we have, a printer and a tape drive. Process A requests for the printer and gets it.
Process B requests tape drive and it is granted to it. Now A asks for the tape drive but the is
denied until B releases it. At this point, the process B asks for the printer before the tape drive.

Now both processes are waiting for each other to release the resource and are blocked. Both
processes will remain in this situation forever. This situation is called deadlock.

Model of Resources

A resource is an object that is used by a process. It can be a piece of hardware such as:

• Tape drive

• Disk drive

• Printer

A resource can be a piece of information such as:

• File

• A record within a file

• A shared variable

• A critical section
3|Page

A computer typically has many different resources. In some cases, there maybe many instances
of a resource of a given type. A process needing one of these resources can use any one of them.
In other cases there may be only one instance of a resource.

Methods for Handling Deadlocks:

We can deal with the deadlock issue in several ways:

1. We can use specific protocols to prevent or avoid deadlocks so that a system may never enter
a deadlock state.

2. We can detect the deadlock and recover it.

3. We can totally ignore the deadlock problem.

Deadlock Prevention in Operating System:

We can prevent deadlocks by ensuring that at least-one of the four necessary conditions for
deadlock cannot occur, If at least one condition is not satisfied, a deadlock will net occur.

1. Mutual Exclusion:

Mutual exclusion condition must hold for non-sharable resources. For example, only one process
can have access to a printer at a time, otherwise the output is disturbed. Some resources can be
made sharable like a read-only file. Several processes can be granted read only access to a file
without interfering with each other. However, deadlock cannot be prevented by only denying the
mutual exclusion condition because some resources are intrinsically non-sharable.

2. Hold and Wait:

Deadlock can be prevented by denying the hold and wait precondition. This can be implemented
in two different ways.

1. One approach is that a process requests all .the resources that it needs in one single request at
process startup. The system will not grant any resource in the list until it can grant all the
required resources.

2. A less restrictive approach is to allow a process to request resources only when it is currently
holding no resources. If a process needs a new resource, it must first release all the resources it
has and then put the request. It may include a request for reallocation of a resource it just
released.

Problems with This Approach:


4|Page

Each of these strategies has some performance or resource utilization issues.

• If we allocate all resources at the beginning of the process, it may hold resources when it does not
need them. It reduces resource utilization. This is especially serious if a process does not know
what resources it will actually need for a given execution until it has started working on the data.
The process must request all the resources it might need.

• A process that needs several popular resources might face starvation. Some processes may never
execute since some other process always has control of some required resource.

3. No Preemption:

Preemption of resources means that we take away resources from processes when they are
waiting for other resources. This could work in the following ways:

• 1. As soon as a process requests for a resource that is not available, all of its held resources are
released. The process is now waiting for all of previously held resources plus the resource that it
requested. Suppose a process holds a tape drive and requests a line printer. If line printer is hot
available, the tape drive is taken away and the process is put into a state of waiting for both a
tape drive and a line printer.
• 2. When a process requests a resource that is available, it gets it. Otherwise, the system checks
those processes that are holding the requested resource and may be waiting for some more
resources. In this case, the resource is taken away from the waiting process and allocated to the
requesting process. If this cannot happen, the process has to wait. During the wait, some other
process may get some of its resources.

Problems with This Approach:

It only works if the resources are preemptible. Suppose a process has printed output on a line
printer and is waiting for some other resource before it can generate more output. The line printer
really cannot be taken away without disturbing the output.

This scheme can also lead to starvation for a process that needs several popular resources at the
same time. It may keep losing the resources it gets because they do L not all become available at
the same time.

4. Circular Wait:

We can prevent deadlock by making circular wait impossible. We can define an order by which
processes get resources to prevent circular wait. For example, each resource type is assigned a
number. The processes can only get resources in increasing order of those resource numbers.
5|Page

Suppose tape drive has number 1, disk drive has number 5 and printer has number 12. A process
wants to read the disk drive and print out the results. It will first need to allocate the disk drive
then the printer. It will be prevented from doing it in reverse order.

Problems with This Approach:

The problems with this approach are as follows:

• The order of resource numbering may prove arbitrary and inconvenient. This is not very serious
problem. There are often natural ways of numbering resources. For example, processes
generally use input devices before output devices.

• The order of numbering can force processes to request resources before they need them. It
reduces resource utilization. Suppose a process did some work on tape before reading input
from a card reader. It would still have to request the reader at the start of processing.

Models of Deadlocks
Distributed systems allow several kinds of resource requests.
The Single Resource Model
In the single resource model, a process can have at most
one outstanding request for only one unit of a resource.
Since the maximum out-degree of a node in a WFG for the
single resource model can be 1, the presence of a cycle in
the WFG shall indicate that there is a deadlock.

The AND Model


In the AND model, a process can request for more than
one resource simultaneously and the request is satisfied
only after all the requested resources are granted to the
process.
The out degree of a node in the WFG for AND model can
be more than 1.
The presence of a cycle in the WFG indicates a deadlock in
the AND model.
6|Page

Since in the single-resource model, a process can have at


most one outstanding request, the AND model is more
general than the single-resource model.
Consider the example WFG described in the Figure 1.
P11 has two outstanding resource requests. In case of the
AND model, P11shall become active from idle state only
after both the resources are granted.
There is a cycle P11->P21->P24->P54->P11 which
corresponds to a deadlock situation.
That is, a process may not be a part of a cycle, it can still be
deadlocked. Consider process P44 in Figure 1.
It is not a part of any cycle but is still deadlocked as it is
dependent on P24which is deadlocked.

The OR Model
In the OR model, a process can make a request for numerous
resources simultaneously and the request is satisfied if any one
of the requested resources is granted.
Presence of a cycle in the WFG of an OR model does not imply
a deadlock in the OR model.
Consider example in Figure 1: If all nodes are OR nodes, then
process P11 is not deadlocked because once process P33 releases
its resources, P32 shall become active as one of its requests is
satisfied.
After P32 finishes execution and releases its resources, process
P11 can continue with its processing.
In the OR model, the presence of a knot indicates a deadlock.
7|Page

The AND-OR Model


A generalization of the previous two models (OR model and
AND model) is the AND-OR model.
In the AND-OR model, a request may specify any combination
of and and or in the resource request.
For example, in the AND-OR model, a request for multiple
resources can be of the form x and (y or z).
To detect the presence of deadlocks in such a model, there is
no familiar construct of graph theory using WFG.
Since a deadlock is a stable property, a deadlock in the AND-
OR model can be detected by repeated application of the test
for OR-model deadlock.

Deadlock Avoidance in Operating System:

Deadlock avoidance is a technique used to avoid deadlock. It requires the information about how
different processes would request different resources. If given several processes and resources,
we can allocate the resources in some order to avoid the deadlock.

1. Safe State:

A state is said to be a safe state if the system may allocate the required resources to each process
up to the maximum required in a particular sequence, without facing deadlock. In safe state,
deadlock cannot occur. If a deadlock is possible, the system is said to be in an unsafe state. The
idea of avoiding a deadlock is to simply not allow the system to enter an unsafe state that may
cause a deadlock.

Example:

Consider a system with 24 tape drives and three processes: P0, P1 and P2. Po may require 20
tape-drives during execution, Pi may require 8, and P2 may require up to 18.
Suppose, Po is holding 10 tape drives, Pi holds 4 and Pa holds 4 tape drives. The system is said
to be in a safe state, since there is a safe sequence that avoids the deadlock.
8|Page

Process Maximum Required Current Allocated

P0 20 10

P1 8 4

P2 18 4

1. Safe Sequence:

This sequence implies that P1 can instantly get all of its needed resources. There are 24 total tape
drives. P1 already has 4 and needs 4 more. It can get since there are 6 free tape drives. Once it
finishes executing, it releases all 8 resources. At this point, the system has 10 free tape drives.
Now P0 can execute since it requires exactly 10 more drives to finish. After it finishes, the
system has a total of 20 free tape drives. These can be allocated to P2 and it can proceed since it
needs 14 more drives to complete its task.

2. Unsafe Sequence:

Suppose at some time P2 requests 2 more resource to make its holding resources 6. Now the
system is in an unsafe state. The system has now 4 free drives and can only be allocated to P1.
After P1 returns, it will leave 8 free resources. It is not enough for either P0 or P1 The system
enters a deadlock.

There are algorithms that determine if a safe state can be maintained after a resource allocation.
The resource will not be allocated that may cause the system to enter an unsafe state resulting in
a deadlock.

2. Resource-Allocation Graph Algorithm:

Resource-allocation graph algorithm is used to avoid deadlock. This algorithm uses the resource-
allocation graph by introducing a new edge called claim edge. The claim edge is used to indicate
that a process may request a resource in future. It is represented by a dashed line. When the
process requests that resource, the dashed line is converted to an assignment edge i.e. a solid line.
It indicates that the resource has been allocated to the process. After the process releases this
resource, the assignment edge is again converted to claim edge. All claim edges of a process are
represented in the graph. It requires the knowledge of all resources to be used by a process,
before the execution of the process starts.

When a process requests a resource, it is allocated only if converting the request edge to an
assignment edge does not form a cycle in the graph. If no cycle is formed, the system is in safe
state and the resource will be allocated. But if there is a cycle, the system will, enter an unsafe
state and the process will wait for the resource.
9|Page

3. Banker's Algorithm:

Banker's algorithm is less efficient than resource allocation graph algorithm. But it can be
implemented in a system with multiple instances of each resource type. It requires that each new
process should declare the maximum number of instances of each required resource type. When
a process requests certain resources, the system determines whether the allocation of those
resources will leave the system in safe state. If it will, the resources are allocated. Otherwise the
process must wait until some other process releases the resources-

The "Banker's algorithm allows the following:

• Mutual exclusion

• Wait and hold

• No preemption

It prevents the following:

• Circular wait

User process may only request one resource at a time. System grants request only if the request
will result in a safe state. This algorithm uses different data structures to implement deadlock
avoidance. Let n be the number of processes and m be the number of resource types.

Following data structures are required:

• Available: A vector of length m indicates number of available resources of each type.

• Max: An n x m matrix indicates the maximum requirement of each process.

• Allocated: An n x m matrix indicates the number of resources of each type currently


allocated to each process.

• Need: An n x in matrix indicates the remaining resource need of each process.

Example:

Assume we have the following resources:

• 5 tape drives

• 2 graphic displays
10 | P a g e

• 4 printers

• 3 disks

We can create a vector representing our total resources: Total = (5,2, 4, 3). Consider we have
already allocated these resources among four processes as demonstrated by the following matrix
named Allocated.

Allocated = (4,2,2,3).

We also need a matrix to show the number of each resource still needed for each process. We
call this matrix Need.

Process Name Tape Drives Graphics Printers Disk Drives

Process A 1 1 0 0

Process B 0 1 1 2

Process C 3 1 0 0

Process D 0 0 1 0

Table: Allocation matrix


Process Name Tape Drives Graphics Printers Disk Drives

Process A 1 1 0 0

Process B 0 1 1 2

Process C 3 1 0 0

Process D 0 0 1 0

Table: Need matrix

Now the vector Available = (1,0,2,0).

Working of Banker's Algorithm:

1. Find a row in the Need matrix, which is less than the Available vector. If such a row exists,
then the process represented by that row may complete with those additional resources. If no
such row exists, deadlock is possible.
11 | P a g e

2. You want to double-check that granting these resources to the process for the chosen row will
result in a safe state. Suppose that the process has acquired all its needed resources, executed,
terminated, and returned resources to Available vector. Now the value of Available vector should
be greater than or equal to the previous value.

3. Repeat steps 1 and 2 until:

• All the processes have successfully reached pretended termination. This indicates that the initial
state was safe. OR

• Deadlock is reached. This indicates that the initial state was unsafe.

Following is the working of the above algorithm: .

Iteration 1:

1. Examine the Need matrix. The only row- that is less than the Available vector is the one for
Process D.

Need (Process D) = (0,0,1,0) < (1/0, 2,0) = Available

2. If we assume that Process D completes, it will turn over its currently allocated resources,
incrementing the Available vector.

(1, 0, 2, 0) Current value of available

+ (1, 1, 0, 1) Allocation (Process D)

(2, 1, 2, 1) Updated value of Available

Iteration 2:

1. Examine the Need matrix, ignoring the row for Process D. The only row that is less than the
Available vector is the one for Process A.

Need (Process A) = (1,1,0,0) < (2,1,2,1) = Available

2. If we assume that Process A completes, it will turn over its currently allocated resources,
incrementing the Available vector.
12 | P a g e

(2, 1, 2, 1) CURRENT VALUE OF AVAILABLE

+ (2, 0, 1, 1) Allocation (Process A)

(4, 1, 3, 2) Updated value of Available

Iteration 3:

1. Examine the Need matrix without the row for Process D and Process A. The only row that is
less than the Available vector is the one for Process B.

Need (Process B) = (0,1,1,2) < (4,1,3,2) = Available

2. If we assume that Process B completes, it will turn over its currently allocated resources,
incrementing the Available vector.

(4, 1, 3, 2) CURRENT VALUE OF AVAILABLE

+ (0, 1, 0, 0) Allocation (Process B)

(4, 2, 3, 2) Updated value of Available

Iteration 4:

1. Examine the Need matrix without the rows for Process A, Process B, and Process D. The only
row left is the one for Process C, and it is less than the Available vector.

Need (Process C) = (3,1,0, 0) < (4,2,3,2) = Available

2. If we assume that Process C completes, it will turn over its currently allocated resources,
incrementing the Available vector.

(4, 2, 3, 3) CURRENT VALUE OF AVAILABLE

+ (1, 0, 1, 1) Allocation (Process C)

(5, 2, 4, 3) Updated value of Available

Notice that the final value of the Available vector is the same as the original Total vector,
showing the total number of all resources:
13 | P a g e

Total = (5, 2,4,2) < (5,2,4,2) = Available

This means that the initial state represented by the Allocation and Need matrices is a safe state.
The safe sequence that assures this safe state is <D, A, B, C>.

Deadlock Detection/Algorithm/Example in Operating System:

If the system does not use any of the preventive or avoidance methods, there is a possibility of
deadlock occurrence. If a deadlock occurs in the system, it must be detected and recovered. The
system needs to provide some methods of deadlock detection and recovery. These methods are
overhead for the system.

1 Single Instance of Each Resource Type:

If each resource type has only one instance (There is only 1 printer, 1 hard drive, 1 tape drive,
etc.), we can use a variant of the resource-allocation graph called a wait-for graph. It is done by
removing the nodes of type resource and collapsing the appropriate edges.

In this graph, processes are not waiting for resources. They are waiting for other processes that
hold these resources. A cycle in the graph indicates the occurrence of a deadlock in the system.

This approach requires that a cycle detection algorithm runs from time to time to detect
deadlock. Algorithms that detect cycles are O(N2). N is the number of vertices in the graph.

2. Detection-Algorithm Usage:

Detection algorithms need to be executed to detect a deadlock. The frequency and time when we
run such algorithm is dependent on how often we assume deadlocks occur and how. many
processes they may effect.
If deadlocks may happen often, we run the detection often. If it affects many processes, we may
decide to run it often so that less processes are affected by the deadlock.
We could run the algorithm on every resource request. The deadlocks are rare so it is not very
efficient use of resources. We could run the algorithm from time to time like every hour or at
random times during the system execution lifetime.

Deadlock Characterization in Operating System / Necessary and Sufficient


Deadlock Conditions

Deadlock occurs if the following four conditions take place simultaneously in a system:
14 | P a g e

1. Mutual Exclusion:

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

2. Hold and Wait:

A process must be holding at least one resource and waiting, to acquire additional resources that
are currently being held by other processes.

3. No Preemption:

Resources cannot be preempted. A resource can be released only voluntarily by the process
holding it after that process has completed its task.

4. Circular Wait:

A set {P0 P1, P2... Pn} of waiting processes must exist such that P1 is waiting for a resource that is
held by P1, P1 is waiting for a resource that is held by P2, ..., Pn-1 is waiting for a resource that is
held by Pn and Pn is waiting for a resource that is held by P0.

All four conditions must hold for a deadlock to occur.

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.
15 | P a g e

As an example, consider the traffic deadlock in the following figure

Consider each section of the street as a resource.

1. Mutual exclusion condition applies, since only one vehicle can be on a section of the
street at a time.
2. Hold-and-wait condition applies, since each vehicle is occupying a section of the street,
and waiting to move on to the next section of the street.
3. No-preemptive condition applies, since a section of the street that is a section of the street
that is occupied by a vehicle cannot be taken away from it.
4. Circular wait condition applies, since each vehicle is waiting on the next vehicle to move.
That is, each vehicle in the traffic is waiting for a section of street held by the next
vehicle in the traffic.

The simple rule to avoid traffic deadlock is that a vehicle should only enter an intersection if it is
assured that it will not have to stop inside the intersection.

It is not possible to have a deadlock involving only one single process. The deadlock involves a
circular “hold-and-wait” condition between two or more processes, so “one” process cannot hold
a resource, yet be waiting for another resource that it is holding. In addition, deadlock is not
possible between two threads in a process, because it is the process that holds resources, not the
thread that is, each thread has access to the resources held by the process.

You might also like