使用信号量实现N个过程障碍 [英] Implementing an N process barrier using semaphores

查看:125
本文介绍了使用信号量实现N个过程障碍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以前的迭代训练OS考试,并且碰到了这一点:

I'm currently training for an OS exam with previous iterations and I came across this:

实施"N个过程障碍",即 是,请确保每个流程都已完成 他们中的一组在等待 指出其各自的执行情况, 其他过程达到他们的 给定点.

Implement a "N Process Barrier", that is, making sure that each process out of a group of them waits, at some point in its respective execution, for the other processes to reach their given point.

您具有以下内容 可用的操作:

You have the following ops available:

init(sem,value), wait(sem) and 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个过程障碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆