hm1233
hm1233

Reputation: 25

Interchange two forked process with semaphores

I am trying to write a program that interchanges two forked children processes with semaphores but I am not sure how to do that. For example, I have child1 and child2 and both of them call the same function. I want them to go through the function once at a time, if I run it 10 times, I want it to look like child1 child2 child1 child2 child1 child2 child1 child2 child1 child2. I am also using shared memory that stores the semaphores that I have.

Upvotes: 0

Views: 54

Answers (1)

FuxTheFox
FuxTheFox

Reputation: 99

Use 2 Semaphore, first initialised with one and the other to 0.

Process child1 :

consume by 1 semaphore 1
do action
increase by 1 semaphore 2

Process child2 :

consume by 1 semaphore 2
do action
increase by 1 semaphore 1

Upvotes: 1

Related Questions