Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.

Similar presentations


Presentation on theme: "CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8."— Presentation transcript:

1 CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8

2 CS4315A. Berrached:CMS:UHD2 Background In a multi-programmed OS, processes/threads often need to share resources: printers, files, data in memory, etc. Concurrent access to shared resources may result in inconsistent/improper operation. OS must provide mechanisms to ensure orderly execution of cooperating processes

3 CS4315A. Berrached:CMS:UHD3 Example An application with two processes P1 and P2 that share data. shared float balance; ---------------- Process P1: balance = balance + amount 1 --------------- Process P2:balance = balance - amount 2

4 CS4315A. Berrached:CMS:UHD4 Example-Cont. a1: LOAD R1, balance b1: MOV R2, amount1 c1: ADD R1, R2 d1: STORER1, balance a2: LOAD R1, balance b2: MOV R2, amount2 c2: SUB R1, R2 d2: STORER1, balance Assume initial values: balance = 100, amount1= 20 and amount2 = 50. What is the expected balance after P1 and P2 are executed? What if the processor executes the following sequence: a1, b1, a2, b2, c2, d2, c1, d1 Process P1:Process P2:

5 CS4315A. Berrached:CMS:UHD5 Critical Section Problem

6 CS4315A. Berrached:CMS:UHD6 Example--Cont. In the previous example –In P1, the statement balance = balance + amount1 is a critical section –In P2, the statement balance = balance - amount2 is a critical section

7 CS4315A. Berrached:CMS:UHD7 Solution to Critical Section Problem 1. Mutual Exclusion: at most one process is in its critical section at any time. 2. Progress: If no process is executing its critical section, a process that wishes to enter can get in. Only those processes that are not in the remainder section can participate in the decision to determine which process enters its critical section next. 3. Bounded Waiting: No process is postponed indefinitely. There must be a bound on the number of times that other process are allowed to get in after a process has made a request to get in.

8 CS4315A. Berrached:CMS:UHD8 Initial Attempt Two processes: P0 and P1 The general structure for each process Pi : repeat Entry section critical section exit section remainder section until false;

9 CS4315A. Berrached:CMS:UHD9 Disabling Interrupts to Implement the Critical Section repeat disableInterrupts(); Critical Section enableInterrupts(); remainder Section until flase; repeat disableInterrupts(); Critical Section enableInterrupts(); remainder Section until flase; Process P0: Process P1:

10 CS4315A. Berrached:CMS:UHD10 Disabling Interrupts to Implement the Critical Section float amt0;.... disableInterrupts(); balance = balance + amt0 enableInterrupts(); float amt1; …... disableInterrupts(); balance = balance - amt1 enableInterrupts(); shared float balance; Process P0: Process P1:

11 CS4315A. Berrached:CMS:UHD11 Algorithm 0 float amt0; …… while (lock ==TRUE) ; lock = TRUE; balance = balance + amt0; lock = FALSE; Shared boolean lock = FALSE; shared float balance; float amt1 ……. while (lock ==TRUE) ; lock = TRUE; balance = balance + amt1; lock = FALSE; Process P0:Process P1:

12 CS4315A. Berrached:CMS:UHD12 Algorithm 1 shared boolean turn = 0 // turn is either 0 or 1 Repeat while (turn == 1) ; Critical Section turn = 1; Remainder Section until flase; Process P0: Repeat while (turn == 0) ; Critical Section turn = 0; Remainder Section until flase; Process P1:

13 CS4315A. Berrached:CMS:UHD13 Algorithm 1 shared boolean turn = 0 // turn is either 0or 1 repeat while (turn == j) ; Critical Section turn = j; Remainder Section until flase; Process Pi: Satisfies mutual exclusion does not satisfy the progress requirement: the two processes must alternate trough the Critical section.

14 CS4315A. Berrached:CMS:UHD14 Algorithm 2 Shared boolean Flag[0..1] = {false, false} repeat Flag[i] = true; while (Flag[j] == true) ; Critical Section Flag[i] = false; Remainder Section until flase; Process Pi: Flag[i] = true ==> process want to get into critical section

15 CS4315A. Berrached:CMS:UHD15 Algorithm 3 Shared boolean Flag[0..1] = {false, false} boolean turn = 0; repeat Flag[i] = true; turn = j; while (Flag[j] and turn==j) ; Critical Section Flag[i] = false; Remainder Section until flase; Process Pi: Turn = i ==> process i can enter its critical section Flag[i] = true ==> process want to get into critical section Solves the critical section problem for two processes

16 CS4315A. Berrached:CMS:UHD16 Mutual Exclusion with Test & Set The test & set instruction is a machine level instruction that tests and sets a variable atomically. The instruction can be defined as follows: boolean test_and_set (boolean * target) {// sets target to True and returns its old value boolean OldValue; OldValue = *target; *target = True; return OldValue; }

17 CS4315A. Berrached:CMS:UHD17 Example boolean X = False; boolean temp; temp = test_and_set(& X); What are the values of temp and X ? –Temp = False –X = True

18 CS4315A. Berrached:CMS:UHD18 Mutual Exclusion with Test & Set The main characteristic of the Test&Set instruction is that it is executed atomically--I.e. as one un-interruptable instruction. Thus if two Test&Set instructions are executed simultaneously (each on a different CPU), they will be executed sequentially in some arbitrary order.

19 CS4315A. Berrached:CMS:UHD19 How Test & Set is used for Mutual Exclusion shared boolean lock = False; …… while ( Test&Set(&lock) == True ) { NULL}; // spin lock lock = FALSE; // atomic Satisfies Mutual Exclusion condition Does not satisfy bounded waiting condition While a process is in its critical section, all other processes which want to execute their critical sections continuously execute the while loop ==> waist of computing power.

20 CS4315A. Berrached:CMS:UHD20 Semaphores A semaphore is a synchronization mechanism that does not require busy waiting. A semaphore is an integer that can only be accessed through one of two atomic operations: wait() and signal(). Wait(S) { while (S<= 0) {NULL} ; S = S -1; } Signal(S) { S = S +1; }

21 CS4315A. Berrached:CMS:UHD21 Example: Critical Section for n Processes shared semaphore Mutex=1; …… wait(Mutex) signal(Mutex); Does not satisfy bounded waiting condition Still has busy waiting

22 CS4315A. Berrached:CMS:UHD22 Semaphores as General Synchronization Tools

23 CS4315A. Berrached:CMS:UHD23 Blocking/Active Semaphore class semaphore { private: int value; L is a list of process Ids // L may be organized as a FIFO list public: initialize( InitialValue) { value = InitialValue; L is an empty list; } // CONTINUE ON NEXT SLIDE

24 CS4315A. Berrached:CMS:UHD24 Blocking/Active Semaphore (Cont.) wait( ) { value = value -1; if ( value < 0) { add the PID of this process to L; // L may be organized as a FIFO queue block this process; } // CONTINUE ON NEXT SLIDE

25 CS4315A. Berrached:CMS:UHD25 Blocking/Active Semaphore (Cont.) signal ( ) { value = value + 1; if (value <= 0) { remove a process P from L; // may be done on a // FCFS basis wakeup(P); } } // end of class semaphore

26 CS4315A. Berrached:CMS:UHD26 Producer-Consumer Problem A Producer process/thread produces data items that are consumed by another process/thread (the Consumer). Each item produced by the producer needs to be consumed by the consumer. Shared memory is used to have the producer pass data items to consumer

27 CS4315A. Berrached:CMS:UHD27 Producer-Consumer Problem Assume threads use one shared variable X Shared itemType X; producer() { Repeat …. Compute next item NextP; …. X = NextP; … until false consumer() { Repeat …. NextC = X; // Consume …. Use NextC...; … until false

28 CS4315A. Berrached:CMS:UHD28 Producer-Consumer Problem Assume threads use one shared variable X Shared itemType X; Shared semaphore P = 0; producer() { Repeat …. Compute next item NextP; …. X = NextP; signal(P); … until false consumer() { Repeat …. wait(P); NextC = X; // Consume …. Use NextC...; … until false

29 CS4315A. Berrached:CMS:UHD29 Producer-Consumer Problem Assume threads use one shared variable X Shared itemType X; Shared semaphore P = 0, C = 1; producer() { Repeat …. Compute next item NextP; …. wait(C); X = NextP; signal(P); … until false consumer() { Repeat …. wait(P); NextC = X; // Consume signal(C); …. Use NextC...; … until false

30 CS4315A. Berrached:CMS:UHD30 Producer-Consumer Problem Assume threads use a buffer of size N Shared itemType buff[N]; producer() { int in = 0; Repeat …. Compute next item NextP; …. Buff[in] = NextP; in = (in+1) % N; … until false consumer() { int out= 0; Repeat …. NextC =buff[out]; // Consume out = (out+1) % N; …. Use NextC...; … until false

31 CS4315A. Berrached:CMS:UHD31 Producer-Consumer Problem Previous solution does not prevent consumer from consuming from empty buffer Does not prevent producer from inserting a new item into a full buffer.

32 CS4315A. Berrached:CMS:UHD32 Producer-Consumer Problem Assume threads use a buffer of size N Shared itemType buff[N]; int counter=0; producer() { int in = 0; Repeat …. Compute next item NextP; …. while (counter >=N) NULL ; Buff[in] = NextP; in = (in+1) % N; ++counter; … until false consumer() { int out= 0; Repeat …. while (counter <=0) NULL; NextC =buff[out]; // Consume out = (out+1) % N; --counter; …. Use NextC...; … until false

33 CS4315A. Berrached:CMS:UHD33 Producer-Consumer Problem Critic of Previous Solution: race condition may occur on access to shared variable counter If one producer & one consumer, access to shared buff does not cause any race conditions (why ?) If multiple producers & consumers, access to shared buff may cause race condition.

34 CS4315A. Berrached:CMS:UHD34 1 Producer & 1 Consumer Shared semaphore full_slots=0, empty_slots=N; Shared itemType buff[N]; producer() { int in = 0; Repeat …. Compute next item NextP; …. wait(empty_slots); Buff[in] = NextP; in = (in+1) % N; signal(full_slots); … until false consumer() { int out= 0; Repeat …. wait(full_slots); NextC =buff[out]; // Consume out = (out+1) % N; signal(empty_slots); …. Use NextC...; … until false

35 CS4315A. Berrached:CMS:UHD35 Multiple Producers & Consumers Shared semaphore full_slots=0, empty_slots=N, Cmutex, Pmutex=1; Shared itemType buff[N]; producer() { int in = 0; Repeat …. Compute next item NextP; …. wait(empty_slots); wait(Pmutex); Buff[in] = NextP; in = (in+1) % N; signal(Pmutex); signal(full_slots); … until false consumer() { int out= 0; Repeat …. wait(full_slots); wait(Cmutex); NextC =buff[out]; // Consume out = (out+1) % N; signal(Cmutex); signal(empty_slots); …. Use NextC...; … until false

36 CS4315A. Berrached:CMS:UHD36 Reader-Writer Problem Suppose a resource is to be shared among a community of processes of two types: readers & writers. A reader process can share the resource with any number of other readers but not with a writer A writer must have exclusive access to the resource

37 CS4315A. Berrached:CMS:UHD37 Readers First Solution Algorithm ideas: no reader should wait unless a writer is using the resource as long as a reader is using the resource, a writer who wishes to use the resource must wait 1st reader must check to make sure no writer is using the resource all subsequent readers do not have to wait provided at least one process is reading

38 CS4315A. Berrached:CMS:UHD38 Readers First Solution Cont. Shared semaphore WriterBlock = 1, Mutex=1; shared int readersCount = 0; Writer( ) { while (true) { wait(WriterBlock); signal(WriterBlock); …. }

39 CS4315A. Berrached:CMS:UHD39 Readers First Solution cont. reader( ) { while (true) { wait(Mutex); readersCount ++; if (readersCount == 1) wait(WriterBlock); signal(Mutex); wait(Mutex); readersCount--; if (readersCount == 0) signal(WriterBlock); signal(Mutex); } }

40 CS4315A. Berrached:CMS:UHD40 Dining-Philosophers Problem Five philosophers sitting at a table. Each has bowl of rice in front of him/her. There are five chopsticks, two at left and right of each philosopher, as shown above. A philosopher’s life consists of thinking and eating. A philosopher needs both left and right chopsticks to be able to eat. A chopstick can be used by only one philosopher at a time.

41 CS4315A. Berrached:CMS:UHD41 Dining-Philosophers Problem There are five philosopher processes numbered 0 through 4. Between each pair of philosophers is a chopstick. The chopsticks are also numbered 0 through 4, so that CStick i is between philosophers i and i-1 (all arithmetic on Cstick numbers and philosopher numbers is modulo 5 so CStick 0 is between philosophers 4 and 0, and Cstick 1 is between philo. 0 and 1).

42 CS4315A. Berrached:CMS:UHD42 Dining-Philosophers Problem Semaphore CStrick[5] (all initialized to 1) Philosopher i: while (true) { THINK(); wait(CStick[i]);// pick left chop stick wait(CStick[(i+1)%5];// pick right chop stick EAT(); signal(CStick[i]); signal(CStick[(i+1)%5]); } Algorithm has the potential of deadlock

43 CS4315A. Berrached:CMS:UHD43 Dining-Philosophers Problem Semaphore CStrick[5] (all initialized to 1) Philosopher i: while (true) { THINK( ); if (i%2 == 0){ // if even numbered wait(CStick[i]); // pick left chop stick wait(CStick[(i+1)%5]; // pick right chop stick } else {// if odd numbered wait(CStick[(i+1)%5]; // pick right chop stick wait(CStick[i]); // pick left chop stick } EAT( ); signal(CStick[i]); signal(CStick[(i+1)%5]); }


Download ppt "CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8."

Similar presentations


Ads by Google