信号量 P 和 V 操作是原子操作吗?
可以在信号量上执行的 P() 和 V() 操作是否保证原子操作?信号量可以防止两个进程进入 P() 吗?
Are the P() and V() operations that can be performed on a semaphore guarantee atomic? Can a semaphore prevent two processes getting into the P()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设我们有一个二进制信号量 s,其值为 1,并且两个进程同时尝试在 s 上执行 P。这些操作中只有一个能够在 s 上的下一个 V 操作之前完成;尝试执行 P 操作的另一个进程被挂起。
摘自我的大学笔记:
信号量可以解决互斥和条件同步问题。所以你的两个问题的答案都是:是的。
Suppose we have a binary semaphore, s, which has the value 1, and two processes simultaneously attempt to execute P on s. Only one of these operations will be able to complete before the next V operation on s; the other process attempting to perform a P operation is suspended.
Taken from my university notes:
Semaphores can solve both mutual exclusion and condition synchronization problems. So the answer to both your questions is: yes.
如果我没记错的话,是的。它们需要确保一个线程无法在另一个线程获取资源时获得资源。如果不是,则意味着两个线程可以开始访问资源,然后被切换出 CPU,而另一个进程可以访问该资源。这会严重扰乱事情。
有关详细信息,请参阅此处:http://en.wikipedia.org/wiki/Semaphore_ (编程)#Semantics_and_Implementation
If I recall correctly, yes they are. They are required to be to ensure that one thread cannot obtain resources while another thread does. If it wasn't, this would imply that two threads could begin accessing a resource and then be switched out of the CPU and another process could gain access to it instead. This would distrupt things quite a bit.
See here for for more info: http://en.wikipedia.org/wiki/Semaphore_(programming)#Semantics_and_Implementation