Answer:
See explaination
Explanation:
Here we will use two semaphore variables to satisfy our goal
We will initialize s1=1 and s2=1 globally and they are accessed by all 3 processes and use up and down operations in following way
Code:-
s1,s2=1
P1 P2 P3
P(s1)
P(s2)
C1
V(s2) .
P(s2). .
. C2
V(s1) .
P(s1)
. . C3
V(s2)
Explanation:-
The P(s1) stands for down operation for semaphore s1 and V(s1) stands for Up operation for semaphore s1.
The Down operation on s1=1 will make it s1=0 and our process will execute ,and down on s1=0 will block the process
The Up operation on s1=0 will unblock the process and on s1=1 will be normal execution of process
Now in the above code:
1)If C1 is executed first then it means down on s1,s2 will make it zero and up on s2 will make it 1, so in that case C3 cannot execute because P3 has down operation on s1 before C3 ,so C2 will execute by performing down on s2 and after that Up on s1 will be done by P2 and then C3 can execute
So our first condition gets satisfied
2)If C1 is not executed earlier means:-
a)If C2 is executed by performing down on S2 then s2=0,so definitely C3 will be executed because down(s2) in case of C1 will block the process P1 and after C3 execute Up operation on s2 ,C1 can execute because P1 gets unblocked .
b)If C3 is executed by performing down on s1 then s1=0 ,so definitely C2 will be executed now ,because down on s1 will block the process P1 and after that P2 will perform up on s1 ,so P1 gets unblocked
So C1 will be executed after C2 and C3 ,hence our 2nd condition satisfied.