Thankyou for the answer first Akhilesh.
I know about the spinlock, context switch and preemption. But some doubts arise while solving questions.
If possible please see gate 2013 question here:
A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution?
Here for the maximum value of shared variable x we need to do the following :
if in critical section there are 3 steps to go for:
"read-modify-write"
if we first allow w. After read and modify, process w is preempted/context switched(MY DOUBT ARISES HERE) and then we allow y and z to proceed the all 3 steps. Y and z will make value of x as -4. After that if w comes back and writes x as 1 and gets out of cs. And then process X comes and proceed. Thus we get maximum value 2 for shared variable x.
This is how my understanding goes for the question and its solution. please correct me wherever I am wrong.
If we can't allow preemption in CS then how it is getting preempted and if it is context switch then my question is - Are we allowed to consider context switch? And yes then Can't it again cause data inconsistency?
Pardon me for my English.