首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并发处理--Peterson算法

并发处理--Peterson算法
EN

Stack Overflow用户
提问于 2011-05-15 21:16:47
回答 1查看 640关注 0票数 3

对于那些不熟悉的人,下面是用于流程协调的Peterson算法:

代码语言:javascript
复制
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
}

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

EN

回答 1

Stack Overflow用户

发布于 2011-05-15 21:22:39

不,不可能出现死锁。你唯一等待的地方就是while循环。线程之间不共享process变量,它们是不同的,但turn变量是共享的。因此,不可能在每个时刻都为多个线程获取turn == processtrue条件。但是不管怎样,你的解决方案是根本不正确的,彼得森的算法只适用于两个并发线程,而不是像你的代码中的任何No_Of_Processes。在N进程的原始算法中,死锁是可能的link

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6008622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档