I have below code with semaphore. I would like to know how does the semaphore work here ? who will get the semaphore every time ?
semaphore my_sem;
my_sem = new(1);
fork
forever begin
my_sem.get();
my_process1(); // Let's say this process will take 10 cycles to complete
my_sem.put();
end
forever begin
my_sem.get();
my_process2(); // Let's say this process will ALSO take 10 cycles to complete
my_sem.put();
end
join
Semaphore requests are taken in FIFO order, so the two processes will alternate. Which process gets the first request is a race condition. Once version of a tool might always pick the first or second process, but you should not spend on it.
Do you mean the which process picked up at the first time is kind of race-condition. But if it’s determined, then it will select alternately afterwards ? ( like round-robin ?)
Hi Dave,
You mentioned that the first time is a race-condition. After that it will be round-robin.
Since statements run parallelly in fork join.
Is it possible that two processes will be picked up randomly like this (T0:my_process1 , T1:my_process1,T2:my_process1,T3:my_process0…)?
Thank you