加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:18:08 所属栏目:Java 来源:网络整理
导读:可以在信号量上执行的P()和V()操作是否保证原子?信号量可以防止两个进程进入P()? 解决方法 假设我们有一个二进制信号量s,其值为1,两个进程同时尝试执行P on.只有其中一个操作才能在下一个V操作之前完成;尝试执行P操作的其他进程被暂停. 取自我的大学笔记:
可以在信号量上执行的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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读