Implementing an N process barrier using semaphores

后端 未结 3 1542
耶瑟儿~
耶瑟儿~ 2020-12-02 17:16

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

Implement a "N Process Barrier", that is, making sure t

3条回答
  •  一整个雨季
    2020-12-02 17:56

    This is well presented in 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
    

提交回复
热议问题