Professional Documents
Culture Documents
• Process Deadlock
• A process is deadlocked when it is waiting on
an event which will never happen
• System Deadlock
• A system is deadlocked when one or more
processes are deadlocked
The Deadlock: How it happens?
A Computing example:
• Scanned document to be Recorded on CD
• System has 1 scanner and 1 CD Recorder
• 2 processes each need both devices.
P0 acquires scanner and waits for CD
P1 acquires CD and waits for scanner
P0 will not release scanner until catch hold the CD
P1 will not release CD until catch hold the scanner
Result: Deadlock
Preemptable and Non Preemptable Resources
Things for which we request exclusive access.
Examples: Databases, files, shared memory,
printer, cd/dvd writer, tape drive, etc.
Types
1. Preemptable – can be taken away w/out ill
effects
2. Non preemptable – cannot be take away
w/out causing a failure!
Resource examples
• Memory – preemptable
• CPU – preemptable
• CD writing – non preemptable
• Printing – non preemptable
Process A
Resource R
Request a Resource A R
A
Hold a Resource A R
R S
Deadlock
B
Deadlock Modeling (1)
Process Resource
• Deadlock Detection
• Deadlock Recovery
• Deadlock Avoidance
• Deadlock Prevention
2. Detect and recover
1. Preemption
2. Rollback (using checkpoints)
3. Kill process
Recovery: preemption
• Temporarily take back needed resource
• Ex. Printer
• Pause at end of page k
• Start printing other job
• Resume printing original job starting at page k+1
Recovery: Rollback
• Checkpoint – save entire process state (typically right
before the resource was allocated)
• Unsafe != deadlocked
Safe states
Unsafe states
• Unsafe != deadlocked
Safe states
Unsafe states
Implementation
• A resource request is only allowed if it results in a
SAFE state
• The system is always maintained in a SAFE state
so eventually all requests will be filled
Advantages
• It works
• Allows jobs to proceed where a prevention
algorithm wouldn't
Problems
• Requires that there are a fixed number of
resources
• What happens if a resource goes down?
• Does not allow the process to change its
Maximum need while processing
Banker’s algorithm (SR)
Issue 1 to B
Free =1
No max could be
served now
N:
(need)
A (available)
can
completely
satisfy D’s
needs.
Banker’s algorithm (MR)
Previous:
E=(6342)
To check for a safe state:
P=(5322)
1. Look for a row, R, in N <= A.
A=(1020)
2. Assume the process of the chosen row requests all
resources, runs to completion, and terminates
(giving back all of its resources to A).
After D
terminates:
E=(6342)
P=(4221)
A=(2121)
Banker’s algorithm (MR)
Previous:
E=(6342)
To check for a safe state:
P=(5322)
1. Look for a row, R, in N <= A. A=(1020)
E=(6342)
P=(4221)
A=(2121)
Banker’s algorithm (MR)
Previous:
E=(6342)
To check for a safe state:
P=(4221)
1. Look for a row, R, in N <= A.
A=(2121)
2. Assume the process of the chosen row requests all
resources, runs to completion, and terminates
(giving back all of its resources
After A
terminates:
E=(6342)
P=(1210)
A=(5132)
Banker’s algorithm (MR)
Previous:
E=(6342)
To check for a safe state:
P=(4221)
1. Look for a row, R, in N <= A.
A=(2121)
E=(6342)
P=(1210)
A=(5132)
Banker’s algorithm (MR)
Previous:
E=(6342)
To check for a safe state:
P=(1210)
1. Look for a row, R, in N <= A.
A=(5132)
E=(6342)
P=(1110)
A=(5232)
Banker’s algorithm (MR)
Previous:
E=(6342)
To check for a safe state:
P=(1210)
1. Look for a row, R, in N <= A.
A=(5132)
E=(6342)
P=(1110)
A=(5232)
DEADLOCK PREVENTION
Deadlock Prevention
Implementation
• A process is given its resources on a "ALL or
NONE" basis
• Either a process gets ALL its required
resources and proceeds or it gets NONE of
them and waits until it can
Denying the “Hold & Wait”
Advantages
• It works
• Reasonably easy to code
Problems
• Resource wastage
• Possibility of starvation
Denying “No pre-emption”
• Tricky at best.
• Impossible at worst.
Implementation
• Resources may be removed from a process
before it is finished with them. Forcefully
preempt the resource.
Denying “No pre-emption”
Advantages
• It works
• Possibly better resource utilisation
Problems
• The cost of removing a process's resources
• The process is likely to lose work it has done.
(How often does this occur?)
• Possibility of starvation
Attacking the Circular Wait Condition