Semaphore Exercises
Semaphore Exercises
Semaphore S = Counter P (prendere) take the semaphore if S == 0 then wait else S := S - 1 V (vendere) return the semaphore S := S + 1
Notation
Semaphore S S.P, S.V P(S), V(S)
A process B must do an operation opB() only after a process A has done operation opA(). B: opB() ... A: opA() How can you guarantee this using semaphores?
The result printed is R I O OK OK OK The nal value of the semaphores is identical to their initial value
Let P1, P2, and P3 be three processes each running an operation op-i. At every moment, we want the number of op-i to be at most one more than the number of op-j. 1. Propose a solution using semaphores, adding them to the processes P1, P2, P3. 2. Propose another solution, where: i. there are at most 2 operations on semaphores for each process ii. another process may be added
Propose some pseudo-code for processes that have the following behavior: the processes A and B may execute operations opA and opB (in any order) only after a process C has done operation opC. Do not forget to indicate the initial value of the semaphores.
Process 2
// initialization code
1. What are the possible nal values for x? 2. Is it possible, using semaphore, to have only two values for x?
Give an example in pseudo-code of 3 processes that use semaphores such that, according to the order in which they are executed, can either:
P1
print(A); print(B); print(C);
P2
print(E); print(F); print(G);
P1
repeat print(A); print(B); print(C); print(D); forever
P2
repeat print(E); print(F); print(G); forever
P3
repeat print(H); print(I); forever
P1
print(C); print(E);
P2
print(A); print(R); print(O);
Insert semaphores such that only ACERO or ACREO is printed. Dont forget to indicate the initial value of the semaphores.
P1
repeat print(A); SC.V; SA.P; forever
P2
repeat print(B); SC.V; SB.P; forever
P3
repeat SC.P; SC.P; print(C); SA.V; SB.V; forever
Assuming semaphores SA, SB, and SC are initialized at 0, what strings can be printed?
Add semaphores such that at any moment the number of A or B differs by at most 1. The solution should allow strings such as: ABBAABBABA
Add semaphores such that the string printed is: ABABABA... Is it possible to have the same result using only instructions sleep(n) that interrupts a thread for n seconds?
(initial value) (initial value) (initial value) (initial value) A: B: r:=r+1 y:=x x:=x+1 z:=z+1 print(x) print(z) print(r)
y r x z
= = = =
0; 1; 2; 0;
What semaphores should you add such that the nal value of y is 3?
semaphore mutex = 1; semaphore times_a = 2; semaphore times_b = 0; A: repeat forever: P(times_a) P(mutex) <A1> V(mutex) V(times_b) B: repeat forever: P(times_b) P(mutex) <B1> V(mutex) V(times_a)
The concurrent execution of A and B produces an innite sequence of <A1> and <B1>. Which one is the only possible start of the sequence: 1. 2. 3. 4. A1, A1, B1, A1, A1, B1, A1, A1, B1, ... A1, B1, A1, A1, B1, A1, B1, A1, A1, ... A1, B1, A1, B1, A1, B1, A1, B1, A1, ... A1, A1, B1, B1, A1, B1, B1, A1, A1, ...