The assignment is to be handed in class. No late assignments will be accepted.
The following is a variation of the critical section codes described in class. (Initially, flag[0] and flag[1] are both false.)
THREAD 0 THREAD 1
for(;;) for(;;)
{ {
... ...
flag[0]=true; while (flag[0]==true);
while (flag[1] == true); flag[1]=true;
<critical section> <critical section>
flag[0]=false; flag[1]=false;
<non-critical section> <non-critical section>
... ...
} }
(Notice that the codes for the two threads are asymmetric.) For the questions given below, justify your answer completely. Regarding this code:r1 = 5 + a; r2 = r1 - b; r3 = e + f; r4 = c * d; r5 = r1 + r2; r6 = r4 * r5; r7 = 4 - r3; r8 = r4 * r6; answer = r8;
In the above computation, the lower-case letters refer to memory variables. Assume that each of the above steps takes 1 time unit to complete. Hence, the entire computation will take 9 time units on a single CPU. Suppose you were running this code on a 2 processor machine and you decide to split the above code into 2 threads that can be run in parallel on the 2 processors. For example, r1 = 10 / a; and r3 = e + f; can be executed in parallel since these 2 commands do not affect each other. The goal is to execute the above code in the least possible time on 2 processors. The threads on the 2 CPUs need to be synchronized. For example, if Thread1 contains the code r4 = c * d; and Thread2 contains the code r6 = r4 / r5, you must ensure that r6=r4 /r5 executes after r4=c * d;
Use semaphores to synchronize the execution of the code on 2 processors. Assume that the P() and V() operations take 0 time units to complete (i.e., they are very fast operations compared to the computation time).
[a]. What is the least number of time units required to run the computation on 2 CPUs?
[b].What is the minimum number of semaphore variables required to run the above code on 2 CPUs?
[c].What is the least number of time units required to run the computation on 3 CPUs?
[d].What is the minimum number of semaphore variables required to run the above code on 3 CPUs?
_________________________________________________
What is the meaning of the term busy waiting? What other kinds of waiting are there in an operating system?
Can busy waiting be avoided altogether? Explain your answer.
_________________________________________________
Show that if the wait and signal semaphore operations are not executed atomically, then mutual exclusion may be violated.
_________________________________________________
Consider the following parallel program:
Thread x: Thread y: Thread z:
for(;;) for(;;) for(;;)
{ { {
P(x) P(y) P(z)
P(x) P(z)
P(x)
write "x" write "y" write "z"
V(y) V(z) V(x)
V(y) V(x) P(z)
V(z)
} } }
Initially, x = 5; y = z = 1. Describe the output format of this program.
Consider the following synchronization problem:
thread UNH adds 7 to a shared variable SCORE;
then thread UM and thread BU must both read the value of SCORE
(but they are allowed to read SCORE in any order);
then after UM and BU read SCORE, thread UNH adds 3 to SCORE
and then both BU and UM read the value of SCORE in any order.
The entire process repeats.
Give the code for these three threads, using semaphores,
to guarantee the above sequence of events.
_________________________________________________
The Sleeping - Barber problem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program (pseudo code) to coordinate the barber and the customers.
_________________________________________________