1
Operating System Homework 7 & 8
Jinyan Xu, 3160101126, Information Security
8.12 Consider the traffic deadlock depicted in Figure 8.11.
a. Show that the four necessary conditions for deadlock hold in this example.
b. State a simple rule for avoiding deadlocks in this system.
Answer:
a. Regard roadways as resources and cars as processes.
Mutual Exclusion: Only one line of cars at a time can use the roadway.
Hold-and-wait: Each line of cars holds one roadway and wait for the next roadway.
No preemption: The roadway can’t be released until the whole line of cars have passed it.
Circular wait: There’re 4 lines of cars, L1 waits for L2, L2 waits for L3, L3 waits for L4, L4 waits for L1. b. Each car detects the intersection before entering, if it can’t pass, it will wait until the
intersection is free.
8.18 Which of the six resource-allocation graphs shown in Figure 8.12 illustrate deadlock?
For those situations that are deadlocked, provide the cycle of threads and resources. Where there is not a deadlock situation, illustrate the order in which the threads may complete execution.
Answer:
Note: I added a resource to R2 in graph (f).
Graph (a): No deadlock, possible order of thread execution: T2, T1, T3. Graph (b): Deadlock, because there is a cycle: T1→R3→T3→R1→T1.
L1
L2 L3
L4
2
Graph (c): No deadlock, possible order of thread execution: T2, T3, T1.
Graph (d): Deadlock, there’re two cycle: T1→R2→T3→R1→T1, T2→R2→T4→R1→T2.
Graph (e): Possible deadlock, there’re two cycles: T1→R2→T4→R1→T1, T3→R2→T4→R1→T3, if T2 releases R2, the deadlock can be avoided.
Graph (f): Possible deadlock, the cycle is: T1→R2→T3→R1, if T2 or T4 release R2, the deadlock can be avoided.
8.22 Consider a system consisting of four resources of the same type that are shared by three threads, each of which needs at most two resources. Show that the system is deadlock free.
Answer:
In any case, there is always a thread can have 2 resources, which means this thread will release its resources in bounded time, so the other threads which wait for the resource can always get what they want. In conclusion, this system is deadlock free.
8.23 Consider a system consisting of m resources of the same type being shared by n threads. A thread can request or release only one resource at a time. Show that the system is deadlock free if the following two conditions hold:
a. The maximum need of each thread is between one resource and m resources.
b. The sum of all maximum needs is less than m + n.
Answer:
From a & b, we can get this: 1 ≤ ∑𝑛𝑖=1𝑀𝑎𝑥𝑖≤ 𝑚 + 𝑛.
Consider deadlock happens: ∑𝑛 𝐴𝑙𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛𝑖
𝑖=1 = 𝑚.
In this case, 1 ≤ ∑𝑛𝑖=1𝑁𝑒𝑒𝑑𝑖+ ∑𝑛𝑖=1𝐴𝑙𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛𝑖= ∑𝑛𝑖=1𝑁𝑒𝑒𝑑𝑖+ 𝑚 = ∑𝑛𝑖=1𝑀𝑎𝑥𝑖≤ 𝑚 + 𝑛.
So, 1 − m ≤ ∑𝑛𝑖=1𝑁𝑒𝑒𝑑𝑖 ≤ 𝑛, there must exist some threads that their needs are 0. For these threads, when they finish, it can release at least 1 resource. So, the other threads waiting for the resource can always get the resource they need. In conclusion, this system is deadlock free.
8.28 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 threads may complete.
b. If a request from thread T4 arrives for (2, 2, 2, 4), can the request be granted immediately?
c. If a request from thread T2 arrives for (0, 1, 1, 0), can the request be granted immediately?
d. If a request from thread T3 arrives for (2, 2, 1, 2), can the request be granted immediately?
3
Answer:
a. The content of the matrix Need:
Both T2 & T3 can run. Let T2 run first.
Then, the remains threads can run in any order, one possible order is: T2, T0, T1, T3, T4. So, the system is in safe state.
b. A request from thread T4 arrives for (2, 2, 2, 4).
Then the available matrix is equal to 0, neither thread can run. So, the system is in unsafe state, which means this request can’t be granted immediately.
c. A request from thread T2 arrives for (0, 1, 1, 0).
Threads can run in this order: T2, T3, T0, T1, T4. So, the system is in safe state, which means this request can be granted immediately.
d. A request from thread T3 arrives for (2, 2, 1, 2).
Threads can run in this order: T3, T0, T1, T2, T4. So, the system is in safe state, which means this request can be granted immediately.