• edited by
48,183 views
187 187 votes

A certain computation generates two arrays a and b such that $a[i] = f(i)$ for $0 \leq i < n$ and $b[i] = g(a[i])$ for $0 \leq i < n$. Suppose this computation is decomposed into two concurrent processes $X$ and $Y$ such that $X$ computes the array $a$ and $Y$ computes the array $b$. The processes employ two binary semaphores $R$ and $S$, both initialized to zero. The array $a$ is shared by the two processes. The structures of the processes are shown below.

Process X:

private i; 
for (i=0; i< n; i++) { 
 a[i] = f(i); 
 ExitX(R, S); 
} 

Process Y:

private i;
for (i=0; i< n; i++) { 
  EntryY(R, S); 
  b[i] = g(a[i]); 
}

Which one of the following represents the CORRECT implementations of ExitX and EntryY?

  1. ExitX(R, S) { 
     P(R); 
     V(S); 
    }
    EntryY(R, S) { 
     P(S); 
     V(R); 
    }
    
  2. ExitX(R, S) { 
     V(R); 
     V(S); 
    }
    EntryY(R, S) { 
     P(R); 
     P(S); 
    }
    
  3. ExitX(R, S) { 
     P(S); 
     V(R); 
    }
    EntryY(R, S) { 
     V(S); 
     P(R); 
    }
    
  4. ExitX(R, S) { 
     V(R); 
     P(S); 
    }
    EntryY(R, S) { 
     V(S); 
     P(R); 
    }
    

11 Answers

Best answer
197 197 votes
  1. $X$ is waiting on $R$ and $Y$ is waiting on $X$. So, both cannot proceed.
     
  2. Process $X$ is doing Signal operation on $R$ and $S$ without any wait and hence multiple signal operations can happen on the binary semaphore so Process $Y$ won't be able to get exactly $n$ successful wait operations. i.e., Process $Y$ may not be able to complete all the iterations.
     
  3. Process $X$ does Wait(S) followed by Signal(R) while Process $Y$ does Signal(S) followed by Wait(R). So, this ensures that no two iterations of either $X$ or $Y$ can proceed without an iteration of the other being executed in between. i.e., this ensures that all $n$ iterations of $X$ and $Y$ succeeds and hence the answer.
     
  4. Process $X$ does Signal(R) followed by Wait(S) while Process $Y$ does Signal(S) followed by Wait(R). There is a problem here that $X$ can do two Signal(R) operation without a Wait(R) being done in between by $Y$. This happens in the following scenario:
    Process $Y$: Does Signal (S); Wait(R) fails; goes to sleep.
    Process $X$: Does Signal(R); Wait(S) succeeds; In next iteration Signal(R) again happens;

So, this can result in some Signal operations getting lost as the semaphore is a binary one and thus Process $Y$ may not be able to complete all the iterations. If we change the order of Signal(S) and Wait(R) in EntryY, then (D) option also can work. 

• edited by
25 25 votes

Let us think of it in this way. To calculate value of $b[0]$, we need to already have value of $a[0]$ with us because $b[0] = g(a[0])$, Else some error will be thrown.

So there can only be 2 cases:

  1. $Process X$ is selected first, this is good. It will calculate the value of $a[0]$ and then after this if $process Y$ executes, it will already have the corresponding value of $a[0]$ to calculate value of $b[0]$. If this case happens always, we do not need any semaphores.
  2. $Process Y$ is selected first. This is bad because now $Process Y$ will try to calculate value of $b[0]$ but value of $a[0]$ is not calculated yet. Now look at option $C$. Before calculating the value of $b[0]$, $Process Y$ needs to pass $EntryY$ first. Inititally $S=0, R=0$. To pass $EntryY$, it will first execute $V(S)$, simple up operation on $S$, now $S=1,R=0$. Now it tries to execute next line, $P(R)$. It will be blocked because $R=0$ and will have to wait for $R$ to be $1$, i.e. until up operation is performed on $R$. Now while it is waiting, $Process X$ executes and it finds value of $a[0]$. (now we can find value of $b[0]$ because $b[0] = g(a[0])$). Now it will go inside the $exitX$ and executes $P(S)$ so now $S=0,R=0$. It goes to the next line which is V(R). This is what we have been waiting for and now $S=0,R=1$. Now $process Y$ resumes and calculates $b[0]$. This can be generalized to $i, 0 < i < n$.

And hence we can safely calculate the arrays $a$ and $b$.

24 24 votes

Approach that must be followed while solving this kind of problem :

1. Identify producer and consumer and which semaphore is acting as wake up signal for consumer.

2. Here there must be strict alteration as elements of B  is dependent on A and semaphores are binary. If there would have been counting semaphore then no need of strict alteration as count will be maintained by counting semaphore.

3. Producing element in a[] up(sem 1(signal for consumer)) then block it till b does not calculate f(a[i]) so down(sem 2 ) here producer will be blocked. Now b will consume signal generated by a(sem 1) and calculate element of b(fa[i]) hence down (sem 1) now as a[i] and b[i] is done to calculate next element of a up (sem 2) so that producer can get unblocked and produce next element.

 

c matches this process.

18 18 votes

Option D is incorrect because If we consider n=2 than Process  X will compute a[0] and a[1] both but Process Y will not able to compute b[1] because P(R) signal. Below Image is showing such a case that Y will not able to execute exactly n times.

11 11 votes
//A
private i; 
for (i=0; i< n; i++) { 
    a[i] = f(i); 
    ExitX(R, S); 
} 

//B
private i;
for (i=0; i< n; i++) { 
    EntryY(R, S); 
    b[i] = g(a[i]); 
}


A and B are supposed to execute such that A[0]->B[0]->A[1]->B[1] and so on

Consider Option A :

ExitX(R, S) { 
 P(R); 
 V(S); 
}
EntryY(R, S) { 
 P(S); 
 V(R); 
}
A B
a[0] = f(0)
 
Busy waiting //R==0  
  Busy waiting //S==0

This creates a deadlock hence A not possible.


Option B:

ExitX(R, S) { 
 V(R); 
 V(S); 
}
EntryY(R, S) { 
 P(R); 
 P(S); 
}
A B
a[0] = f(0)
 
R=1  
S=1  
a[1] = f(1)
 
R=1  
S=1  
a[2] = f(2)
 


This will starve B and lead to incorrect results.

 

Option D :

ExitX(R, S) { 
 V(R); 
 P(S); 
}
EntryY(R, S) { 
 V(S); 
 P(R); 
}
A B
a[0] = f(0)
 
R=1  
S=0 // Busy Waiting  
  S=1
S=0  
a[1] = f(1)
 
R=1  

This leads to incorrect solution again.
A,B,D eliminated, C can be proven to work in the same way.

Answer:
Position:
Show:

Related questions

97 97 votes
10 answers 10 answers
38.7k
38.7k views
Arjun asked Sep 24, 2014
38,703 views
A shared variable $x$, initialized to zero, is operated on by four concurrent processes $W, X, Y, Z$ as follows. Each of the processes $W$ and $X$ reads $x$ from memory, ...
92 92 votes
9 answers 9 answers
43.9k
43.9k views
go_editor asked Apr 21, 2016
43,903 views
A computer uses $46\text{-bit}$ virtual address, $32\text{-bit}$ physical address, and a three–level paged page table organization. The page table base register stores th...
116 116 votes
11 answers 11 answers
49.6k
49.6k views
Arjun asked Sep 24, 2014
49,559 views
Consider a hard disk with $16$ recording surfaces $(0-15)$ having $16384$ cylinders $(0-16383)$ and each cylinder contains $64$ sectors $(0-63)$. Data storage capacity in...
87 87 votes
13 answers 13 answers
29.4k
29.4k views
Arjun asked Sep 23, 2014
29,356 views
Three concurrent processes $X$, $Y$, and $Z$ execute three different code segments that access and update certain shared variables. Process $X$ executes the $P$ operation...