Answer:
If a switch happens during counter++ or counter--, we will get a wrong value for the counter.
Explanation:
counter++ counter--
register1 = counter register2 = counter
register1 = register1 + 1 register2 = register2 - 1
counter = register1 counter = register2
Lets consider this example, lets assume that counter=4, this simply means that there are 4 items which are produced already. Lets look at the following steps and value of counter after each step.
a) Producer process is updating counter++, this takes place at machine level, also, during that when it completed register1=counter counter=4
b) Supposing we have a context switch happen to consumer process and it is trying to update counter--, this takes place at machine level, also when it completed
register2=counter counter=4
register2=register2-1
counter=register2 counter=3
c) Lets assume that context switch happened back to producer process and it continues from where it stopped, look at step a
register1=register1+1
counter=register1 counter=5
This will make the value of counter variable to become 5. Consumption of one item is not reflected in the counter variable. To avoid this we need to allow the atomic execution of all these instructions. This implies completion of any updates on the counter, while others wait for its completion.
d) In the event a producer executes all these machine level instructions at once,then there is no inconsistency on this.