134 views
4 4 votes

Consider the following atomic operation:

$\texttt{test_and_set(x)}$ sets $x$ to $1$ and returns its previous value.

Initially,

$x=0$

Process $P_1$ executes $\texttt{test_and_set(x)}$, followed by process $P_2$ executing $\texttt{test_and_set(x)}$.


Which of the following is correct?

  1. $P_1$ returns $0$, $P_2$ returns $1$, and the final value of $x$ is $1$
     
  2. $P_1$ returns $1$, $P_2$ returns $0$, and the final value of $x$ is $0$
     
  3. Both processes return $0$, and the final value of $x$ is $1$
     
  4. Both processes return $1$, and the final value of $x$ is $0$

1 Answer

0 0 votes

Given

test_and_set(x):

 
old = x;
x = 1;
return old;


Iska matlab:

  • Pehle purani value return karega.
  • Fir x = 1 kar dega.
  • Ye sab atomically hota hai.

Initially:

 
x = 0

Pehle P1 execute karega, uske baad P2.

p1 ke time x = 0 he ;

to old=0

x=1

return old;

p1=0 and x=1

now;

p2 

x = 1; it mean lock accure by p1

old=1

return old;

p2=1

x=1

 

Answer:
Position:
Show:

Related questions

2 2 votes
1 1 answer
113
113 views
GO Classes asked Jul 29
113 views
Two processors $CPU_0$ and $CPU_1$ share a variable.$CPU_0$ disables its local interrupts and enters a critical section without acquiring any shared hardware lock.At the ...
3 3 votes
1 1 answer
123
123 views
GO Classes asked Jul 29
123 views
The atomic operation $\texttt{compare_and_swap(x, old, new)}$ changes $x$ to $\texttt{new}$ only if its current value equals $\texttt{old}$.Initially,$x=5$The following o...
4 4 votes
1 1 answer
119
119 views
GO Classes asked Jul 29
119 views
Consider the following lock:Initially, $\texttt{lock = 0.}$$\texttt{while(test_and_set(\&lock));}$$\texttt{Critical Section}$$\texttt{lock = 0;}$Which of the following st...
4 4 votes
1 1 answer
137
137 views
GO Classes asked Jul 29
137 views
Which of the following statements about disabling interrupts are correct?On a uniprocessor system, disabling interrupts can prevent the running process from being preempt...