对于那些不熟悉的人,下面是用于流程协调的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
}我的问题是,这个算法会导致死锁吗?
发布于 2011-05-15 21:22:39
不,不可能出现死锁。你唯一等待的地方就是while循环。线程之间不共享process变量,它们是不同的,但turn变量是共享的。因此,不可能在每个时刻都为多个线程获取turn == process的true条件。但是不管怎样,你的解决方案是根本不正确的,彼得森的算法只适用于两个并发线程,而不是像你的代码中的任何No_Of_Processes。在N进程的原始算法中,死锁是可能的link。
https://stackoverflow.com/questions/6008622
复制相似问题