0% found this document useful (1 vote)
1K views

Semaphore Exercises

The document discusses processes and synchronization using semaphores. It provides examples of processes that print strings and examples using semaphores to control the order of printing and ensure conditions like mutual exclusion. It also discusses initializing semaphores to different values and how this impacts the possible outputs.

Uploaded by

Gheorghe Cotleţ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
1K views

Semaphore Exercises

The document discusses processes and synchronization using semaphores. It provides examples of processes that print strings and examples using semaphores to control the order of printing and ensure conditions like mutual exclusion. It also discusses initializing semaphores to different values and how this impacts the possible outputs.

Uploaded by

Gheorghe Cotleţ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Exercises on Semaphores

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?

Consider the tree following processes P1 P2 P3

Print(R) Print(I) Print(O) Print(OK) Print(OK) Print(OK)

Add operations on semaphores such that:

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.

Consider the following processes, where initially y = z = 0: Process 1


// initialization code integer x; x = y + z; // other code... y = 1; z = 2; // other code...

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?

Is this a solution to mutual exclusion?


C1, C2 : INTEGER := 1; task P1; task P2; task body P1 is task body P2 is begin begin loop loop NON_CRITICAL_SECTION_1; NON_CRITICAL_SECTION_2; loop loop exit when C2 = 1; exit when C1 = 1; end loop; end loop; C1 := 0; C3 := 0; CRITICAL_SECTION_1; CRITICAL_SECTION_2; C1 := 1; C2 := 1; end loop; end loop; end P1; end P2;

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:

terminate normally; or deadlock.

Remember to indicate how the semaphores are initialized.

P1
print(A); print(B); print(C);

P2
print(E); print(F); print(G);

Insert semaphores to satisfy the properties:

print A before printing F print F before printing C

Dont forget to indicate the initial value of the semaphores.

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

Add semaphores such that:

number F number A number H number E number C number G number I number T

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?

Consider the two following threads:


T_1 = while true do print A T_2 = while true do print B

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

Consider the two following threads:


T_1 = while true do print A T_2 = while true do print B

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, ...

You might also like