The enter_CS() and leave_CS() functions to implement critical section of a process are realized using test-and-set instruction as follows:
void enter_CS(X)
{
while(test-and-set(X));
}
void leave_CS(X)
{
X = 0;
}
In the above solution, is a memory location associated with the and is initialized to . Now consider the following statements:
-
The above solution to problem is deadlock-free
-
The solution is starvation free
-
The processes enter in FIFO order
-
More than one process can enter at the same time
Which of the above statements are TRUE?
View worked solution
Answer: A
The test-and-set(X) instruction is an atomic hardware instruction that performs the following operations atomically:
temp = X;
X = 1;
return temp;
Initially, . Let us evaluate each statement:
- Statement I: The solution is deadlock-free. (TRUE)
If multiple processes attempt to enter the critical section simultaneously when , exactly one process will successfully executetest-and-set(X), receive a return value of , and exit thewhileloop to enter the critical section. The other processes will spin in the loop. Since progress is guaranteed and there is no circular dependency preventing all processes from proceeding, the solution is deadlock-free. - Statement II: The solution is starvation-free. (FALSE)
There is no mechanism to guarantee bounded waiting. A process waiting in thewhileloop might repeatedly lose the race to other processes that exit and re-enter the critical section. Thus, a process can starve indefinitely. - Statement III: The processes enter CS in FIFO order. (FALSE)
The selection of the next process to enter the critical section is arbitrary and depends purely on CPU scheduling and which process executes the atomictest-and-setinstruction first when becomes . No FIFO queue is maintained. - Statement IV: More than one process can enter CS at the same time. (FALSE)
Sincetest-and-setis atomic, only one process can read and set it to . Any subsequent process will read and remain blocked in thewhileloop. Thus, mutual exclusion is strictly preserved.
Therefore, only statement (I) is TRUE.
Free practice · Your answers on this page are not saved.
