简体   繁体   English

如何防止死锁?

[英]How do I prevent a deadlock?

I've been struggling with a synchronization problem for a while. 我一直在努力解决同步问题。 I've got a process A, this is the main one, and 3 processes of type B. These processes have to signal A and wait for response. 我有一个进程A,这是主要的进程,还有3个类型B的进程。这些进程必须向A发出信号并等待响应。

A is waiting for all 3 signals. A正在等待所有3个信号。 When they all arrive, A does it's job and signals all B processes back so they can continue their work. 当他们全部到达时,A便完成了工作,并向B发出了所有信号,以便他们继续工作。

A:
   if(/*all signals received*/)  {
     //do my job
     //signal all Bs
   }

B:
   //some code
   signal(A);
   pause();  //wait for response
   //Do other stuff

When B calls signal(A) , A does his job, signals B back, then schedules B again causing B to call pause() and wait for a signal indefinitely. 当B调用signal(A) ,A做他的工作,向B发信号,然后再次调度B,使B调用pause()并无限期地等待信号。 I don't know how to prevent this deadlock. 我不知道如何防止这种僵局。

Is there any other way to achieve this? 还有其他方法可以做到这一点吗?

Consider using semaphore as IPC synchronization, 考虑使用信号量作为IPC同步,
any process want to send signal have to lock the semaphore, 任何想要发送信号的过程都必须锁定信号量,
and after receiveing A's return, unlocking it so next process can proceed. 并在收到A的退货后,将其解锁,以便继续进行下一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM