Can you please help me with the PSEUDO CODE for this problem? Thank you in advan
ID: 3831403 • Letter: C
Question
Can you please help me with the PSEUDO CODE for this problem? Thank you in advance.
Processes 0, 1, 2, 3 are executing concurrently. Process 3 must execute AFTER processes P0, P1, P2 finish their execution. There is no enforcement of order between the executions of Process0, Process 1, Process2. c) Implement the required synchronization using minimum number of binary semaphores. Give the initial value of each semaphore. d) Implement the required synchronization using a minimum number of counting semaphore. Give the initial value of each semaphore.Explanation / Answer
we will take 3 binary semaphores for the 3 processes p0,p1 and p2.Let them be a,b and c.
Initially a=0,b=0,c=0
process p0 process p1 process p2
----------- ------------------- ---------------------
code c0;//execute the code code c1;execute code code c2;execute code
signal(a);//increment a value,now a=1 signal(b);//increment b value,b=1 signal(c);//increment cvalue,c=1
process p3
----------------
wait(a);
wait(b);
wait(c);
code c3;
process p0,p1 and p2 will execute first and increment the value of binary semaphores to value 1
process p3,if it tries to execute before p0,p1 and p2 will try to decrement a,b,c and gets blocked as the value is 0.
Therefore p3 will not get executed until p0,p1 and p2 are executed,in this way the order of execution is maintained by binary semaphores.
---------------------------------------
counting semaphores contain a non zero value greater than 1.The count indicated the number of processes that can be executed simultaneously.
suppose a counting semaphore count is intialized to zero.
process p0 process p1 process p2
--------------- ------------------ ----------------------
code c1; code c2; code c3;
signal(count);
//count becomes 1 signal(count); signal(count);
//count becomes 2 //count becomes3
note:counting semaphores are not used to implement mutual exclusion,they are used to implement multiple threads execution in the critical section
process p3
--------------
while(count!=3){ ;//do nothing}
code c3;
so when process p0 p1 and p2 complete their execution the count becomes 3,then only the process p3 begins its execution otherwise it gets blocked.In this way order can be maintained.