0
votes

Assume you have a parent process which forks two children process.

You want the three processes to do something in order (first one child, then the other child, then the parent) until a stop condition is reached.

Semaphores must be used to achieve this sequential order.

My question is how to go about switching between the three processes. In other words, how do you identify that a given process is running and use the semaphores to ensure that no process runs twice in a row or is skipped. I understand that a semaphore can be used to "block" until the signal is reached, but I don't get how I can use that concept to make my three processes go in order.

1
what have you tried so far? can you share your sample implementation of this behavior? - catzilla
Have you used a semaphore in a producer-consumer pattern yet? Consider what happens if you make three producer-consumer queues with semaphores. - nneonneo

1 Answers

0
votes

First you have to make some shared memory. So that if you change something in that memory then that will reflect to all process..Now then its time to use sem_wait(&mutex) and sem_post(&mutex) to get your desired result..If you don't get then let me know I will provide some code snippet so that you can easily understand...