Presentation is loading. Please wait.

Presentation is loading. Please wait.

Synchronization Exercises. Exercise 1 r Let S be a semaphore that is initialized to 2 r Consider the following: down(S) up(S) down(S) r Does this program.

Similar presentations


Presentation on theme: "Synchronization Exercises. Exercise 1 r Let S be a semaphore that is initialized to 2 r Consider the following: down(S) up(S) down(S) r Does this program."— Presentation transcript:

1 Synchronization Exercises

2 Exercise 1 r Let S be a semaphore that is initialized to 2 r Consider the following: down(S) up(S) down(S) r Does this program block? 2

3 Exercise 1: Answer r No r The first down decrements from 2 to 1 r The second down decrements from 1 to 0 r The up increments from 0 to 1 r The third down decrements from 1 to 0 3

4 Exercise 2 r Let S be a semaphore that is initialized to 2 r Consider the following: down(S) up(S) down(S) r Does this program block? 4

5 Exercise 2: Answer r Yes it does r The first down decrements from 2 to 1 r The second down decrements from 1 to 0 r The up increments from 0 to 1 r The third down decrements from 1 to 0 r The 4 th down blocks the process 5

6 Exercise 3 r Thread B must do operation opB only after thread A has done opA r How can you guarantee this using semaphores? 6

7 Exercise 3: Incorrect Answer thread A down(S) opA up(S); thread B down(S) opB up(S) 7 Initialize S to 1 Why incorrect? Thread B could get to down(S) before thread A. -- in this case B executes opB before A executes opA

8 Exercise 3: Correct Answer thread A opA up(S); thread B down(S) opB 8 Initialize S to 0 Even if B gets to down(S) before A gets to opA it will block until A has executed opA

9 Exercise 4 r Assume process A and process B are to take turns executing opA and opB respectively? r Process A starts first r How can you guarantee this using semaphores? 9

10 Exercise 4 : Incorrect Answer Process A while(1){ opA up(S 1 ); down(S 1 ); } Process B while(1) { opB up(S 2 ); up(S 1 ); } 10 Initialize S 1 to 0 and S 2 to 1 Process B could beat process A to executing opB before opA

11 Exercise 4 : Correct Answer Process A while(1){ down(S 2 ) opA up(S 1 ); } Process B while(1) { down(S 1 ) opB up(S 2 ); } 11 Process A will always execute first since the first time it hits down(S 2 ) it doesn’t block while process B always blocks the first time it hits down(S 1 ) Initialize S 1 to 0 and S 2 to 1

12 Exercise 4 : Correct Answer Process A while(1){ opA up(S 1 ); down(S 2 ) } Process B while(1) { down(S 1 ) opB up(S 2 ); } 12 Initialize S 1,S 2 to 0 The first time process B enters the loop it blocks but A doesn’t.

13 Exercise 5 r Why is it important to keep the size of the critical section as small as possible 13

14 Exercise 5:Answer r As a critical section can be executed by one thread/process at a time. r Other threads/processes must wait until the current thread must finish. Long critical sections lead to lower throughput 14


Download ppt "Synchronization Exercises. Exercise 1 r Let S be a semaphore that is initialized to 2 r Consider the following: down(S) up(S) down(S) r Does this program."

Similar presentations


Ads by Google