Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the following threads, T1, T2, and T3 executing on a single processor, synchronized using three binary semaphore variables, S1, S2, and S3, operated upon using standard wait() and signal(). The threads can be context switched in any order and at any time.

T1T2T3
while(true){
  wait(S3);
  print("C");
  signal(S2); }
while(true){
  wait(S1);
  print("B");
  signal(S3); }
while(true){
  wait(S2);
  print("A");
  signal(S1); }

Which initialization of the semaphores would print the sequence BCABCABCA....?

  • A.\(S_{1} = 1;\ S_{2} = 1;\ S_{3} = 1\)
  • B.\(S_{1} = 1;\ S_{2} = 1;\ S_{3} = 0\)
  • C.\(S_{1} = 1;\ S_{2} = 0;\ S_{3} = 0\)
  • D.\(S_{1} = 0;\ S_{2} = 1;\ S_{3} = 1\)

Answer: C

The initialization is \(S_{1} = 1;\ S_{2} = 0;\ S_{3} = 0\).

The sequence must start with B, so only T2, which waits on \(S_{1}\), may run first: \(S_{1} = 1\) and the others 0. Then T2 prints B and signals \(S_{3}\), which lets only T1 print C; T1 signals \(S_{2}\), which lets only T3 print A; T3 signals \(S_{1}\) and the cycle repeats. At every moment exactly one semaphore is 1, so the order BCA is forced.

In the other options more than one semaphore starts at 1. With \(S_{2} = 1\), T3 could print A first; with \(S_{3} = 1\), T1 could print C first; and option D, with \(S_{1} = 0\), cannot print B first at all.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.