The enter_CS() and leave_CS() functions to implement critical section of a…
2009
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, X is a memory location associated with the CS and is initialized to 0. Now consider the following statements:
I. The above solution to CS problem is deadlock-free
II. The solution is starvation free.
III. The processes enter CS in FIFO order.
IV More than one process can enter CS at the same time.
Which of the above statements is TRUE?
- A.
I only
- B.
I and II
- C.
II and III
- D.
IV only
Attempted by 63 students.
Show answer & explanation
Correct answer: A
The test-and-set instruction is an atomic operation that reads a memory location and sets it to 1 simultaneously. In the given solution, `enter_CS` spins until `test-and-set(X)` returns 0, ensuring that only one process can successfully acquire the lock (mutual exclusion), which makes Statement IV false. Since at least one process will eventually succeed in acquiring the lock, the system is deadlock-free (Statement I is true). However, this implementation does not track request order or guarantee fairness; a process might be repeatedly bypassed by others, leading to starvation. Thus, the solution is not starvation-free (Statement II false) and does not enforce FIFO ordering (Statement III false). Therefore, only Statement I is correct.
A video solution is available for this question — log in and enroll to watch it.