2017-04-03 116 views
1

我理解使用搶佔來避免死鎖的過程,但我很難將它放入代碼中。搶先避免死鎖

我搶佔避免死鎖使用線程T1,T2資源W,X,Y是理解:

thread t1 -> has resource X. request resource W

thread t2 -> has resources W, Y. request resource X

t1 checks if resource W is free. if it's not free, don't acquire resource X or release it.

t2 executes. Releases resources W, Y, X

t1 executes

我應該如何去實現的東西就像我在C上述++?任何幫助表示讚賞。

回答

2

一種方式是通過編號資源(看7.4.4 here):

w = 1 
y = 2 
x = 3 

然後一個簡單的原則:一個線程只能在遞增的順序獲取資源。

換句話說,爲了請求資源Rj,進程必須首先釋放所有Ri,使得i> = j。

線程t1 - >有資源X(值爲3)。請求資源W(值爲1)==>它需要釋放X第一

螺紋t2 - >具有資源W(值爲1),Y(值爲2)。請求資源X(值爲3)==>在t1發佈後立即獲得它