我目前正在为之前的迭代的OS考试进行培训,我遇到了这样的情况:
实现了"N进程屏障“,即确保一组进程中的每个进程在其各自执行的某个点上等待其他进程到达其给定点。
您可以使用以下操作:
init(sem,value), wait(sem) and signal(sem)
N是一个任意数。我可以使其适用于给定数量的进程,但不适用于任何数量的进程。
有什么想法吗?可以用伪代码回复,这不是作业,只是个人学习。
发布于 2011-06-13 23:21:16
这在The Little Book of Semaphores中得到了很好的体现。
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
发布于 2015-12-18 18:13:21
使用N个信号量。不太确定..。
semaphore barr[N]
semaphore excl=1
int count=0
int i=1
while (i<=N)
barr[i]=0 #initialization
i=i+1
# use, each thread (tid)
wait(excl)
count=count+1
if (count==N)
int j=1
while (j<=N)
signal(barr[j])
j=j+1
count=0
signal(excl)
wait(barr[tid])
发布于 2015-12-18 18:30:39
只有2个屏障信号量,但不确定...
semaphore barr[0..1] # two semaphores: barr[0] and barr[1]
semaphore excl=1
int count=0
int whichOne=0 # select semaphore to avoid race conditions
barr[0]=0 #initialization
barr[1]=0
# sample use
int current #local for each thread
wait(excl)
current=whichOne
count=count+1
if (count==N)
int j=1
while (j<=N)
signal(barr[current])
j=j+1
count=0
whichOne=1-whichOne # swap barrier to avoid race conditions
signal(excl)
wait(barr[current])
https://stackoverflow.com/questions/6331301
复制相似问题