多线程 – 信号量P和V操作是否为原子?

可以在信号量上执行的P()和V()操作是否保证原子?信号量可以防止两个进程进入P()?

解决方法

假设我们有一个二进制信号量s,其值为1,两个进程同时尝试执行P on.只有其中一个操作才能在下一个V操作之前完成;尝试执行P操作的其他进程被暂停.

取自我的大学笔记:

We can think if P and V as controlling
access to a resource:

When a process wants to use the
resource,it performs a P operation:
if this succeeds,it decrements the
amount of resource available and the
process continues; if all the
resource is currently in use,the
process has to wait.

When a process is finished with the
resource,it performs a V operation:
if there were processes waiting on the
resource,one of these is woken up;
if there were no waiting processes,
the semaphore is incremented
indicating that there is now more of
the resource free. Note that the
definition of V doesn’t specify which
process is woken up if more than one
process has been suspended on the same
semaphore.

信号量可以解决互斥和条件同步问题.所以你的问题的答案是:是的.

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。