in Operating System edited by
1,594 views
2 votes
2 votes

Can a process be preempted while it is in a critical section?

If yes, then how does the critical section or synchronisation mechanism concept provide solution for 'Data inconsistency'?

If no, then how the gate2013 problem on critical section is solved.( LInks are given below)

1. for gateoverflow answer: https://gateoverflow.in/1545/gate2013-34

2. for video explanation of the solution: https://www.youtube.com/watch?v=jDUJsCFNI0E

In video explanation instead of preemption sir has used context switch. How preemption is different from context switch?

in Operating System edited by
1.6k views

2 Comments

preemption simply means that a process A is stopped from executing and a process B continues then on. To change this execution of process from A to B, context witch takes place wherein A's data is stored in the PCB and B's data is retrieved.
0
0
Please answer my first 2 question. the 3rd one I just asked for confirmation.
0
0

1 Answer

2 votes
2 votes

No, a process in Critical Section can not be preempted. Best example to understand this is Priority Inversion:

https://en.m.wikipedia.org/wiki/Priority_inversion

That GATE 2013 problem is based on counting semaphore that can allow upto two processes at a same time in the Critical Section. There is no preemption there. 

I haven't watched the video but I think you are not clear about the meaning of preemption and context switch.

Preemption: A process under execution(not in Critical Section) is preempted by a higher priority process, i.e., now the previous process is in blocked/waiting queue and the resources it had are now free to be used by the new higher priority process. Preemption is about getting hold of the processor to execute. Best example of preemption is any preemptive scheduling algorithm, like Round Robin.

https://en.m.wikipedia.org/wiki/Preemption_(computing)

Context Switch: Here context means information of a process, stored in Process Control Block of a process. Context Switch happens whenever a process under execution is paused to execute another process. This is done by storing info about the process so that when the process is loaded again, it is in the same state. Read "Steps" section in the below link:

https://en.m.wikipedia.org/wiki/Context_switch

I also suggest you to read about Spinlock if you don't know about it yet. It will deepen the understanding.

4 Comments

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.

1
1
When W(or any other process) is in CS, no other process can preempt it but accompany it in the CS since semaphore was initialized to 2. So Y and Z won't come in the CS like that. Any other process can accompany with W in the CS. Both the process will read the same value of 'x' at the beginning. The process out of those two(W and another one) that writes last on 'x' will actually make the change to stay.

Neither Context Switch nor Preemption is happening in this GATE question because no priority has been discussed and actions on variable 'x' are being performed in a CS.
2
2

does It mean that process W is going too slow and meanwhile Y and Z did their part one by one and after that W is writing for x and again process X is incrementing for the possible maximum value of shared variable 'x'?

Though in the following video tutor has involved the Context Switch concept.

https://www.youtube.com/watch?v=jDUJsCFNI0E

0
0
W just can't go slow like that because it has same number of instructions in the code like others. To maximize the value, we can allow Y(or Z) to enter CS first, then allow W(or X) to accompany them. Both of them will have same values of 'x' in the beginning. Since Y entered first, it will have a head start from W and it will complete its execution first but its change will not retain as it will be overwritten by W. Similarly, Z and X can happen.

Yes, Context Switch can happen in the manner he is discussing in the video but we don't really require Context Switch because we can execute Y with W and then Z with X to get the maximum value of 'x'. Context Switch is an overhead and we generally avoid it as much as we can. That's why I said no Context Switch is happening in the question because we don't really require it. But yes, there can be a possible Context Switch but if semaphore is 0 then the process(es) trying to enter CS will enter in the blocked queue.
0
0

Related questions

0 votes
0 votes
0 answers
1