使用单个信号量的多进程同步
我们正在参加的课程中介绍多线程编程。教授提出了一个额外的问题,我一直在尝试解决这个问题,但没有成功:
每个进程 P0、P1、P2 和 P3 都必须等待其他三个进程交叉或到达其代码中的特定同步点,并且只有这样该进程才能跨越它自己的同步点。
我已经知道如何用四个信号量来回答这个问题,困难的部分是只用一个信号量来回答这个问题。
关于如何进行的任何建议或提示?
We're covering multithreaded programming in a class I'm taking. The professor offered a bonus question that I have been trying, to no avail, to figure out:
Each of processes P0, P1, P2 and P3 have to wait for the other three to cross or reach a particular synchronization point in their code, and only then may that process cross its own synchronization point.
I already know how to answer the question with four semaphores, the hard part is doing it with only one semaphore.
Any suggestions or hints as to how to proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对解决方案所施加的限制不太了解,但请参阅 The Little Book of Semaphores 并阅读通过障碍部分。这应该会给你一些想法。
You are a little light on the constraints imposed on your solution but see The Little Book of Semaphores and read through the sections on barriers. That should give you some ideas.
如果信号量不是二进制信号量,只需将其初始化为 -4 即可。
Just initialize your semaphore at -4 if it's not a binary one.
原来教授的意思是说你可以使用两个信号量而不是一个。正如我在思考这个问题一段时间后所做的那样,他认为用单个信号量是不可能做到的。
Turns out the professor had meant to say that you could use two semaphores instead of one. He believes, as I do after having thought about the problem for a while, that it is impossible to do with a single semaphore.