Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing an N process barrier using semaphores

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 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 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.

like image 283
F. P. Avatar asked Jun 13 '11 13:06

F. P.


People also ask

What is a barrier semaphore?

A barrier is similar to a semaphore, but it's meant to be a one-off, well, barrier. A barrier is preconfigured to accept n threads. Its API is defined by b. wait() , where a thread waits until n-1 other threads are also waiting on b , an only then are the threads allowed to continue.

How can you implement process synchronization using semaphores?

Semaphores are integer variables that are used to solve the critical section problem by using two atomic operations, wait and signal that are used for process synchronization. The wait operation decrements the value of its argument S, if it is positive. If S is negative or zero, then no operation is performed.

What are semaphores How do they implement mutual exclusion?

A semaphore is a shared variable which is used to implement mutual exclusion between system processes. It is mainly helpful to solve critical section problems and is a technique to achieve process synchronization.


1 Answers

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 
like image 179
cnicutar Avatar answered Sep 21 '22 18:09

cnicutar