Necessary criteria for critical section solution are
- Mutual exclusion
- Progress
- Bounded waiting
Usually test and set is taken as a system call but it is implemented as a hardware instruction.Test_and_Set is a special assembly language instruction that does two operations autonomously. That is, the instruction can not be interrupted in the middle and it is not necessary to disable interrupts
The definition of test-and -set is
test-and-set(x)
{
t=x // t is temporary variable
x=1
return t
}
given initial value x=0
let A,B,C be the processes trying to enter critical section .
A tries to enter the critical section since x=0 it will get the access, sets x value to 1 and enters critical section.
now, A is in critical section and B tries to enter critical section since x=1 B will be kept waiting on while loop so the system is deadlock free since it is mutually exclusive
If A completes the process and sets X=0 and leaves the critical section so B enters the critical section hence progress is possible
Case 2:
we will reset all the values so Now X=0 again
Process A tries to enter the critical section since x=0 it will get the access, sets x value to 1 and enters critical section.
now, A is in critical section and B tries to enter critical section since x=1 B will be kept waiting on while loop. If CPU preempts the B. If C tries to enter the critical section since B is preempted C will enter the critical section if A leaves the critical section. so FIFO is not followed
FIFO is not followed then starvation is possible.
The above solution is a simple test-and-set solution that makes sure that deadlock doesn’t occur, but it doesn’t use any queue to avoid starvation or to have FIFO order.