Example 1: Counting Semaphore
Let capacity of critical section is 3 i.e. S.value = 3 (four process can enter Critical Section).
1. First P1 try to enter into its critical section, it initiated Wait() function and before it go to critical section, run line 1 (S.value = S.value - 1) , now S.value = 2. Then run line 2 (if (S.value < 0), not satisfied so, process go to critical section and continue.
2. Now P2 come, first run line 1 (S.value = S.value - 1), S.value = 1 and enter into critical section because line 2 (if (S.value < 0)) not satisfied.
3. Now P3 come, first run line 1 (S.value = S.value - 1), S.value = 0 and enter into critical section because line 2 (if(S.value < 0) not satisfied.
4. Now P4 come, first-run line 1 (S.value = S.value-1), S.value = -1 and then line 2 (if (S.value < 0) is satisfied. Not allowed the process to enter into critical section (critical section is full). Put the process P4 at the suspended queue list and sleep (block) the process P4.
# In counting semaphore increment and decrement signified by wait and signal operation.
But it can be identified two more ways up and down or V and P.
Q. At a particular time, the value of a counting semaphore is 10, it will become 7 after
a. 3V
b. 3P
c. 5V & 2P
d. 13p & 10V
Which one is correct:
A. a, b
B. b, c
C. b, d
D. d
Answer:
Counting semaphore = 10. After which operation it will be 7.
Case 1:
One thing is very clear that counting semaphore variable value reduce from 10 to 7 means it will be a 3P or Down operation.
10 – 3 = 7 so, after 3P operation it will become 7. Option B correct
Case 2:
If the counting semaphore variable reduces from 10 to 7 means it will be a V or Up operation along with a P or Down operation or vice-versa but in such a way that it reduces by 3.
It will be possible by only option D. (13P and 10V)
First 13P operation: 10 – 13 = -3, then 10V operation: -3 + 10 = 7
So, Correct answer: C. b, d