edited by
24,660 views
61 votes
61 votes

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:

  1. The above solution to $CS$ problem is deadlock-free

  2. The solution is starvation free

  3. The processes enter $CS$ in FIFO order

  4. More than one process can enter $CS$ at the same time

Which of the above statements are TRUE?

  1. (I) only
  2. (I) and (II)
  3. (II) and (III)
  4. (IV) only
edited by

13 Answers

Best answer
51 votes
51 votes

The answer is (A) only.

The solution satisfies:

  1. Mutual Exclusion as test-and-set is an indivisible (atomic) instruction (makes option (IV) wrong)
  2. Progress as at initially $X$ is $0$ and at least one process can enter critical section at any time.


But no guarantee that a process eventually will enter $CS$ and hence option (IV) is false. Also, no ordering of processes is maintained and hence III is also false.

So, eliminating all the $3$ choices remains $A$.

edited by
25 votes
25 votes

I.It is a test and set(load the previous value of R0 and store 1 into it),instruction,atomic in nature.Only one process executing TSL can only enter CS as it makes the value of variable=1 and other process trying to enter the CS would find value of variable=1 and hence wont enter the CS.

TSL can be thought of as

1.Load Ro

2 Store 1

3.CMP R0,#0

4.JNZ step 1

1 and 2 as atomic.

ME is guaranteed.

II. TSl suffers unbounded waiting.This is because there is no strict alteration and number of processes are also not bounded.

Lets us suppose

1.TSL

2.while(TSl!=1);

3.CS

4. store variable=0

P1:12 CS| P2 wants to enter but has to wait due to variable being 1 |P1 :4 | P3 enters executes TSL 1 2  CS | P2 wants to enter but has to wait due to variable being 1 |P3 :4 |P4 enters

So poor P2 everytime has to starve unlucky guy.

III. From the above example only we see that P2 although came first was scheduled later so FCFS not maintained.

Iv. ME guaranteed,

A)I Only

edited by
12 votes
12 votes
A) First one is true as it is simple test and set.
B) seems to be true but it is not as when a process is in spin lock all the thread of a process is waiting for this thread to complete job , whenever it is executed by cpu so in every context switch you are waiting for spin lock to over. means you are starving because you can complete other thread in parallel. This can be remove by introducing the waiting que but there is no que mention so system is starving.
c) No possible as no order or Que order is mention in the question.
d) Not possible.
8 votes
8 votes

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.
 

Answer:

Related questions