F. *_* P. 19 computer-science operating-system semaphore systems-programming barrier
我目前正在接受以前迭代的OS考试培训,我遇到了这个:
实现"N进程障碍",即确保一组中的每个进程在其各自的执行中的某个时刻等待其他进程到达其给定点.
您有以下操作:
init(sem,value), wait(sem) and signal(sem)
N是任意数.我可以使它适用于给定数量的进程,但不适用于任何数字.
有任何想法吗?用伪代码回复是可以的,这不是一项任务,只是个人学习.
cni*_*tar 39
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
Run Code Online (Sandbox Code Playgroud)