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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With