两个进程之间的障碍
我想在两个进程之间创建一个屏障。为此,我使用了两个信号量。当进程 1 到达屏障时,它通过发布第一个信号量向进程 2 发出信号。进程 2 收到信号后,发布第二个信号量作为确认。
代码看起来像这样...
Proc1:
sem_post( &sem_sig );
sem_wait( &sem_ack );
Proc2:
sem_wait( &sem_sig );
sem_post( &sem_ack );
现在我的问题是这种方法是否是最有效的或者是否有更好的技术来实现两个进程之间的进程级屏障?
I want to create a barrier between two processes. For that purpose, I have used two semaphores. When Process 1 reaches the barrier, it signals Process 2 by posting the first semaphore. Process 2 on receiving the signal, posts the second semaphore as an acknowledgment.
The code looks something like this...
Proc1:
sem_post( &sem_sig );
sem_wait( &sem_ack );
Proc2:
sem_wait( &sem_sig );
sem_post( &sem_ack );
Now my question is if this method is the most efficient or is there any better technique to implement process level barriers between two processes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Linux 实现了
pthread_barrier_t
。对我来说,它看起来非常适合您的需求。对于对pthread_barrier_init
的调用,您只需指定此屏障是进程共享的。Linux implements
pthread_barrier_t
. To me it looks like a perfect fit to your needs. For the call topthread_barrier_init
you'd just have to specify that this barrier is process shared.