0% found this document useful (0 votes)
32 views

Slide 04

The document discusses various topics related to operating systems and critical sections, including questions about race conditions, Peterson's solution, conditions for resolving critical sections, semaphores, and deadlocks. It provides definitions and examples to explain these concepts, such as the four conditions required for deadlock and methods for handling deadlocks like prevention, detection, and avoidance.

Uploaded by

phong hai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Slide 04

The document discusses various topics related to operating systems and critical sections, including questions about race conditions, Peterson's solution, conditions for resolving critical sections, semaphores, and deadlocks. It provides definitions and examples to explain these concepts, such as the four conditions required for deadlock and methods for handling deadlocks like prevention, detection, and avoidance.

Uploaded by

phong hai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 101

Operating System

Nguyen Tri Thanh


[email protected]

1
Review
Which is correct about race condition?
A. Happen even when there is only once
process
B. Happen when multiple processes use a
shared resource concurrently
C. Happen when multiple processes use a
resource sequentially
D. Happen when there are multiple processes in
the system

2
Review
Which is incorrect about the Peterson’s
solution?
A. It satisfies all the conditions of critical section
B. It is easy to control even the number of
processes is above 2
C. It is difficult to control
D. It is complicated when the number of
processes is above 2

3
Review
Which of the following is the most correct
about critical section?
A. A code snippet that operates on a global
variable
B. A code snippet that operates on a resource
C. A code snippet that operates on a global
resource
D. A code snippet that operates on a shared
resource

4
Review
How many conditions for resolving
critical section are there ?
A. 1
B. 2
C. 3
D. 4

5
Review
Which is incorrect about the conditions of
critical section?
A. The progress condition utilizes the resource
effectively
B. The exclusive condition removes race
condition
C. The exclusive condition ensures processes to
use a shared resource sequentially
D. The bounded waiting condition allows a
process to use a shared resource several
consecutive times 6
Question
Which is the purpose of the second condition of
critical section?
A. It reduces the waiting time of requested
processes
B. It ensures the correct use of the shared
resource
C. It makes the algorithm more complicated to
implement
D. It makes the algorithm less complicated to
implement
7
Question
Which is the purpose of the third condition of
critical section?
A. It supports the priority of processes
B. It ensures the correct use of the shared
resource
C. It utilizes the shared resource effectively
D. It makes sure no process is in its critical
section forever

8
Review
Which is incorrect about the semaphore?
A. Semaphore is an implementation of critical
section
B. Semaphore does not guarantee the
conditions of critical section
C. A semaphore usually includes an integer
variable
D. Semaphore has atomic operators

9
Review
How many types the semaphore are there?
A. 1
B. 2
C. 3
D. 4

10
Review
Which of the following is correct about
counting semaphore?
A. The value of the semaphore is 0 or 1
B. The same as binary semaphore
C. The value of the semaphore variable can be
above 1
D. The value of the semaphore variable can
never be below 0

11
Review
Which of the following is the most suitable
use for counting semaphore?
A. Use for shared resources with a single
instance
B. Use for shared resources with 2 instances
C. Use for shared resources with any instances
D. Use for shared resources with multiple
instances

12
Deadlock

13
Objectives
 Introduce what a deadlock is
 Introduce methods of handling deadlocks
 Implement deadlock handling algorithms

14
Reference
 Chapter 7 of Operating System Concepts

15
Deadlock examples

16
Deadlock examples

17
Deadlock examples

18
Deadlock examples

19
Definition of deadlock
 A set of blocked processes each
 holding a resource and
 waiting to acquire a resource held by another
process in the set
 There must be a circular wait in this set

20
Deadlock example (cont’d)
 Process A:  Process B
{ {
… …
Lock file F1; Lock file F2;
... ...
Open file F2; Open file F1;
… …
Unlock F1; Unlock F2;
} }
21
Question
When does the deadlock happen?
A. A gets F1 and waits for F2

B. A gets F2 and waits for F1 and


B waits for F1
C. A gets F1 and waits for F2 and
B gets F2 and waits for F1
D. A gets F1 and F2 and
B waits for F2

22
Deadlock Characterization
 Deadlock can arise if four conditions hold
simultaneously
 C1: Mutual exclusion
 C2: Hold and wait holding one resource, waiting
other resources held by another
 C3: No preemption only process has right to
release its holding resources
 C4: Circular wait there exists a set {P0, P1, …, Pn}
of processes:
 P0 is waiting for a resource that is held by P1,
 P1 is waiting for a resource that is held by P2, …
 Pn is waiting for a resource that is held by P0.
23
System Model
 Resource types R1, R2, . . ., Rm
 shared variables, memory space, I/O devices,
 Each resource type Ri has Wi instances.
 Each process utilizes a resource as
follows:
 request
 use
 release

24
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 Pi  Rj
 assignment edge – directed edge Rj  Pi

25
Resource-Allocation Graph
(Cont'd)
 Process

 Resource Type with 4 instances

 Pi requests instance of Rj Pi
Rj

 Pi is holding an instance of Rj Pi
Rj

26
Example of a RAG

27
RAG With A Deadlock

 When P3 asks for R2


 There are two cycles
 P1R1P2R3P3R2
P1
 P2R3P3R2P2
 Set of P1, P2, P3 is
deadlock

28
RAG With A Deadlock

 When P3 asks for R2


 There are two cycles
 P1R1P2R3P3R2
P1
 P2R3P3R2P2
 Set of P1, P2, P3 is
deadlock

29
RAG With A Deadlock

 When P3 asks for R2


 There are two cycles
 P1R1P2R3P3R2
P1
 P2R3P3R2P2
 Set of P1, P2, P3 is
deadlock

30
Graph With A Cycle But No
Deadlock

31
Graph With A Cycle But No
Deadlock

32
Basic Facts
 If graph contains no cycles  no deadlock.
If graph contains a cycle 
 if only one instance per resource type, then
deadlock.
 if several instances per resource type, possibility
of deadlock.

33
Deadlock handling

34
Methods for Handling Deadlocks
 Ensure that the system will never enter a
deadlock state
 Deadlock prevention, deadlock avoidance
 Allow the system to enter a deadlock state and
then recover
 Deadlock detection and recovery
 Ignore the problem and pretend that deadlocks
never occur in the system
 used by most operating systems, including UNIX.
35
Deadlock Prevention

 The method prevents at least one of the four


deadlock conditions from occurring
 This method is classified as a static method

36
Deadlock Prevention
 C1: Mutual Exclusion
 In some situations, this condition is required
 Not feasible to make this NOT to happen

37
Deadlock Prevention
 C2: Hold and Wait
 Solution
 must guarantee that whenever a process requests a
resource, it does not hold any other resources, or
 require process to request and be allocated all its
resources before it begins execution
 low resource utilization; starvation possible.

38
Deadlock Prevention (Cont'd)
 C3: No Preemption
 If a process holding some resources requests
another resource that cannot be immediately
allocated to it,
 then all resources currently being held are released
 released 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 and the new requesting ones

39
Deadlock Prevention (Cont'd)
 C4: Circular Wait
 impose a total ordering of all resource types and
 require that each process requests resources in an
increasing order of enumeration
 Let R = {R1, R2, ..., Rm} with increasing instances
 i.e., F(Ri)>F(Rj) if (i>j)
 A process holding a resouce Rj request Ri, then i>j
 If i<j then it must release some resource Rj
 It must request all instances of Rj at once

40
Question
How many conditions for a dead lock to happen are
there?
A. 2

B. 3

C. 4

D. 5

41
Question
When does a deadlock happen?
A. any of the 4 conditions occur

B. any two of the 4 conditions occur

C. any 3 of the 4 conditions occur

D. all the 4 conditions occur

42
Deadlock avoidance

43
Deadlock Avoidance
 This method requires additional information to
decide resource allocation so that deadlock
will not happen
 each process has to register the number of each
required resource types as additional information
 The deadlock-avoidance algorithm
dynamically examines the resource-allocation
state to ensure that there can never be a
circular-wait condition
44
Deadlock Avoidance
 Deadlock avoidance algorithms check the
state of resource-allocation to decide
allocation
 Resource-allocation state is defined by the
number of available and allocated resources,
and the maximum demands of the processes

45
Safe State
 System is in safe state if a sequence <P1, P2,
…, Pn> of ALL the processes exists
 Pi can be satisfied by currently available
resources + resources held by all the Pj, with j < i
 processes terminate in the above order

46
Graph With A Cycle But No
Deadlock
+ P2, P1, P3, P4
+ P4, P2, P3, P1
+ P4, P3, P2, P1
+ P2, P4, P3, P1
+ P2, P4, P1, P3
...

47
Graph With A Cycle But No
Deadlock
+ P2, P1, P3, P4
+ P4, P2, P3, P1
+ P4, P3, P2, P1
+ P2, P4, P3, P1
+ P2, P4, P1, P3
...

48
Basic Facts
 If a system is in safe state  no deadlocks
 If a system is in unsafe state  possibility of
deadlock
 Avoidance  ensure that a system will never
enter an unsafe state

49
Safe, Unsafe , Deadlock State

50
Avoidance algorithms
 Single instance of a resource type
 Use a resource-allocation graph
 Multiple instances of a resource type
 Use the banker’s algorithm

51
RAG Algorithm convention
 Claim edge Pi  Rj
 process Pj may request resource Rj
 presented as a dash line
 Claim edge converts to request edge when a
process requests a resource
 Request edge becomes an assignment edge
when the resource is assigned to it
 When a resource is released by a process,
assignment edge reconverts to a claim edge
 Resources must be claimed a priori in the system.
52
Resource-Allocation Graph

53
Unsafe State In
Resource-Allocation Graph

54
Resource-Allocation Graph
Algorithm
 Suppose that process Pi requests Rj
 The request can be granted only if
 converting the request edge to an assignment
edge does not result in a cycle in the RAG

55
Deadlock avoidance
For multi-instance resources

56
Example
 A system has 12 tapes, and 3 processes P0,
P1, P2 with corresponding requests:
 P0 requests at most 10 tapes
 P1 requests at most 4 tapes
 P2 requests at most 9 tapes
 At t0, P0 has 5 tapes, P1 and P2 each has 2
tapes
 3 tapes available
 Is the system safe?
57
Banker’s Algorithm
 Multiple instances
 Each process must a priori claim maximum use
 When a process requests a resource it may
have to wait
 When a process gets all its resources, it must
return them in a finite amount of time

58
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types

 Available: Vector of length m


 Available [j] = k: there are k instances of resource
type Rj available
 Max: n x m matrix - Claim matrix
 Max [i,j] = k: Pi may request at most k instances of Rj
 Allocation: n x m matrix
 Allocation[i,j] = k: Pi is allocated k instances of Rj

59
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types

 Need: n x m matrix - remainder


 Need[i,j] = k: Pi may need k more instances of Rj
 Need [i,j] = Max[i,j] – Allocation [i,j]
 Let Work and Finish be vectors of length m and
n, respectively
 Let A=(A1, A2, …, An), B=(B1, B2, …, Bn)
 Define A  B if only if Ai Bi , 1  i  n

60
Safety/Banker’s Algorithm
1. Initialize
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2.Find an i that satisfies both
(a) Finish [i] = false
(b) Need[i]  Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4.If Finish [i] == true for all i, then the system is
in a safe state
61
Question
Which of the following is correct about the
Work variable in the algorithm?
A. It stores the available resources when each
process finishes
B. It is a redundant variable
C. It stores the state of the system
D. It stores possible resources for each process

62
Question
Which of the following is the most correct
about banker’s algorithm?
A. it detects the state of the processes
B. it detects the deadlock state of the system
C. it detects the safe sequence of the system
D. it detects the available resources

63
Example of Banker’s Algorithm
 5 processes: P0 - P4; 3 resource types
 A (10 instances), B (5 instances), and C (7
instances)
 At time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
64
Example (Cont'd)
 Matrix Need = Max – Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
 The system is in a safe state or not?
 sequence < P1, P3, P0, P2, P4> satisfies the safety
criteria. 65
Example
A system has 12 tapes, and 3 processes P0, P1,
P2 with corresponding requests:
Max request Current Allocation
P0 10 5
P1 4 2
P2 9 2
 At t0, the system is in safe state
 The sequence <P1, P0, P2> is a safe sequence
 At t1, P2 requests 1 more tape
 Is it possible to grant resource to this request?

66
Resource-Request Algorithm
 Resource-request algorithm
 another algorithm to avoid unsafe state
 Additional data structure
 Request = request vector for process Pi
 Requesti [j] = k: process Pi wants k instances of Rj

67
Resource-Request Algorithm
1. 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 – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti; => call banker’s algorithm
 If safe  the resources are allocated to Pi (exit)
 If unsafe  Pi must wait, and the old resource-allocation
state is restored
68
Example
A system has 12 tapes, and 3 processes P0, P1,
P2 with corresponding requests:
Max request Current Allocation
P0 10 5
P1 4 2
P2 9 2
 At t0, the system is in safe state
 The sequence <P1, P0, P2> is a safe sequence
 At t1, P2 requests 1 more tape (is it safe?)
 the system is in unsafe state
 it is wrong to allocate a tape for P2
69
Question
Which of the following is correct about
resource-request algorithm?
A. it detects the unsafe state of the system
B. it detects the deadlock state of the system
C. it detects the safe sequence of the system
D. it detects the safe sequence of the system if
the request is granted

70
Example: P1 Request (1,0,2)
 Request1  Available ((1,0,2)  (3,3,2)  true)
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 1 600
P3 2 1 1 011
P4 0 0 2 431
 < P1, P3, P0, P2, P4> is a safe sequence
 Can request for (1,0,0) by P4 be granted?
 Can request for (0,2,0) by P0 be granted?
71
Deadlock detection

72
Deadlock Detection
 Allow system to enter deadlock state

 Use detection algorithms

 Recover from deadlock

73
Single Instance of Each
Resource Type
 Maintain wait-for graph
 Nodes are processes
 Pi  Pj if Pi is waiting for Pj
 Periodically invoke an algorithm that searches
for a cycle in the graph
 If there is a cycle, there exists a deadlock
 An algorithm to detect a cycle in a graph
requires an order of n2 operations
 where n is the number of vertices in the graph
74
Resource-Allocation Graph and
Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

75
Several Instances of a
Resource Type
 Available: A vector of length m
 number of available resources of each type
 Allocation: An n x m matrix
 number of resources of each type currently allocated
to each process
 Request: An n x m matrix
 current request of each process
 If Requesti [j] = k, then process Pi is requesting k
more instances of resource type Rj

76
Detection Algorithm
Let Work and Finish be vectors of length m and
n, respectively
1. Initialize:
(a) Work = Available
(b) For i = 0,2, …, n-1,
if Allocationi  0 OR Requesti  0, then Finish[i] = false;
otherwise, Finish[i] = true.
2.Find an index i such that both
(a) Finish[i] == false
(b) Requesti  Work
If no such i exists, go to step 4. 77
Detection Algorithm (Cont'd)
3.Work = Work + Allocationi
Finish[i] = true
go to step 2

4. If Finish[i] == false, for some i, 0  i  n-1,


then the system is in deadlock state
 Moreover, if Finish[i] == false, then Pi is in the
deadlock set.

Algorithm requires an order of O(m x n2) operations to detect


whether the system is in deadlocked state.

78
Example of Detection Algorithm
 Processes P0 - P4; resources (numbers)
 A (7), B (2), and C (6)
 Snapshot at time T0 (deadlock?)
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, P1, P3, P4> will result in
Finish[i] = true for all i.
79
Example (Cont'd)
 P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
 State of system (deadlock? processes in deadlock?)
 Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes
 Deadlock exists, consisting of processes P1, P2, P3, and P4
80
Example (Cont'd)
 P2 requests an additional instance of type C
Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
 State of system (deadlock? processes in deadlock?)
 Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes
 Deadlock exists, consisting of processes P1, P2, P3, and P4
81
Question
 Which of the following is correct about
deadlock detection algorithm?
A. it only detects the unsafe state of the system
B. all the processes in the system are in the deadlock
when it detects a deadlock
C. it can only detect the deadlock not the processes
involved in the deadlock
D. it can detect deadlock as well as the involved
processes

82
Detection-Algorithm Usage
 When, and how often, to invoke depends on:
 How often a deadlock is likely to occur?
 How many processes will need to be rolled back?
 one for each disjoint cycle
 If detection algorithm is invoked arbitrarily
 there may be many cycles in the resource graph
 would not be able to tell which of the many
deadlocked processes “caused” the deadlock.

83
Recovery from Deadlock

84
Recovery from Deadlock

85
Recovery from Deadlock

86
Recovery from Deadlock

87
Recovery from Deadlock

88
Recovery from Deadlock

89
Recovery from Deadlock

90
Recovery from Deadlock

91
Recovery from Deadlock:
Process Termination

 Abort all deadlocked processes


 Abort each process until the deadlock is removed
 In which order should we choose to abort?
 Priority of the process.

 How long process has computed, and/or how much


longer to completion.
 Resources the process has used.

 Resources process needs to complete.

 How many processes will need to be terminated.

 Is process interactive or batch?

92
Recovery from Deadlock:
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

93
Discussion

 For each abort condition, discuss which process


will be selected to be cancelled
 Priority of the process
 How long process has computed, and how much
longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?

94
Discussion

 For each abort condition, discuss which process


will be selected to be cancelled
 Priority of the process
 How long process has computed, and how much
longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?

95
Discussion

 For each abort condition, discuss which process


will be selected to be cancelled
 Priority of the process
 How long process has computed, and how much
longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?

96
Discussion

 For each abort condition, discuss which process


will be selected to be cancelled
 Priority of the process
 How long process has computed, and how much
longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?

97
Discussion

 For each abort condition, discuss which process


will be selected to be cancelled
 Priority of the process
 How long process has computed, and how much
longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?

98
Discussion

 For each abort condition, discuss which process


will be selected to be cancelled
 Priority of the process
 How long process has computed, and how much
longer to completion
 Resources the process has used
 Resources process needs to complete
 How many processes will need to be terminated
 Is process interactive or batch?

99
End of chapter

100
Question? 101

You might also like