@Avinash Choudhary
Because here we’ve to design the process flow such a way, that we can maximize the value of X. Yes, of course we can consider Process X after Process W, but if we do so, we would not be able to maximize the value of X.
So, to maximize the value of X, steps would be –
Step 1: Process W arrives and makes the Counting Semaphore, S value down to 1 from 2 and Reads the value of X, i.e., 0 and let’s say, gets preempted by CPU.
So, when Process, W will be alive again, it would read X=0 only.
Step 2: Then, we should must send Process, Y and it makes the counting Semaphore value S=0 from 1 (since, W is already sleeping somewhere inside the Critical Section and it has not still come out) and read the value of X which is still 0.
By executing X = X – 2; the new value of X becomes X = (-2) and comes out of Critical Section.
So, new updated value of X = (-2) and Counting Semaphore, S is available with its value 1
Step 3: Next, we must need to send the Process, Z and it makes the Counting Semaphore value from 1 to 0
It executes again X = X – 2; as its role and makes the new updated value of X = (-2) – (-2) = (-4)
Since, already the value X was -2 done by Process Y
then, Z comes out of the Critical Section and again, makes the Counting Semaphore, S available with value 1
Step 4: Now, W let’s say wakes up from Preemption and as we know, W doesn’t know abuot the updated value of X = (-4)
W still remembers what it last read when it was just going to sleep, i.e., 0
So, now Process W executes X = X + 1; and makes the new value of X as 0 + 1 = 1
Hence, the value of X = (-4) is totally lost now, there is no further existence of X = (-4)
And, then W comes out of the Critical Section by updating Counting Semaphore value as 2 (because, last time when Process Z left Critical Section, that time value of Counting Semaphore was 1)
Step 5: Now last process is left and that’s Process X
it’ll be invoked now and will make the Counting Semaphore value S = 1 from 2
Now, it’ll be executing X = X + 1; and will make the new value of X = 1 + 1 = 2 and will come out of the Critical Section again by making the Counting Semaphore available with its value 2
Hence, finally the Maximum value of X would be 2 and to get that, you must have to follow this sequence.
So, basically there are 2 sequences of execution possible to achieve the maximum value of X (i.e., 2) and those would be –
- Process W (needs to preempt just after reading value of X) → Process Y (complete) → Process Z (complete) → Process W (complete) → Process X (complete) OR,
- Process X (needs to preempt just after reading value of X) → Process Y (complete) → Process Z (complete) → Process X (complete) → Process W (complete)
Similarly, to get the minimum value of X (i.e., -4), there also we have 2 ways –
- Process Y (needs to preempt just after reading value of X) → Process W (complete) → Process X (complete) → Process Y (complete) → Process Z (complete) OR,
- Process Z (needs to preempt just after reading value of X) → Process W (complete) → Process X (complete) → Process Z (complete) → Process Y (complete)
Hope this helps!