Answer should be: A, B, D
No way it’s a Mutex problem since the Capacity of the Semaphore, S is 5
Hence, at any given point of time, total 5 processes can encounter to get into the Critical Section and that’s the property of Counting Semaphore.
Now, if we go option-by-option not by looking at the program too much (since, that’d be the time efficient way to solve here) –
Option A: The value of the counter is 5 after all the threads successfully complete the execution. Yes, possible. Why even 5, it can be any value 6, 7, 10. 50, whatever it wants if all the processes follow a sequential order to execute the Critical section. Here, it’s given in the question that, 5 threads are processing the block, so yes – if they process sequentially, then Counter value can be 5 at the end. So, Option A is correct.
Option B: The value of the Counter is 1 after all the threads successfully complete the execution. Yes, possible.
Because, this ‘Counter++;’ is a line of code which is actually 3 lines of machine code should look like –
Line #1: reg = Counter;
Line #2: reg = reg + 1;
Line #3: Counter = reg;
Now, let’s say Process, P1 is the first process which went inside the critical section and executed only Line #1
and after that. gets preempted by the CPU. So, when Process P1 will wake up later, it will only remember reg = 0 and from there, it’ll start executing again from Line #2
.
Now, let’s say – when this Process P1 is in preempted mode and sleeping, other of processes came and made the Counter value as 4 (Because leaving P1, there should be other 4 processes since in the question, it’s given 5 threads are executing the parop())
But when P1 will wake up, it doesn’t make any sense to Process P1 and P1 will still remember only one thing – My reg value is 0 and from there it’ll start executing and will finally make Counter value as 1 and will come out of the Critical Section.
Hence, final value of the Counter as 1 is possible. Option B is also correct.
Option C: The value of the Counter is 0 after all the threads successfully completed. No, never ever possible.
Even a single process also completes all the steps successfully, then minimum value would be 1, but never 0
Option D: There is a Deadlock involving all the steps. Yes, possible.
Let’s say –
Process P1 came and execute the first W(S); (Line #5 in the question
) and gets preempted by CPU. So, current updated value of Semaphore, S = 4
Then, Process P2 came and execute the first W(S); (Line #5 in the question
) and gets preempted by CPU. So, current updated value of Semaphore, S = 3
Then, Process P3 came and execute the first W(S); (Line #5 in the question
) and gets preempted by CPU. So, current updated value of Semaphore, S = 2
Then, Process P4 came and execute the first W(S); (Line #5 in the question
) and gets preempted by CPU. So, current updated value of Semaphore, S = 1
Then, Process P5 came and execute the first W(S); (Line #5 in the question
) and gets preempted by CPU. So, current updated value of Semaphore, S = 0
So, the capacity of Semaphore, S is NULL now. No further process from outside can execute the first W(S)
Also, even any of these 5 processes which are already invoked, if wakes up or, even more than one process wake up, they can’t make any further progress.
Because, there is no Semaphore, S left to execute the second W(S) (Line #6 in the question
)
So, Deadlock is possible following this manner.
Hence, Option D is also valid