Unit5 Jntustore
Unit5 Jntustore
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously
Mutual exclusion: only one process at a time can use a resource
Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes
No preemption: a resource can be released only voluntarily by the process
holding it, after that process has completed its task
Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that
P0 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 P0 is waiting for a resource that is held by P0.
n
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
Pi is holding an instance of Rj
Pi
Rj
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
Restrain the ways request can be made
Mutual Exclusion – not required for sharable resources; must hold for
nonsharable resources
Hold and Wait – must guarantee that whenever a process requests a resource,
it does not hold any other resources
Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has
none
Low resource utilization; starvation possible
No Preemption –
If a process that is holding some resources requests another resource that
cannot be immediately allocated to it, then all resources currently being held are
released
Preempted resources are added to the list of resources for which the process is
waiting
Process will be restarted only when it can regain its old resources, as well as the
new ones that it is requesting
Circular Wait – impose a total ordering of all resource types, and require that
each process requests resources in an increasing order of enumeration
Deadlock Avoidance
Requires that the system has some additional a priori information
available
Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need
The deadlock-avoidance algorithm dynamically examines the resource-allocation
state to ensure that there can never be a circular-wait condition
Resource-allocation state is defined by the number of available and allocated
resources, and the maximum demands of the processes
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 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
nSingle instance of a resource type
lUse a resource-allocation graph
nnMultiple instances of a resource type
l Use the banker’s algorithm
Resource-Allocation Graph
Unsafe State In Resource-Allocation Graph
Banker’s Algorithm
nMultiple 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
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
nAvailable: Vector of length m. If available [j] = k, there are k instances of
resource type Rj available
nMax: n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type RjnAllocation: n x m matrix. If Allocation[i,j] = k then
Pi is currently allocated k instances of RjnNeed: n x m matrix. If Need[i,j] = k,
then Pi may need k more instances of Rj to complete its task
Deadlock Detection
nAllow system to enter deadlock state nDetection algorithmnRecovery scheme
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
Detection-Algorithm Usage
nWhen, and how often, to invoke depends on:
lHow often a deadlock is likely to occur?
lHow many processes will need to be rolled back?
one for each disjoint cyclenIf detection algorithm is
invoked arbitrarily, there
may be many cycles in the resource graph and so we would not be able to tell
which of the many deadlocked processes “caused” the deadlock
Recovery from Deadlock: Process Termination
nAbort all deadlocked processesnAbort one process at a time until the deadlock
cycle is eliminatednIn which order should we choose to abort?
lPriority of the process
lHow long process has computed, and how much longer to completion
lResources the process has used
lResources process needs to complete
lHow many processes will need to be terminated
lIs process interactive or batch?
I/O Systems
nExplore the structure of an operating system’s I/O subsystem
nDiscuss the principles of I/O hardware and its complexity
nProvide details of the performance aspects of I/O hardware and software
n
I/O Hardware
nIncredible variety of I/O devices
nCommon concepts
lPort
lBus (daisy chain or shared direct access)
lController (host adapter)
nI/O instructions control devices
nDevices have addresses, used by
lDirect I/O instructions
lMemory-mapped I/O
Device-status Table
Sun Enterprise 6000 Device-Transfer Rates
Error Handling
nOS can recover from disk read, device unavailable, transient write failuresnMost
return an error number or code when I/O request fails nSystem error logs hold
problem reports
I/O Protection
nUser process may accidentally or purposefully attempt to disrupt normal
operation via illegal I/O instructions
lAll I/O instructions defined to be privileged
lI/O must be performed via system calls
Memory-mapped and I/O port memory locations must be protected too
Performance
nI/O a major factor in system performance:lDemands CPU to execute
device driver, kernel I/O code
lContext switches due to interrupts
lData copying
lNetwork traffic especially stressful
Intercomputer Communications
Improving Performance
nReduce number of context switches
nReduce data copying
nReduce interrupts by using large transfers, smart controllers, polling
nUse DMA
nBalance CPU, memory, bus, and I/O performance for highest throughput
Device-Functionality Progression