本文介绍了使用信号量实现 N 个进程屏障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在为之前迭代的操作系统考试进行培训,我遇到了这个问题:
I'm currently training for an OS exam with previous iterations and I came across this:
实施N 流程屏障",即是,确保每个过程出来一群人在等待,在某些点在其各自的执行,对于其他进程达到他们的给定点.
你有以下可用的操作:
init(sem,value)、wait(sem)和signal(sem)
N 是一个任意数字.我可以使它适用于给定数量的进程,但不适用于任何数量.
N is an arbitrary number. I can make it so that it works for a given number of processes, but not for any number.
有什么想法吗?用伪代码回复就可以了,这不是作业,只是个人学习.
Any ideas? It's OK to reply with the pseudo-code, this is not an assignment, just personal study.
推荐答案
这在信号量小书.
n = the number of threads
count = 0
mutex = Semaphore(1)
barrier = Semaphore(0)
mutex.wait()
count = count + 1
mutex.signal()
if count == n: barrier.signal() # unblock ONE thread
barrier.wait()
barrier.signal() # once we are unblocked, it's our duty to unblock the next thread
这篇关于使用信号量实现 N 个进程屏障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!