3

对于不熟悉的人,以下是用于流程协调的 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
}

我的问题是,这个算法会导致死锁吗?

4

1 回答 1

1

不,不可能出现死锁。您唯一等待的地方是while循环。并且process变量在线程之间不共享并且它们是不同的,但是turn变量是共享的。因此,不可能在每一刻都获得一个以上线程的true条件。turn == process但是无论如何,您的解决方案根本不正确,彼得森的算法仅适用于两个并发线程,而不适用No_Of_Processes于您的代码中的任何类似线程。在N进程的原始算法中,死锁是可能的链接

于 2011-05-15T13:22:39.050 回答