Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Chapter 2: Process Management

2.5. Deadlock - Excercise

GV: Nguyễn Thị Thanh Vân


Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

Methods for Handling Deadlocks

 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection

Operating System Concepts – 10th Edition 5a.2 Silberschatz, Galvin and Gagne ©2018

1
System Model

 System consists of resources


 Resource types R1, R2, . . ., Rm
• CPU cycles, memory space, I/O devices
 Each resource type Ri has Wi instances.
 Each process utilizes a resource as follows:
• Request: The thread requests the resource
• Use: The thread can operate on the resource
• Release: The thread releases the resource.

Operating System Concepts – 10th Edition 5a.3 Silberschatz, Galvin and Gagne ©2018

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, …, Pn} 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
• Pn is waiting for a resource that is held by P0.

Operating System Concepts – 10th Edition 5a.4 Silberschatz, Galvin and Gagne ©2018

2
Avoidance Algorithms
 Single instance of a resource type
• Use a modified resource-allocation graph
 Multiple instances of a resource type
• Use the Banker’s Algorithm
 Ensure the system is safe

Operating System Concepts – 10th Edition 5a.5 Silberschatz, Galvin and Gagne ©2018

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 in 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 < I
Need [i,j] = Max[i,j] – Allocation [i,j]
Available= Available + Allocation [i,j]
 That 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

Operating System Concepts – 10th Edition 5a.6 Silberschatz, Galvin and Gagne ©2018

3
Ex0
 Consider a system with 12 magnetic tape drives and 3 processes P1, P2, P3.
• Requires maximum tape drives: P1: 10, P2: 4; P3: 9
• Suppose that at time t1: P1 is holding 5; P2 is holding 2; P3 is holding 3.
At time t1, system is in:
• A.safe state
• B. unsafe state
• C. deadlocked state
• D. starvation state
 Expl Max Holding Need
• 2 tape are free. P1 10 5 5
• P2. execute, free 4 tape P2 4 2 2
• 4 tape can not fulfill for P1 or P3 P3 9 3 6
• option (B) is correct.

Operating System Concepts – 10th Edition 5a.7 Silberschatz, Galvin and Gagne ©2018

Ex1

 Consider the following snapshot of a system:

 Answer the following questions using the banker’s algorithm:


• a. What is the content of the matrix Need?
• b. Is the system in a safe state?
• c. If a request from process P1 arrives for (0,4,2,0), can the request be
granted immediately?

Operating System Concepts – 10th Edition 5a.8 Silberschatz, Galvin and Gagne ©2018

4
Ex1
 Use the Banker’s Algorithm
Work
1520
1532
----
2886
2 14 11 8
2 14 12 12
3 14 12 12 (P1)

 (P0,P2,P3,P4,P1) is safe => the system is in a safe state.


Chú ý: Còn các chuỗi an toàn khác, tổng cộng 36 chuỗi an toàn trong trường hợp này.

Operating System Concepts – 10th Edition 5a.9 Silberschatz, Galvin and Gagne ©2018

Ex1
 P1 request (0,4,2,0).
• Need(P1) = (0, 7, 5, 0)

Work (P0, P2, P3, P4, P1) is safe


1100  P1 Request (0, 4, 2, 0) is
11 12 granted immediately.
----
2466 Chú ý: Còn các chuỗi an toàn
2 10 9 8 khác, tổng cộng 4 chuỗi an toàn
2 10 10 12 trong trường hợp này.
3 14 12 12
Operating System Concepts – 10th Edition 5a.10 Silberschatz, Galvin and Gagne ©2018

5
Ex2

 Consider the following snapshot of a system:

 Using the banker’s algorithm, determine whether or not each of the


following states is unsafe. If the state is safe, illustrate the order in which
the processesmay complete.Otherwise, illustratewhy the state is unsafe.
• a. Available = (0, 3, 0, 1)
• b. Available = (1, 0, 0, 2)

Operating System Concepts – 10th Edition 5a.11 Silberschatz, Galvin and Gagne ©2018

Ex3
 Consider the following snapshot of a system:

 Answer the following questions using the banker’s algorithm:


• a. Illustrate that the system is in a safe state by demonstrating an order in which
the processes may complete.
• b. If a request from process P1 arrives for (1, 1, 0, 0), can the request be
granted immediately?
• c. If a request from process P4 arrives for (0, 0, 2, 0), can the request be
granted immediately?

Operating System Concepts – 10th Edition 5a.12 Silberschatz, Galvin and Gagne ©2018

6
Ex4

 Giải thuật Banker - Điền số liệu (có giải thích) vào các bảng sau. Có tồn tại
chuỗi cấp phát an toàn hay không ?. Cho biết tài nguyên ban đầu của hệ
thống.

Allocation Request Available Need


R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5

P1 1 1 1 1 1 4 2 2 3 3 1 2 1 1 1
p2 1 1 0 1 1 2 2 2 2 3
P3 0 0 0 1 1 3 1 1 4 1
P4 1 2 1 1 0 2 4 4 3 2
P5 1 0 0 0 1 2 1 1 1 1
P6
0 2 1 0 1 2 3 2 1 2

Operating System Concepts – 10th Edition 5a.13 Silberschatz, Galvin and Gagne ©2018

Test_1

 What is the minimum number of resources required to ensure that


deadlock will never occur, if there are currently three processes P1, P2 and
P3 running in a system whose maximum demand for the resources of
same type are 3, 4, and 5 respectively
• a. 3
• b. 9
• c. 7
• d. 10
 Expl:
• Let the resources needed by P1: R1 =3, P2: R2 = 4, P3: R3=5
• Minimum resources required to ensure that deadlock will never occur:
(R1-1) + (R2-1) + (R3-1) + 1 = (3-1) + (4-1)+ (5-1) + 1 = 10.
• Option (D) is correct.

Operating System Concepts – 10th Edition 5a.14 Silberschatz, Galvin and Gagne ©2018

7
Test_2

 Suppose n processes, P1, …. Pn share m identical resource units, which


can be reserved and released one at a time. The maximum resource
requirement of process Pi is Si, where Si > 0. Which one of the following is
a sufficient condition for ensuring that deadlock does not occur?

 Expl: In the extreme condition, all processes acquire Si -1 resources and


need 1 more resource. So following condition must be true to make sure
that deadlock never occurs.
𝑛 𝑛

෍(𝑆𝑖 −1) < 𝑚 » => ෍ 𝑆𝑖 < (𝑚 + 𝑛)


𝑖=1 𝑖=1

• Answer (c)

Operating System Concepts – 10th Edition 5a.15 Silberschatz, Galvin and Gagne ©2018

Test_3
 A system shares 9 tape drives. The current allocation and maximum
requirement of tape drives for 4 processes are shown below:

 Which of the following best describes the current state of the system?
• A. Safe, Deadlocked
• B. Safe, Not Deadlocked
• C. Not Safe, Deadlocked
• D. Not Safe, Not Deadlocked
 Expl: 9 tapes and allocated (3+1+3+0 =) 7 => available tapes are 9 - 7 = 2.
• P1,2 require: not satisfied
• P3 requires 2 tapes => available=5
• P2 requires 5 tapes => available= 6
• P1 requires 6 tapes => available= 9.
• P4 requires 10 => not satisfied
• (C) is correct.
Operating System Concepts – 10th Edition 5a.16 Silberschatz, Galvin and Gagne ©2018

8
Test_4

 An operating system contains 3 user processes each requiring 2 units of


resource R. The minimum number of units of R such that no deadlocks will
ever arise is
• a. 3
• b. 4
• c. 5
• d. 6
 Expl:
• If we give 1 resource to 1 process then total resource = 1 + 1 + 1 = 3 but in this
case deadlock will definitely occur because each process hold 1 unit resource
and waiting for another resource so if we increase 1 more resource (3+1=4)
then deadlocks will ever arise (i.e., when process 1 complete their execution
then it free 2 resource and this 2 resource will used by another process.).
• Option (B) is correct

Operating System Concepts – 10th Edition 5a.17 Silberschatz, Galvin and Gagne ©2018

Test_5
 Consider the following snapshot of a system:

 The minimum value of x for the above system to be in a safe state is


• 1
• 2
• 3
• Not safe for any x
 Expl:
• x = 1 process D will execute, free 1 1 2 2 1. None of the other process will execute.
• x = 2 process D will execute and free 1 1 3 2 1. (x=3: free 1 1 4 2 1)
 process C will execute and free 2 2 3 3 1 instances.
 process B will execute, but process A will not execute because 5 resources needs 2
instances (2 last columns) which will never be satisfied.
• That's why system is not in safe state. So, option (D) is correct.

Operating System Concepts – 10th Edition 5a.18 Silberschatz, Galvin and Gagne ©2018

9
Ex1, using Resource allocation graph

 Is system deadlock

Operating System Concepts – 10th Edition 5a.19 Silberschatz, Galvin and Gagne ©2018

Ex2, Modified Resource allocation graph


 Consider the following snapshot of a system. The system contains a total
of 2 instances of A, 1 of resource B, 1 of resource C, 2 resource D and 1 of
resource E.

 Do the following problems:


• Convert this matrix representation to a resource allocation graph.
• Use the deadlock detection algorithm to determine whether the system contains
a deadlock.
• Which processes are involved in the deadlock?
• While you are use the deadlock detection algorithm, add and remove directed
edges of the resource allocation graph.

Operating System Concepts – 10th Edition 5a.20 Silberschatz, Galvin and Gagne ©2018

10
Ex2
 unsatisfied “requests”

 P0 request, run, Available= [1,0,1,1,0]+[2,1,1,2,1]= [3,1,2,3,1].

 P0 has new request (so far), the new graph is


obtained by removing all request edges and allocation edges

Operating System Concepts – 10th Edition 5a.21 Silberschatz, Galvin and Gagne ©2018

Ex2
 P1’s Request = [0,0,1,0,1]. Run
Available = [3,1,2,3,1]+[1,1,0,0,0] = [4,2,2,3,1].

 Next, we run P2. Update:

 Finally, P3 can run and return all of its resources.


Because all involved processes are finished, this
system does no have a deadlock.

Operating System Concepts – 10th Edition 5a.22 Silberschatz, Galvin and Gagne ©2018

11
Ex3
 Consider the following resource allocation graph

 Do the following problems:


• (a) Convert it to the matrix representation (i.e., Allocation, Request and Available).
• (b) Do a step-by-step execution of the deadlock detection algorithm. For each step,
add and remove the directed edges, and redraw the resource allocation graph.
• (c) Is there a deadlock? If there is a deadlock, which processes are involved?

Operating System Concepts – 10th Edition 5a.23 Silberschatz, Galvin and Gagne ©2018

12

You might also like