Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

processes and semaphores order of excecution valid?

🙃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.✌
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 :)
Use the down command bro that should work


Possibly Related Threads...
Thread / Author Replies Views Last Post
by ByteDoktorFeb 26, 2025 01:24 PM 0 146 Feb 26, 2025 01:24 PM
Last Post: ByteDoktor
[C] by georgeziozasDec 20, 2020 02:00 PM 0 142 Dec 20, 2020 02:00 PM
Last Post: georgeziozas
[C#] by aesopfablesSep 16, 2020 06:38 AM 4 268 Sep 16, 2020 06:38 AM
Last Post: aesopfables