3. System Model (1/5)
• System consists of finite resources
• The resources may
– Physical resources
• Example: printers, tape drives, memory space, CPU cycles
– Logical resources
• Example: semaphores, mutex locks, files
• Resource types/classes & Resource Instances
– Resource classes should be defined appropriately
• Example: CPU cycles, memory space, I/O devices, etc.
– Each resource type Ri has Wi instances
4/8/2022 3 of 52
Instructor: Mr.S.Christalin Nelson
4. System Model (2/5)
• Example-1
– System has 2 CPUS. i.e. Resource type CPU has 2 instances
– If process requests resource type CPU => any instance of this
type can be allocated
• Example-2
– System with resource type Printer has 2 instances (Basement
& Floor-9)
– If process requests resource type printer => Can any instance
be allocated?
4/8/2022 4 of 52
Instructor: Mr.S.Christalin Nelson
5. System Model (3/5)
• Process resource utilization sequence
– (1) Request, (2) Use , (3) Release
– (1) & (3) may be system calls
• Examples:
– request() and release() device
» semaphore: wait(), signal()
» mutex lock: acquire(), release()
– open() and close() file
– allocate() and free() memory
• System Table (OS records free/allocated resources) & Queue
4/8/2022 5 of 52
Instructor: Mr.S.Christalin Nelson
6. System Model (4/5)
• Deadlock?
– A set of processes is deadlocked when every process in the set
is waiting for an event (resource acquisition/release) that can
be caused only by another process in the set
• Processes never finish => System resources are tied-up => other
jobs prevented from starting
4/8/2022 6 of 52
Instructor: Mr.S.Christalin Nelson
7. System Model (5/5)
• Illustration-1: Deadlock involving same resource type
– A system has 2 CD-RW drives & 2 processes (P1 & P2)
– Each process holds 1 CD-RW drive
– If P1 & P2 requests for one more drive => Deadlock occurs
• Illustration-2: deadlock involving different resource type
– A system has 1 printer, 1 DVD drive & 2 processes (P1 & P2)
– P1 holds DVD drive & Process-2 holds printer
– If P1 requests printer & P2 requests DVD drive => Deadlock
occurs
4/8/2022 7 of 52
Instructor: Mr.S.Christalin Nelson
8.
9. Necessary Conditions for Deadlock
• The following conditions are not completely independent
– (1) Mutual exclusion
– (2) Hold and wait
– (3) No preemption
– (4) Circular wait
4/8/2022 9 of 52
Instructor: Mr.S.Christalin Nelson
10. Resource allocation graph (1/3)
• Describes Deadlock using Directed Graph
• Graph (G)
– Defined as set (V, E)
– V = {V1, V2, ..., Vn}
– E = {E1, E2, ..., En}
• For a Resource allocation graph (RAG)
– V = {P, R}
• Set of Active Processes, P = {P1, P2, ..., Pn}
• Set of Resources Types, R = {R1, R2, ..., Rn}
– E = Set of Directed edges
• Ei can be Request edge or Assignment edge
– Pi -> Rj (Pi requested for Rj & waits for it)
– Ri -> Pj (Pi’s request accepted & Ri is allocated to Pj)
4/8/2022 10 of 52
Instructor: Mr.S.Christalin Nelson
11. Resource allocation graph (2/3)
• Notation
– Circle (Process), Rectangle (Resource type), Dot in rectangle
(Resource instance)
– Request edge (Pi-> Rj)
• Edge points to a rectangle
– Assignment edge (Ri -> Pj)
• Edge designates one dot in rectangle
4/8/2022
Resource Instances &
Process States?
11 of 52
Instructor: Mr.S.Christalin Nelson
12. Resource allocation graph (3/3)
• RAG can contain a cycle
– If no cycle => No deadlock
– If cycle exists => Deadlock may or may not exist
• If each resource has one instance (Necessary & sufficient
condition) deadlock exists
• If each resource has >1 instance (Necessary & not sufficient
condition) deadlock may exists
• Example
– Two cycles
4/8/2022 12 of 52
Instructor: Mr.S.Christalin Nelson
13.
14. Methods to handle Deadlock (1/2)
• Ensure system never enters deadlock state -> Deadlock
prevention/avoidance
– Deadlock prevention
• One necessary condition not satisfied
– Deadlock avoidance
• Prior information on resource which process can request/use in
its lifetime required -> OS decides whether current request is
satisfied/delayed (i.e. Consider available resources, allocated
resources & future requests and releases)
• Allow system to enter deadlock state & recover -> Deadlock
detection & recovery
• Ignore problem & pretend that deadlocks never occur in
system -> handled by application developer (Used by UNIX &
Windows)
4/8/2022 14 of 52
Instructor: Mr.S.Christalin Nelson
15. Methods to handle Deadlock (2/2)
• Overhead for Deadlock detection and Recovery
– Run-time costs of maintaining necessary information &
executing detection algorithm
– Potential losses inherent in recovering from a deadlock
4/8/2022 15 of 52
Instructor: Mr.S.Christalin Nelson
16.
17. Deadlock Prevention (1/8)
• Mutual exclusion condition
– Should be satisfied (cannot be denied)
– Sharable resources do not require mutually exclusive access &
not involved in a deadlock.
• Example: Read-only files
– Some resources (at least one) are non-sharable
• Example: mutex lock
4/8/2022 17 of 52
Instructor: Mr.S.Christalin Nelson
18. Deadlock Prevention (2/8)
• Hold and Wait condition
– To deny, guarantee that when a process requests a resource it
does not hold any other resources
• Solution-1: Process can execute only after its gets all resources
requested. i.e System calls requesting resources for a process
precede all other system calls
• Solution-2: Process can request resources only after releasing all
currently allocated resources
– (Soln. 1 vs. 2) Illustration: P1 copies data from DVD drive to a
file on disk, sorts file, and prints results to a printer
• Solution-1: P1 requests & holds DVD drive, disk file, and printer
for its entire execution. Note: Printer required only at the end
• Solution-2: P1 requests & holds DVD and Disk File to perform
copy => Release both => Requests Disk File & sorts => Requests
Disk File & Printer to print
4/8/2022 18 of 52
Instructor: Mr.S.Christalin Nelson
19. Deadlock Prevention (3/8)
• Hold and Wait condition (contd.)
– Disadvantages of both solutions
• (1) Low resource utilization
– Allocated resources are unused for a long period
• (2) Starvation
– At least one resource is always allocated to another process
4/8/2022 19 of 52
Instructor: Mr.S.Christalin Nelson
20. Deadlock Prevention (4/8)
• No preemption of already allocated resources
– To deny the condition
• Solution-1: P1 requests for resources -> Some resources are
allocated to P1 & few are held by other processes -> Preempt all
resources from P1 & add to resource waiting list -> P1 restarts
after getting all resources
• Solution-2: P1 requests for resources -> Some resources are
allocated to P1 & few are held by P2 -> Check if P2 is waiting for
new resource -> If yes, preempt desired resources from P2 &
allocate to P1 -> If no, P1 waits -> During wait other acquired
resources may be preempted if requested by other process -> P1
restarts after getting all resources
– Note:
• Cannot be applied to resources (mutex locks, semaphores)
• Applied to resources whose state can be easily saved & restored
later (CPU registers, Memory space).
4/8/2022 20 of 52
Instructor: Mr.S.Christalin Nelson
21. Deadlock Prevention (5/8)
• Circular Wait
– To deny this condition
• Total ordering of all resource types & Process should request
resources in an increasing order of enumeration.
– Illustration: Let set of resource types, R = {R1, R2, ..., Rm}
» Assign unique integer no. to each resource type. i.e Define a
one-to-one function F: R→N, where N = set of natural numbers
» Compare two resources & determine whether one precedes
another in ordering.
• Function F is based on normal order of usage of system resources
– Example: If tape drive is usually needed before printer, define
F(tape drive) < F(printer)
4/8/2022 21 of 52
Instructor: Mr.S.Christalin Nelson
22. Deadlock Prevention (6/8)
• Circular Wait (contd.)
– Example
• R = {tape drives, disk drives, printers}
• Define F as follows:
– F(tape drive) = 1
– F(disk drive) = 5
– F(printer) = 12
• Solution: If a process wants tape drive & printer at same time
– Using F, it request tape drive & then printer
– Process requesting an instance of Rj must have released any
resources Ri such that F(Ri) ≥ F(Rj)
• Note: A single request is required if several instances of same
resource type are needed
4/8/2022 22 of 52
Instructor: Mr.S.Christalin Nelson
23. Deadlock Prevention (7/8)
• Circular Wait (contd.)
– “Circular wait is eliminated” - Proof by contradiction
• Assume circular wait exists
• Processes {P0, P1, ..., Pn} are involved in circular wait such that
– Pi is waiting for Ri, which is held by process Pi+1
– Pn is waiting for a resource Rn held by P0
– Pi+1 is holding resource Ri while requesting resource Ri+1
• Hence, F(Ri+1) > F(Ri) for all i
– Which means F(R0) < F(R1) < ... < F(Rn) < F(R0)
– By transitivity, F(R0) < F(R0) is impossible
• Therefore, there can be no circular wait
4/8/2022 23 of 52
Instructor: Mr.S.Christalin Nelson
24. Deadlock Prevention (8/8)
• Using with application program
– Develop an ordering of all synchronization objects in system
– Application developers write programs to ensure that
resources are acquired in proper order
– Requests for synchronization objects made in increasing order
• Verification & Warning software
– Used to check ordering & when locks are acquired out of order
and deadlock is possible
– Example: Witness
• Lock-order verifier for BSD versions of UNIX such as FreeBSD
• Uses mutual-exclusion locks to protect critical sections
• Note: Imposing lock ordering does not guarantee deadlock
prevention if locks can be acquired dynamically
4/8/2022 24 of 52
Instructor: Mr.S.Christalin Nelson
25.
26. Deadlock Avoidance Vs. Prevention
• Deadlock prevention works by limiting how requests can be
made.
– i.e Ensure at least one of necessary conditions for deadlock
cannot occur.
• Side effects of Deadlock Prevention
– Low device utilization
– Reduced system throughput
• Deadlock avoidance
– System has knowledge (additional information) of complete
sequence of requests & releases for each process & can decide
• Example: A system has one tape drive & one printer. System
knows P will request tape drive first & then printer, before
releasing both. Q will request printer first & then the tape drive
4/8/2022 26 of 52
Instructor: Mr.S.Christalin Nelson
27. Algorithms
• Approaches can differ in amount & type of information
required
• Simplest & most useful model
– Process declares required max. no. of resources of each type
• Should be <= max. resources in system
• Given this a priori information, algorithm can ensure that system
will never enter deadlocked state
• Ensure circular-wait condition does not exist by dynamic
examination of resource-allocation state
– Resource allocation state: No. of available & allocated
resources, max. demands of processes
4/8/2022 27 of 52
Instructor: Mr.S.Christalin Nelson
28. Safe & Unsafe State (1/2)
• Safe State
– System can allocate resources to each process (up to max.) in
some order & still avoid a deadlock
– Safe sequence of processes <P1, P2, ..., Pn> for current
allocation state exists
• Resource requests of Pi is satisfied by currently available
resources + resources held by all Pj and j<i
– If resources are not immediately available -> Pi waits until all Pj have
finished -> When finished, Pi obtains all its needed resources,
complete task, return allocated resources, and terminate -> When Pi
terminates, Pi+1 obtains its needed resources -> ….
4/8/2022 28 of 52
Instructor: Mr.S.Christalin Nelson
29. Safe & Unsafe State (2/2)
• Unsafe state
– Behavior of processes controls unsafe states
– OS cannot prevent processes from requesting resources in
such a way that a deadlock occurs
– May lead to a deadlocked state
• Note: Not all unsafe states are deadlocks
– Depicted as a Cycle in RAG
4/8/2022 29 of 52
Instructor: Mr.S.Christalin Nelson
30. Avoidance Algorithms
• Single instance of a resource type
– Resource-allocation graph (RAG)
• Multiple instances of a resource type
– Banker’s algorithm
• Less efficient than RAG scheme
4/8/2022 30 of 52
Instructor: Mr.S.Christalin Nelson
31. RAG Algorithm (1/2)
• Include Claim edge Pi → Rj (Notation: dashed line)
– Pi may request Rj in future
• All claim edges should be known a priori (before execution)
– Variant: Known a priori if all edges of Pi are claim edges
4/8/2022 31 of 52
Instructor: Mr.S.Christalin Nelson
32. RAG Algorithm (2/2)
• On request: Claim edge is converted to Request edge
• On allocation: Request edge is converted to Assignment
edge
– Cycle is not formed in RAG to ensure safe state
– Cycle Detection algorithm requires n2 operation for ‘n’
processes
– On release: Assignment edge is converted to Claim edge
4/8/2022
cycle
32 of 52
Instructor: Mr.S.Christalin Nelson
33. Banker’s Algorithm (1/7)
• New process entering system should declare max. no. of
instances of each resource type required
• Resource allocations occur only if safe state exists
– Else, process waits required resources are released
• Data Structures
– Encode resource-allocation state
– For 'n' processes & 'm' resources types
• Available[m] – Vector mentioning the availability of resources
• Max[n][m] – Matrix mentioning the demands of processes
• Allocation[n][m] – Matrix mentioning the allocation to processes
• Need[n][m] – Matrix mentioning the additional requirement of
processes
4/8/2022 33 of 52
Instructor: Mr.S.Christalin Nelson
34. Banker’s Algorithm (2/7)
• Data structures (contd.)
– Size & value of Data structures can vary over time
– Note:
• Consider each row of matrix as vector denoted as Allocationi &
Needi
4/8/2022
S.No. Example Remarks
1 Available[j] = k ‘k’ instances of resource type ‘Rj’ is available
2 Max[i][j] = k ‘k’ instances of resource type ‘Rj’ is required by process Pi
3 Allocation[i][j] = k ‘k’ instances of resource type ‘Rj’ is allocated to process Pi
4 Need[i][j] = k
‘k’ instances of resource type ‘Rj’ is needed by process Pi
Need[i][j] = Max[i][j] - Allocation[i][j]
34 of 52
Instructor: Mr.S.Christalin Nelson
35. Banker’s Algorithm (3/7)
• Safety algorithm
– To find if System is in Safe state
• (1) Let Work & Finish be vectors of length m & n, respectively.
Initialize Work = Available, Finish[i] = false for i = 0, 1, ..., n-1
• (2) Find an index i such that Finish[i] == false & Needi ≤ Work
– (2a) If i exists
» Work = Work + Allocationi
» Finish[i] = true, Go to next i
– (2b) Else if no such i exists
» If Finish[i] == true for all i, then system is in a safe state
– Note:
• Requires order of m x n2 operations
4/8/2022 35 of 52
Instructor: Mr.S.Christalin Nelson
36. Banker’s Algorithm (4/7)
• Resource Request algorithm
– Grant requests safely
• (1) Requesti is request vector for Pi
– Pi wants k instances of Rj if Requesti[ j] == k
• (2) Actions taken when Pi requests for resource
– (a) If Requesti ≤ Needi go to step (2a)
Else, raise error condition (Process has exceeded its max. claim)
– (b) If Requesti ≤ Available go to step (2c)
Else, Pi must wait as resources are unavailable
– (c) System pretends to allocate resources to Pi by modifying state
» Available =Available - Requesti and Needi = Needi - Requesti
» Allocationi = Allocationi + Requesti
• If resource-allocation state is safe, Pi is allocated its resources &
Transaction completed. Else Pi waits for Requesti & old resource-
allocation state restored
4/8/2022 36 of 52
Instructor: Mr.S.Christalin Nelson
37. Banker’s Algorithm (5/7)
• Example
– A system has 5 processes (P0, P1, P2, P3 & P4), and 3 resource
types (A, B, C). A has 10 instances, B has 5 instances, and C has
7 instances
– At time T0, consider system snapshot is
4/8/2022
Need?
37 of 52
Instructor: Mr.S.Christalin Nelson
38. Banker’s Algorithm (6/7)
• Example (contd.)
– Consider system is in safe state with safe sequence <P1, P3, P4,
P2, P0>
– If P1 requests 1 instance of A & 2 instances of C
• i.e. Request1 = (1,0,2). Can request be immediately granted?
– Check Request1 ≤ Available i.e. (1,0,2) ≤ (3,3,2) => true
– Pretend that this request has been fulfilled & resulting state is:
4/8/2022
Request1 = (1,0,2)
38 of 52
Instructor: Mr.S.Christalin Nelson
39. Banker’s Algorithm (7/7)
• Example (contd.)
– Check if above new system state is safe using safety algorithm
• Find safe sequence. We get <P1, P3, P4, P0, P2>. Hence,
immediately grant request of P1
– Further
• Request for (3,3,0) by P4 cannot be granted, since resources are
unavailable
• Request for (0,2,0) by P0 cannot be granted, though resources are
available but resulting state is unsafe
4/8/2022
How?
39 of 52
Instructor: Mr.S.Christalin Nelson
40.
41. All resources have single Instance (1/2)
• Convert RAG to WFG
– Remove resource nodes & collapse appropriate edges
• i.e. An edge from Pi -> Pj in WFG => Pi is waiting for process Pj to
release a resource that Pi needs
• An edge Pi → Pj exists in WFG iff corresponding RAG has 2 edges
Pi → Rq & Rq → Pj for Rq
4/8/2022 41 of 52
Instructor: Mr.S.Christalin Nelson
42. All resources have single Instance (2/2)
• Deadlock exists iff WFG contains a cycle
– System maintains WFG & periodically invoke an Cycle
detection algorithm (requires an order of n2 operations for 'n'
vertices in graph)
• Not applicable when multiple instances of each resource
type exist
4/8/2022 42 of 52
Instructor: Mr.S.Christalin Nelson
43. Several Instances of a Resource Type (1/4)
• Data structures used (Similar to Banker's algorithm Slide#33)
– For 'n' processes & 'm' resources types
• Available[m] – Vector mentioning availability of resources
• Allocation[n][m] – Matrix mentioning allocation to processes
• Request[n][m] – Matrix mentioning requests of processes
– Size & value of Data structures can vary over time
– Note:
• Consider each row of matrix as vector denoted as Allocationi &
Requesti
4/8/2022 43 of 52
Instructor: Mr.S.Christalin Nelson
44. Several Instances of a Resource Type (2/4)
• Algorithm
– (1) Let Work & Finish be vectors of length m & n, respectively
• Initialize:
– Work = Available
– For i = 0, 1, ..., n–1
» If Allocationi != 0, Finish[i] = false. Else, Finish[i] = true
– (2) Find index i such that Finish[i] == false & Requesti ≤ Work
• (2a) If i exists
– Work = Work + Allocationi
– Finish[i] = true, Go to next i
• (2b) Else if no such i exists
– If Finish[i] == false for some i, 0≤i<n, then Pi is deadlocked (System is
in deadlocked state)
– Note: Requires order of m x n2 operations
4/8/2022 44 of 52
Instructor: Mr.S.Christalin Nelson
45. Several Instances of a Resource Type (3/4)
• Example
– A system has 5 processes (P0, P1, P2, P3 & P4), and 3 resource
types (A, B, C). A has 7 instances, B has 2 instances, and C has 6
instances
– At time T0, consider resource allocation state (RAS)
• System is not in deadlocked state as Safe sequence <P0, P2, P3,
P1, P4> results in Finish[i] == true for all i
4/8/2022 45 of 52
Instructor: Mr.S.Christalin Nelson
46. Several Instances of a Resource Type (4/4)
• Example (contd.)
– P2 makes additional request for 1 instance of C. The modified
Request matrix is
• System is deadlocked (consists processes P1, P2, P3, and P4)
– Though resources held by P0 can be reclaimed, no. of available
resources is insufficient to fulfill requests of other processes
4/8/2022 46 of 52
Instructor: Mr.S.Christalin Nelson
47. Usage (1/2)
• Factors to decide invocation of deadlock detection
– (1) How frequent is a deadlock likely to occur?
• If frequent -> Invoke detection algorithm frequently
– (2) How many processes will be affected by deadlock when it
happens?
• Resources allocated to deadlocked processes will be idle until
deadlock can be broken
• No. of processes involved in deadlock cycle may grow
• Deadlocks occur when a process makes a request (final
request makes a cycle) that cannot be granted immediately
– Deadlocked processes is a link in the cycle
– If there are many different resource types, one request may
create many cycles in the resource graph
4/8/2022 47 of 52
Instructor: Mr.S.Christalin Nelson
48. Usage (2/2)
• (Extreme) Invoke deadlock detection every time a request
for allocation cannot be granted immediately
– Deadlocked set of processes & specific process that “caused”
deadlock can be identified
• Invoke deadlock-detection for every resource request
– Considerable overhead in computation time
• Invoke deadlock-detection at defined intervals at arbitrary
points in time (once per hr. or whenever CPU utilization
drops below 40%)
– Resource graph may contain many cycles
– The deadlocked processes which “caused” deadlock cannot be
identified
4/8/2022 48 of 52
Instructor: Mr.S.Christalin Nelson
49.
50. Process Termination
• Abort all deadlocked processes
• Abort one process at a time until the deadlock cycle is
eliminated
• In which order should we choose to abort?
– 1. Priority of the process
– 2. How long process has computed, and how much longer to
completion
– 3. Resources the process has used
– 4. Resources process needs to complete
– 5. How many processes will need to be terminated
– 6. Is process interactive or batch?
4/8/2022 50 of 52
Instructor: Mr.S.Christalin Nelson
51. Resource Preemption
• Selecting a victim – minimize cost
• Rollback – return to some safe state, restart process for that
state
• Starvation – same process may always be picked as victim,
include number of rollback in cost factor
4/8/2022 51 of 52
Instructor: Mr.S.Christalin Nelson