2011-05-15 82 views
3

對於那些不熟悉,下面是用於處理協調Peterson算法:併發處理 - Peterson算法

int No_Of_Processes; // Number of processes 
int turn; // Whose turn is it? 
int interested[No_Of_Processes]; // All values initially FALSE 

void enter_region(int process) { 
int other; // number of the other process 

other = 1 - process; // the opposite process 
interested[process] = TRUE; // this process is interested 
turn = process; // set flag 
while(turn == process && interested[other] == TRUE); // wait 
} 

void leave_region(int process) { 
interested[process] = FALSE; // process leaves critical region 
} 

我的問題是,可以在此算法產生死鎖?

+0

該死的,我找到了一個蠢貨,但它被關閉了一個不是真正的問題(什麼?),並且配方很差。所以我不會投票結束。 – 2011-05-15 13:21:32

回答

1

不,沒有死鎖可能。 您正在等待的唯一地點是while循環。而線程之間不共享變量process,它們是不同的,但turn變量是共享的。因此,對於turn == process而言,在每個時刻多於一個線程無法獲得true條件。 但無論如何,你的解決方案是不正確的,彼得森的算法只適用於兩個併發線程,而不是像你的代碼中的任何No_Of_Processes。 在原始算法N進程死鎖是可能的link