Hack Forums
[C] processes and semaphores order of excecution valid? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] processes and semaphores order of excecution valid? (/showthread.php?tid=6128588)



processes and semaphores order of excecution valid? - georgeziozas - Dec 20, 2020

🙃Hey there,

Process A:

Code
down(s1);
up(s2);


Process B:

Code
down(s2);
down(s2);
up(s1);
up(s2);

Semaphores initilization:

Code
s1=2  , s2=0

If we consider the order of execution:
Α1->Α2->Β1->Α3->Β2 meaning A1 starts first and finishes then follow B1 with the same pattern etc.
Then the code is valid and this order of execution can be achieved, at least from my knowledge.

But, What if we have this order of execution?
Α1->Α2->Β1->Β2->Α3

In my opinion and the way I solve it, is that it is not possible at all because B2 will be paused and won't be able to finish (cause S2 will be -1).

Am I correct ? or I am missing crucial information on semaphores inner workings?

Thanks in advance.✌


RE: processes and semaphores order of excecution valid? - SIM-3364521 - Jan 3, 2021

Yes, you are correct. In this order there would occur a problem:
A1->A2->B1->B3->A3

if you go through it step by step:

initial values: S1=2, S2=0
after A1: S1=1, S2=1
after A2: S1=0, S2=2
after B1: S1=1, S2=1

so in B2 it comes to a stop at the 2nd down(s2), to resolve this another A1 would need to be called.

I would suggest you to write a small program that allows you to model different execution flows, that way you can let the computer test
the case for you to validate it :)


RE: processes and semaphores order of excecution valid? - Juice - Jan 19, 2021

Use the down command bro that should work