Search code examples
multithreadingsemaphoremonitor

Semaphore minimization


I stumbled upon a problem in a multi-threading book in the library and I was wondering what I would need to do in order to minimize the number of semaphores.

What would you do in this situation?

Semaphores


Solution

  • Assume a process P0's execution depends on other k processes: P1,...,Pk. You need one semaphore to synchronize the processes and to satisfy this single constrain.

    The semaphore S0 is initialized with 0, while P0 will try to wait k times on S0 (in other word, it will try to acquire k resources).

    Each of k processes P1, ..., Pk will release S0 upon their ends of executions.

    This will guarantee that P0 will start execution only after all the other k processes complete their execution (in any order and asynchronously).

    In the link you provided, you need 4 semaphores, T1 does not need any semaphore because its execution depends on nobody else.