线程被信号量唤醒后返回的线程在哪里?
我对信号原理的理解
我目前正在尝试了解信号量的工作方式。 我已经了解到,当调用P(SEM)时,如果SEM = 0,则螺纹将被阻止,否则信号量的值将减小,线程将其放入关键部分。 当调用V(SEM)时,信号量值将增加,如果SEM = 0并且线程在等待,则该线程被唤醒。
现在考虑这个问题:
两个线程正在运行,thread1运行函数1,thread2运行函数2。两个线程之间共享两个信号量S = 0和M = 1。
function1 {
while(true) {
P(s)
P(s)
P(m)
print("a")
V(m)
}}
function2 {
while(true) {
P(m)
print("b")
V(m)
V(s)
}}
我期望发生的事情
我期望输出字符串是打印b的,而A则按一定顺序进行。
线程开始。假设thread1首先输入函数1:
step1: p(s) - > s = 0因此阻止线程
Thread2进入功能2
p(m) - > m = 1->设置M = M-1 = 0
打印B
v(m) - > M = M+1 = 1
v(s) - > s = 0,螺纹为wating->集s = s+1 = 1,然后唤醒线程
step2: thread2返回 second p-statement
thread1在函数中继续1
p(s) - > s = 1->设置S = S-1 = 0
p(m) - > m = 1->设置M = M-1 = 0
打印
v(m) - > m = 0,但没有人等待 - >设置M = M+1 = 1
step2: thread1 runction1再次运行
p(s) - > s = 0->块
Thread2运行功能2
p(m) - > m = 1->设置M = M-1 = 0
打印B v(m) - > M = M+1 = 1
v(s) - > s = 0和线程等待 - >唤醒线程2
step3: thread1返回函数1第二p-statement
p(s) - > s = 1->设置S = S-1 = 0
p(m) - > m = 1->设置M = M-1 = 0
打印
v(m) - >设置M = 1
step4 thread2运行功能2
p(m) - > m = 1->设置M = 0
打印B v(m) - >设置M = 1
v(s) - > s = 0->设置s = 1,没有线程等待
step5 螺纹2连接功能2再次
打印B
...等等
问题/问题
我不确定,如果是正确的话,当Thread2运行函数2时,Thread1在被唤醒后返回第二个p-statement2。
在我看来似乎是错误的,因为如果我考虑通常如何实现信号量:
P(s)
* do something *
V(s)
如果在p-statement之后返回何处,则信号量的价值不会降低,并且V-Statement将增加信号量错误的价值为2。
但是,如果在示例中重复第一个p-statement,则意味着输出字符串仅为b。
有人可以告诉我我的理解是否正确,或者如果不正确,请纠正我的错误?
My understanding of Semaphore principle
I am currently trying to understand how Semaphores work.
I have understood that when calling P(sem), if sem=0 the thread will get blocked, otherwise the value of the semaphore will be reduced and the thread let into the critical section.
When calling V(sem), the semaphore value will be increased, if sem=0 and a thread is waiting, that thread is woken up.
Now consider this problem:
Two threads are running, thread1 runs function1, thread2 runs function2. There are two semaphores s=0 and m=1 which are shared between both threads.
function1 {
while(true) {
P(s)
P(s)
P(m)
print("a")
V(m)
}}
function2 {
while(true) {
P(m)
print("b")
V(m)
V(s)
}}
What I expected would happen
I have expected the output string to be print b's and a's in some random order.
The threads start. Let's say thread1 enters function1 first:
Step1: P(s)-> s=0 so block the thread
thread2 enters function2
P(m) -> m=1 -> set m=m-1=0
print b
V(m) -> m=m+1=1
V(s) -> s=0 and thread is wating -> set s=s+1=1 and wake up thread
Step2: Thread2 returns to the second P-statement
thread1 continues in function1
P(s) -> s=1 -> set s=s-1=0
P(m) -> m=1 -> set m=m-1=0
print a
V(m) -> m=0 but noone waiting -> set m=m+1=1
Step2: Thread1 runs function1 again
P(s) -> s=0 -> block
Thread2 runs function 2
P(m) -> m=1 -> set m=m-1=0
print b
V(m) -> m=m+1=1
V(s) -> s=0 and thread waiting -> wake up thread 2
Step3: Thread1 returns to function1 second P-statement
P(s) -> s=1 -> set s=s-1=0
P(m) -> m=1 -> set m=m-1=0
print a
V(m) -> set m=1
Step4 Thread2 runs function 2
P(m) -> m=1 -> set m=0
print b
V(m) -> set m=1
V(s) -> s=0 -> set s=1, no thread waiting
Step5 Thread 2 runs function 2 again
print b
... and so on
The Problem/The Questions
I am very unsure, if it is correct, that thread1 returns to the second P-statement after it is woken up when thread2 runs function2.
It seems wrong to me, because if I consider how a semaphore would usually be implemented:
P(s)
* do something *
V(s)
If it where to return after the P-statement, the value of the semaphore does not get decreased, and the V-statement would increase the semaphore to a wrong value of 2.
But if it repeats the first P-statement in the example, that would mean the output string is only b's.
Can someone tell me if my understanding is correct or if not, correct my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论