Documente Academic
Documente Profesional
Documente Cultură
2
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
A system can be modeled as a collection of limited
resources, which can be partitioned into different
categories, to be allocated to a number of processes, each
having different needs. Resource categories may include
memory, printers, CPUs, open files, tape drives, CD-
ROMS, etc.
By definition, all the resources within a category are
equivalent, and a request of this category can be equally
satisfied by any one of the resources in that category. Some
categories may have a single resource.
A process must request a resource before using it and must
release the resource after using it. A process may request as
many resources as it requires to carry out its designated
task. 3
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
Under the normal mode of operation, a process may
utilize a resource in only the following sequence:
i. Request: The process requests the resource. If the
request cannot be granted immediately (for
example, if the resource is being used by another
process), then the requesting process must wait
until it can acquire the resource. For example the
system calls open( ), malloc( ), new( ), and request( ).
ii. Use: The process can operate on the resource (for
example, if the resource is a printer, the process can
print on the printer or read from a file).
iii. Release: The process releases the resource. For
example, close( ), free( ), delete( ), and release( ).
4
For all kernel-managed resources, the kernel keeps
track of what resources are free and which are
allocated, to which process they are allocated, and a
queue of processes waiting for this resource to become
available.
Application-managed resources can be controlled using
mutexes or wait( ) and signal( ) calls, (i.e. binary or
counting semaphores.)
A set of processes is deadlocked when every process in
the set is waiting for a resource that is currently
allocated to another process in the set (and which can
only be released when that other waiting process
makes progress.)
5
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
A deadlock situation can arise if the following four conditions hold simultaneously
in a system:
1. Mutual exclusion. At least one resource must be held in a
nonsharable 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.
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; that is, 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, ..., Pn} of waiting processes must exist
such that P0 is waiting for a resource held by P1, P1 is waiting for a
resource held by P2, ..., Pn−1 is waiting for a resource held by Pn, and
Pn is waiting for a resource held by P0. 6
In some cases deadlocks can be understood more clearly through the use
of Resource-Allocation Graphs, having the following properties:
• A set of resource categories, { R1, R2, R3, . . ., RN }, which appear as
square nodes on the graph. Dots inside the resource nodes indicate
specific instances of the resource. ( E.g. two dots might represent two
laser printers.)
• A set of processes, { P1, P2, P3, . . ., PN } the set consisting of all
the processes in the system
• Request Edges - A set of directed arcs from Pi to Rj, indicating that
process Pi has requested Rj, and is currently waiting for that resource
to become available. Pi Rj
• Assignment Edges - A set of directed arcs from Rj to Pi indicating that
resource Rj has been allocated to process Pi, and that Pi is currently
holding resource Rj. Rj Pi
Note that a request edge can be converted into an assignment edge by reversing
the direction of the arc when the request is granted. 7
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
R
E
S
O
U
R
C
E
-
A
L
L
O
C
A
T
I
O The sets P, R, and E:
N
P = {P1, P2, P3}
G
R R = {R1, R2, R3, R4}
A
P E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3}
H 8
• If a resource-allocation graph contains no cycles, then the system is not
deadlocked.
• 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.
17
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.
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.
• The banker's algorithm relies on several key data structures:
18
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available :
It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a
system.
Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation :
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type
Rj
Need :
It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
• Allocationi specifies the resources currently allocated to process Pi and Needi
specifies the additional resources that process Pi may still request to complete
its task.
• Banker’s algorithm consist of Safety algorithm and Resource request algorithm.
19
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
The algorithm for finding out whether or not a system is in a safe state can
be described as follows:
20
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
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:
21
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
Considering a system with five processes P0 through P4 and
three resources types A, B, C. Resource type A has 10 instances, B has
5 instances and type C has 7 instances. Suppose at time t0 following
snapshot of the system has been taken:
22
Question2. Is the system in safe state? If Yes, then what is the safe
sequence?
23
Question3. What will happen if process P1 requests one additional
instance of resource type A and two instances of resource type C?
24
Hence the new system state is safe, so we can immediately grant the request for process P1
when the system is in this state, a request for (3,3,0) by P4 cannot be granted, since the
resources are not available. Furthermore, a request for (0,2,0) by P0 cannot be granted,
even though the resources are available, since the resulting state is unsafe. 25
An operating system uses the Banker’s algorithm for deadlock avoidance when
managing the allocation of three resource types X, Y, and Z to three processes P0,
P1, and P2. The table given below presents the current system state. Here, the
Allocation matrix shows the current number of resources of each type allocated
to each process and the Max matrix shows the maximum number of resources of
each type required by each process during its execution.
There are 3 units of type X, 2 units of type Y and 2 units of type Z still available.
The system is currently in a safe state. Consider the following independent
requests for additional resources in the current state:
REQ1: P0 requests 0 units of X,
0 units of Y and 2 units of Z
REQ2: P1 requests 2 units of X,
0 units of Y and 0 units of Z
26
Operating Systems 14CS3503 Mukesh Chinta, Asst Prof, CSE, VRSEC
If deadlocks are not avoided, then another approach is to detect
when they have occurred and recover somehow.
In addition to the performance hit of constantly checking for
deadlocks, a policy / algorithm must be in place for recovering
from deadlocks, and there is potential for lost work when
processes must be aborted or have their resources preempted.
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Now suppose that process P2 makes a request for an additional instance of type C, yielding
the state shown below. Is the system now deadlocked?