Unit5 OS
Unit5 OS
org
Principles of Deadlock
A set of blocked processes each holding a resource and waiting to acquire a
resource held by another process in the set
Example
System has 2 disk drives
P1 and P2 each hold one disk drive and each needs another one
Example
semaphores A and B, initialized to 1
P0 P
wait (A); wait(B)
wait (B); wait(A)
System Model:
In any system, there are number of processes which are competing for the
resources available in the system. Memory, CPU, files, input/output devices (like printer,
scanner, etc.) are called resource types. Each resource type may have many instances i.e.,
the number of resource of the same type.
For example, printer is a particular resource type and if we have two printers in
the system, we say that, we have two instances of resource type printer. If a process
requests same resource, then any instance of that resource type should satisfy the need of
process, only then they belong to same class of resource type.
www.specworld.in 1 www.smartzworld.com
www.jntuworldupdates.org
Deadlock Characterization:
Resource-Allocation Graph
A set of vertices V and a set of edges E
V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes in the system
R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
request edge – directed edge P1 ® Rj
assignment edge – directed edge Rj ® Pi
Process
www.specworld.in 2 www.smartzworld.com
www.jntuworldupdates.org
Pi is holding an instance of Rj
Pi
Rj
Example of a Resource Allocation Graph
www.specworld.in 3 www.smartzworld.com
www.jntuworldupdates.org
Basic Facts
If graph contains no cycles Þ no deadlocknIf graph contains a cycle Þlif only one instance
per resource type, then deadlock
if several instances per resource type, possibility of deadlock
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock statenAllow the system to enter a
deadlock state and then recovernIgnore the problem and pretend that deadlocks never
occur in the system; used by most operating systems, including UNIX
Deadlock Prevention:
Deadlock Avoidance:
Safe State
When a process requests an available resource, system must decide if immediate
allocation leaves the system in a safe state
System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
processes is the systems such that for each Pi, the resources that Pi can still request can
www.specworld.in 4 www.smartzworld.com
www.jntuworldupdates.org
be satisfied by currently available resources + resources held by all the Pj, with j < inThat
is:
If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished
When Pj is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate
When Pi terminates, Pi +1 can obtain its needed resources, and so on
Basic Facts:
nIf a system is in safe state Þ no deadlocksnIf a system is in unsafe state Þ possibility of
deadlocknAvoidance Þ ensure that a system will never enter an unsafe state.
Safe, Unsafe , Deadlock State
Avoidance algorithms:
Resource-Allocation Graph
www.specworld.in 5 www.smartzworld.com
www.jntuworldupdates.org
Banker’s Algorithm
Multiple instancesnEach process must a priori claim maximum usenWhen a process
requests a resource it may have to wait nWhen a process gets all its resources it must
return them in a finite amount of time
www.specworld.in 6 www.smartzworld.com
www.jntuworldupdates.org
Finish[i]=true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
1. Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k
instances of resource type Rj1. If Requesti £ Needi go to step 2. Otherwise, raise
error condition, since process has exceeded its maximum claim
2. If Requesti £ Available, go to step 3. Otherwise Pi must wait, since resources are
not available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
If safe Þ the resources are allocated to Pi
If unsafe Þ Pi must wait, and the old resource-allocation state is restored
Example of Banker’s Algorithm
www.specworld.in 7 www.smartzworld.com
www.jntuworldupdates.org
Deadlock Detection
nAllow system to enter deadlock state nDetection algorithmnRecovery scheme
www.specworld.in 8 www.smartzworld.com
www.jntuworldupdates.org
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available(b) For i = 1,2, …, n, if Allocationi ¹ 0, then
Finish[i] = false;otherwise, Finish[i] = true2. Find an index i such that both:
(a) Finish[i] == false(b) Requesti £ WorkIf no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 24. If Finish[i] == false, for some i, 1 £ i £ n, then the system is in deadlock
state. Moreover, if Finish[i] == false, then Pi is deadlocked
Algorithm requires an order of O(m x n2) operations to detect whether the system is
in deadlocked state
Example of Detection Algorithm
Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)
Snapshot at time T0:
Allocation Request
Available
ABC ABC
ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Detection-Algorithm Usage
When, and how often, to invoke depends on:
www.specworld.in 9 www.smartzworld.com
www.jntuworldupdates.org
Selecting a victim – minimize costnRollback – return to some safe state, restart process
for that statenStarvation – same process may always be picked as victim, include number
of rollback in cost factor
I/O Systems
Explore the structure of an operating system’s I/O subsystem
Discuss the principles of I/O hardware and its complexity
Provide details of the performance aspects of I/O hardware and software
I/O Hardware
www.specworld.in 10 www.smartzworld.com
www.jntuworldupdates.org
Polling
www.specworld.in 11 www.smartzworld.com
www.jntuworldupdates.org
busy
ErrornBusy-wait cycle to wait for I/O from device
Interrupts
CPU Interrupt-request line triggered by I/O devicenInterrupt handler receives
interruptsnMaskable to ignore or delay some interruptsnInterrupt vector to dispatch
interrupt to correct handler
Based on priority
Some nonmaskablenInterrupt mechanism also used for exceptions
www.specworld.in 12 www.smartzworld.com
www.jntuworldupdates.org
www.specworld.in 13 www.smartzworld.com
www.jntuworldupdates.org
www.specworld.in 14 www.smartzworld.com
www.jntuworldupdates.org
Network Devices
Varying enough from block and character to have own interfacenUnix and Windows
NT/9x/2000 include socket interface
Separates network protocol from network operation
Includes select() functionalitynApproaches vary widely (pipes, FIFOs, streams, queues,
mailboxes)
Synchronous Asynchronous
Kernel I/O Subsystem
Scheduling
Some I/O request ordering via per-device queue
www.specworld.in 15 www.smartzworld.com
www.jntuworldupdates.org
Some OSs try fairnessnBuffering - store data in memory while transferring between
devices
To cope with device speed mismatch
To cope with device transfer size mismatch
To maintain “copy semantics”
Device-status Table
www.specworld.in 16 www.smartzworld.com