Explain with an example the need of Shift Alteration in critical section problem?
Consider processes Pi and Pj and consider the algorithm for Pi and Pj.
For Pi
do
{
while(turn!=i)
critical section
turn=j
remainder section
}whie(1);
For Pi
do
{
while(turn!=j)
critical section
turn=i
remainder section
}whie(1);
In the above code variable 'turn' is shared among the two processes. If turn=i after that process Pi is allowed to get into the critical section. This make sure that only one process at a time can be in its critical section. It doesn't satisfy the progress requirement and as a result we can do strict alteration with this code. That is if turn=0 and P1 is ready to get into the critical section, P1 can't enter into the critical section even though P0 may be in its remainder section. therefore a new algorithm which stores the state of the process is implemented. The code is:
do
{
flag[i]=true
while(flag[j])
critical section
flag[i]=false
remainder section
}whiel(1)
Here Pi is setting its flag as true. Then it confirms whether Pj is present in the critical section. If so after that it waits until the process Pj comes out of the critical section. If state of Pj is set as false after that Pi directly enters into the critical section.