• edited by
16,411 views
58 58 votes

The following is a code with two threads, producer and consumer, that can run in parallel. Further, $S$ and $Q$ are binary semaphores quipped with the standard $P$ and $V$ operations.

semaphore S = 1, Q = 0; 
integer x;

producer:                   consumer:
while (true) do             while (true) do
    P(S);                       P(Q);
    x = produce ();             consume (x);
    V(Q);                       V(S);
done                        done

Which of the following is TRUE about the program above?

  1. The process can deadlock
  2. One of the threads can starve
  3. Some of the items produced by the producer may be lost
  4. Values generated and stored in '$x$' by the producer will always be consumed before the producer can generate a new value

5 Answers

Best answer
45 45 votes

Producer: consumer: while (true) do while (true) do $1$ $P(S)$; $1$ $P(Q)$; $2$ $x =$ produce $()$; $2$ consume $(x)$; $3$ $V(Q)$; $3$ $V(S)$; done done

Lets explain the working of this code.

It is mentioned that $P$ and $C$ execute parallely.

$P:1 2 3$

  1. $S$ value is $1$, down on $1$ makes it $0$. Enters the statement $2$.
  2. Item produced.
  3. Up on $Q$ is done (Since the queue of $Q$ is empty, value of $Q$ up to $1$).

This being an infinite while loop should infinitely iterate.

In the next iteration of while loop $st 1$ is executed.

But $S$ is already $0$, further down on $0$ sends $P$ to blocked list of $S.P$ is blocked.

$C$ Consumer is scheduled.

Down on $Q.$ value makes $Q.$value$=0$;

Enters the statement $2$, consumes the item.

Up on $S$,now instead of changing the value of $S$. value to $1$, wakes up the blocked process on Q 's queue.Hence process P is awaken. $P$ resumes from statement $2$, since it was blocked at statement $1$. So, $P$ now produces the next item.

So, consumer consumes an item before producer produces the next item.

(D) Answer

(A) Deadlock cannot happen has both producer and consumer are operating on different semaphores (no hold and wait )

(B) No starvation happen because there is alteration between $P$ and Consumer. Which also makes them have bounded waiting.

• edited by
21 21 votes
D Consumer can consume only once the producer has produced the item, and producer can produce(except the first time) only once the consumer has consumed the item.
• edited by
8 8 votes

Hence option D  

________________

As we can see the code ,  the semaphore value of S=1 , Q=0 , then firstly producer will run then it will down S and then goes to critical section then up  Q .

there is a STRICT ALTERATION in the codes .

we see that producer will not produce more as after producing the item , then  consumer will consume .

if strick alteration then no dead lock and no starvation possible every process will get chance

Note :

even  if buffer size is 1 then also no overflow 
------------------------------------------------------

 

Don’t Forgot To Upvote

• edited by
1 1 vote

Producer can't decrement the Entry point of the consumer and vice-versa. So, no deadlock. Option A eliminated.

Producer holds the V operator of the consumer, and consumer holds the V operator of the producer. Both can't starve; in fact there's strict alteration. Option B eliminated.

Option C asks if producer can run twice consecutively (assuming there's no buffer, the most recent value would overwrite previous one). There's strict alteraton, so the answer is no. Option C eliminated.

Option D is correct; this is a proper working code of a one-item-only producer/consumer relationship.

0 0 votes
As we can see the code ,  the semaphore value of S=1 , Q=0 , then firstly producer will run then it will down S and then goes to critical section then up  Q . similarly , consumer will down Q and up S . so , we can see there is a STRICT ALTERATION in the codes . we see that producer will not produce more as after producing the item , then  consumer will consume . so , no overflow is there even if buffer size is 1 .

Hence option D .
Answer:
Position:
Show:

Related questions

40 40 votes
5 answers 5 answers
22.0k
22.0k views
Ishrat Jahan asked Oct 28, 2014
22,032 views
An operating system implements a policy that requires a process to release all resources before making a request for another resource. Select the TRUE statement from the ...
86 86 votes
10 answers 10 answers
37.6k
37.6k views
Ishrat Jahan asked Oct 28, 2014
37,564 views
Assume that a main memory with only $4$ pages, each of $16$ bytes, is initially empty. The CPU generates the following sequence of virtual addresses and uses the Least Re...
49 49 votes
6 answers 6 answers
21.1k
21.1k views
Ishrat Jahan asked Oct 27, 2014
21,113 views
A paging scheme uses a Translation Look-aside Buffer (TLB). A TLB-access takes $10$ ns and the main memory access takes $50$ ns. What is the effective access time(in ns) ...
1 1 vote
0 0 answers
2.4k
2.4k views
Ishrat Jahan asked Oct 27, 2014
2,412 views
Consider the execution of the following commands in a shell on a Linux operating sys­tem.bash\$ cat alphaMathematicsbash\$ In alpha betabash\$ rm alphabash\$ cat > beta <...