1 - Consider the traffic deadlock depicted in the following Figure.
a. Show that the four necessary conditions for deadlock indeed hold in this example.
b. State a simple rule for avoiding deadlocks in this system.
a. 互斥:“路面上的每個位置”資源只能由一輛車占有;
占有並等待:任何一輛車都占有當前位置,並且等待前方位置,不會讓出;
非搶占:調度者無法從某個位置移除一輛車;
循環等待:從圖中可以明顯看出,所有車的等待資源構成了一個環形。
b. 可以消滅占有並等待條件。規定當一輛車將匯入某位置時,該位置不能有另一輛車也將匯入。即需要申請該位置,並且僅有一輛車可以分配到使用權。
2 - Consider the deadlock situation that could occur in the dining-philosophers problem when the philosophers obtain the chopsticks one at a time. Discuss how the four necessary conditions for deadlock indeed hold in this setting. Discuss how deadlocks could be avoided by eliminating any one of the four conditions.
該情景下,死鎖是可能發生的。考慮四個必要條件。互斥:“筷子”資源只能由一個哲學家占有;占有並等待:哲學家在獲得筷子后會占有該筷子,並等待另一只筷子,而不會主動釋放筷子;非搶占:不能從哲學家手上強行奪走筷子;循環等待:哲學家的“筷子等待鏈”顯然可能構成環形。
可以消滅占有並等待條件。規定哲學家在等待另一只筷子一段時間后,主動釋放手上的一只筷子。
3 - Compare the circular-wait scheme with the deadlock-avoidance schemes (like the banker’s algorithm) with respect to the following issues:
a. Runtime overheads
b. System throughput
應用死鎖避免方法會增加運行時間代價,因為需要追蹤資源分配情況並執行一定的算法。但進行死鎖避免后,系統的並發性更好,比起循環等待的死鎖預防方法可以支持更多的並發任務。從這個角度來看,系統的吞吐量增加了。
4 - In a real computer system, neither the resources available nor the demands of processes for resources are consistent over long periods (months). Resources break or are replaced, new processes come and go, new resources are bought and added to the system. If deadlock is controlled by the banker’s algorithm, which of the following changes can be made safely (without introducing the possibility of deadlock), and under what circumstances?
a. Increase Available (new resources added).
b. Decrease Available (resource permanently removed from system)
c. Increase Max for one process (the process needs more resources than allowed, it may want more)
d. Decrease Max for one process (the process decides it does not need that many resources)
e. Increase the number of processes.
f. Decrease the number of processes.
a. 增加可用實例數量Available,顯而易見不會帶來新的安全問題;
b. 減少可用實例數量Available可能導致某些進程分配不到請求的資源;
c. 進程需要更多最大資源Max,可能導致其分配不到足夠量的資源;
d. 進程減少最大資源需求Max,顯而易見不會帶來新的安全問題;
e. 增加進程數量時,只要當給該進程分配資源后,系統仍處於安全狀態,就不會帶來問題;
f. 減少進程數量,顯而易見不會帶來新的安全問題。
5 - Consider a system consisting of m resources of the same type, being shared by n processes. Resources can be requested and released by processes only one at a time. Show that the system is deadlock free if the following two conditions hold:
a. The maximum need of each process is between 1 and m resources
b. The sum of all maximum needs is less than m + n
由a知道\(1≤Max_i≤m,∀i\)
由b知道\(∑_n Max_i <m+n\),即\(∑_n(Need_i+Allocation_i)<m+n\)
假設發生死鎖,則至少有\(∑_n Allocation_i =m\)
則\(∑_n Need_i <n\),這意味着存在一個進程\(P_j\),滿足\(Need_j=0\)。又\(Max_j≥1\),則\(Allocation_j≥1\)。只要這樣的進程釋放已Allocate的資源,在經歷若干輪資源重新分配后,即可解除死鎖狀態。
6 - 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 threads may complete. Otherwise, illustrate why the state is unsafe.
a. Available = (2, 2, 2, 3); b. Available = (4, 4, 1, 1)
c. Available = (3, 0, 1, 4); d. Available = (1, 5, 2, 2)
7 - 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?
b. 不能。如果立即滿足該請求,則初始Available變為(0, 0, 0, 0),系統陷入不安全狀態。
c. 不能。如果立即滿足該請求,則初始Available變為(2, 1, 1, 4),系統陷入不安全狀態。
d. 不能。如果立即滿足該請求,則初始Available變為(0, 0, 1, 2),系統陷入不安全狀態。