ProcessThread Synchronization Part 2 Modified from Chapter 6

  • Slides: 33
Download presentation
Process/Thread Synchronization (Part 2) Modified from Chapter 6 for 170 with Nachos threads, Tao

Process/Thread Synchronization (Part 2) Modified from Chapter 6 for 170 with Nachos threads, Tao Yang, 2012 Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009

What to learn? n Software and hardware solutions of the synchronization primitives n Deadlock/starvation

What to learn? n Software and hardware solutions of the synchronization primitives n Deadlock/starvation n More synchronization applications 6. 2

Implementation of Synchronization Primitives n Software solutions through shared memory data checking or message

Implementation of Synchronization Primitives n Software solutions through shared memory data checking or message passing. l Assume that the LOAD and STORE instructions are atomic for shared variables; 4 that is, cannot be interrupted n Hardware-assisted solutions n Properties (e. g. for implementing a lock) l Mutual exclusion: only one gets a lock. l Progress: when multiple threads try to acquire a lock, one can get it if nobody has it. l Bounded waiting: lock waiting time is limited. 6. 3

Software Solution for 2 processes/threads n Two processes use a shared variable to coordinate

Software Solution for 2 processes/threads n Two processes use a shared variable to coordinate int turn=0; // whose turn for the critical section n Process 0 l while (turn != 0); critical section turn= 1; //give turn to P 1 n Process 1: while (turn != 1); critical section turn= 0; //give turn to P 0 n Mutual exclusion? Progress? Bounded waiting? 6. 4

Mutual exclusion? n Prove by contraction. Assume both Processes 0 and 1 are in

Mutual exclusion? n Prove by contraction. Assume both Processes 0 and 1 are in the critical section. n Process 0 while (turn != 0); turn==0 critical section turn= 1; //give turn to P 1 n Process 1: while (turn != 1); critical section turn==1 turn= 0; //give turn to P 0 n (turn==0) and (turn==1) is false 6. 5

Progress? n The two processes use a shared variable to coordinate int turn=0; //

Progress? n The two processes use a shared variable to coordinate int turn=0; // whose turn for the critical section n Process 0 l while (turn != 0); critical section turn= 1; n Process 0 tries again: Loop forever while (turn != 0); critical section turn= 1; 6. 6

Peterson’s Solution n Two process software solution (Text book chapter 6. 3) n Assume

Peterson’s Solution n Two process software solution (Text book chapter 6. 3) n Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. n The two processes share two variables: int turn; 4 indicates whose turn it is to enter the critical section. l Boolean ready[2] 4 indicate if a process is ready to enter the critical section. ready[0] = true implies that process P 0 is ready! l 6. 7

Peterson’s Algorithm Process P 0: ready[0] = TRUE; Ready to enter critical section; Wait

Peterson’s Algorithm Process P 0: ready[0] = TRUE; Ready to enter critical section; Wait if another process is in. turn = 1; while (ready[1] && turn == 1); Critical section Exit critical section ready[0] = FALSE; Process P 1: ready[1] = TRUE; turn = 0; while (ready[0] && turn == 0); Critical section ready[1] = FALSE; 6. 8 Mutual exclusion? Progress? Bounded waiting?

Mutual Execution? Prove by contradition Process P 0: ready[0] = TRUE; turn = 1;

Mutual Execution? Prove by contradition Process P 0: ready[0] = TRUE; turn = 1; while (ready[1] && turn == 1); critical section ready[0]=T and (Ready[1]=F or turn=0) ready[0] = FALSE; Process P 1: ready[1] = TRUE; turn = 0; while (ready[0] && turn == 0); critical section ready[1]=T and (Ready[0]=F or turn=1) ready[1] = FALSE; Both conditions are true, which is not possible 6. 9

Progress? Can both P 0/P 1 wait forever? Process P 0: ready[0] = TRUE;

Progress? Can both P 0/P 1 wait forever? Process P 0: ready[0] = TRUE; Loop forever turn = 1; while (ready[1] && turn == 1); (Ready[1]=T and turn=1) critical section ready[0] = FALSE; Process P 1: ready[1] = TRUE; Loop forever turn = 0; while (ready[0] && turn == 0); (Ready[0]=T and turn=0) critical section ready[1] = FALSE; Both conditions are true, which is not 6. 10 possible

Progress? Can P 0 wait forever after P 1 leaves Process P 0: ready[0]

Progress? Can P 0 wait forever after P 1 leaves Process P 0: ready[0] = TRUE; Loop forever turn = 1; while (ready[1] && turn == 1); (ready[1]=T and turn=1) critical section ready[0] = FALSE; Process P 1: ready[1] = TRUE; turn = 0; while (ready[0] && turn == 0); critical section ready[1]=F ready[1] = FALSE; Both conditions are true, which is not possible. 6. 11

Hardware Solution for Synchronization n Many systems provide hardware support for critical section code

Hardware Solution for Synchronization n Many systems provide hardware support for critical section code n Uniprocessors – could disable interrupts Currently running code would execute without preemption 4 Project 1. l Generally too inefficient on multiprocessor systems 4 Operating systems using this not broadly scalable l n Modern machines provide special atomic hardware instructions 4 Atomic = non-interruptable l Either test memory word and set value l Or swap contents of two memory words 6. 12

Atomic Test. And. Set Instruction n Definition: Fetch the value of a shared variable

Atomic Test. And. Set Instruction n Definition: Fetch the value of a shared variable and then set it to TRUE atomically. boolean Test. And. Set (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } 6. 13

Lock Solution using Test. And. Set n Shared boolean variable: lock. “True” means somebody

Lock Solution using Test. And. Set n Shared boolean variable: lock. “True” means somebody has acquired the lock. l initialized to FALSE. n Solution: l while ( Test. And. Set (&lock )) ; // wait Critical section; lock = FALSE; Mutual exclusion? Progress? Bounded waiting? no 6. 14

Mutual exclusion? Assume P 0 gets first, and then P 1 Process P 0:

Mutual exclusion? Assume P 0 gets first, and then P 1 Process P 0: Property: Lock=T somebody is in the critical section Lock=F nobody is in the critical section. while(Test. And. Set (&lock)); critical section lock = FALSE; } C 1: lock was F in last Test. And. Set() returns F Now lock is T. Process P 1: while(Test. And. Set (&lock)); critical section lock = FALSE; C 2: lock was F in last Test. And. Set() returns F Conditions C 1 and then C 2: cannot be true 6. 15

Bounded Waiting? Process P 0: Process P 1: Test. And. Set (&lock)); //get in

Bounded Waiting? Process P 0: Process P 1: Test. And. Set (&lock)); //get in … Test. And. Set(&lock); // wait Lock=FALSE; //get out Test. And. Set(&lock); // get in Test. And. Set(&lock); // wait 6. 16

Atomic Swap Instruction n Definition: exchange values of two variables automatically. void Swap (boolean

Atomic Swap Instruction n Definition: exchange values of two variables automatically. void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: } 6. 17

Lock Solution using Swap n Shared Boolean variable lock initialized to FALSE; Each process

Lock Solution using Swap n Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key n Solution: key = TRUE; while ( key == TRUE) Swap (&lock, &key ); Critical section lock = FALSE; Mutual exclusion, progress. bounded waiting? 6. 18 Key: True Lock: False

Test. And. Set Solution with Bounded Waiting Enter Critical Section if: Lock =False or

Test. And. Set Solution with Bounded Waiting Enter Critical Section if: Lock =False or Waiting[i]==False Waiting[] T T T F Assign with cyclic order for fairness 6. 19

Bounded-waiting Mutual Exclusion with Test. And. Set() waiting[i] = TRUE; key = TRUE; while

Bounded-waiting Mutual Exclusion with Test. And. Set() waiting[i] = TRUE; key = TRUE; while (waiting[i] && key) key = Test. And. Set(&lock); Loop if this process should wait and Lock =True waiting[i] = FALSE; Critical section j = (i + 1) % n; while ((j != i) && waiting[j]== False) j = (j + 1) % n; if (j == i) lock = FALSE; else waiting[j] = FALSE; 6. 20 Find a waiting process j and set waiting[j]=False Or Set Lock=False

Deadlock and Starvation n Deadlock – two or more processes (or threads) are waiting

Deadlock and Starvation n Deadlock – two or more processes (or threads) are waiting indefinitely for an event that can be only caused by one of these waiting processes n Starvation – indefinite blocking. A process is in a waiting queue forever. n Let S and Q be two locks: P 0 P 1 Acquire(S); Acquire(Q); Acquire (Q); . Acquire (S); . . . Release (Q); Release (S); . . Release(S); Release(Q); 6. 21

Deadlock Avoidance n Order the locks and always acquire the locks in that order.

Deadlock Avoidance n Order the locks and always acquire the locks in that order. n Eliminate circular waiting P 0 P 1 Acquire(S); Acquire(Q); . . . Release(Q); Release(S); Acquire (Q); . . . Release (Q); Release (S); 6. 22

Classical Problems of Synchronization n Bounded-Buffer Problem n Laundromat n Barriers n Readers and

Classical Problems of Synchronization n Bounded-Buffer Problem n Laundromat n Barriers n Readers and Writers Problem 6. 23

Barriers 6. 24

Barriers 6. 24

Barriers called multiple times barrier(3); 6. 25

Barriers called multiple times barrier(3); 6. 25

Implement a barrier int count=0; barrier(N) { //for N threads mylock->Acquire(); count ++; mylock->Release();

Implement a barrier int count=0; barrier(N) { //for N threads mylock->Acquire(); count ++; mylock->Release(); What’s wrong with this? while (count <N) Cond->Wait(mylock); if(count==N) { Cond->Broadcast(mylock); Count=N for next count=0; barrier() called in } another thread mylock->Release() 26 6. 26

Readers-Writers Problem n A data set is shared among a number of concurrent processes.

Readers-Writers Problem n A data set is shared among a number of concurrent processes. l Readers – only read the data set; they do not perform any updates l Writers – can both read and write n Requirement: l l allow multiple readers to read at the same time. Only one writer can access the shared data at the same time. n Reader/writer access permission table: Reader Writer OK NO No No Reader Writer 6. 27

Readers-Writers (First try with 1 lock) n writer do { wrt. Acquire(); // wrt

Readers-Writers (First try with 1 lock) n writer do { wrt. Acquire(); // wrt is a lock // writing is performed wrt. Release(); } while (TRUE); n Reader Writer Reader ? ? Writer ? ? do { wrt. Acquire(); // Use wrt lock // reading is performed wrt. Release(); } while (TRUE); 6. 28

2 nd try using a lock + readcount n writer do { wrt. Acquire();

2 nd try using a lock + readcount n writer do { wrt. Acquire(); // Use wrt lock // writing is performed wrt. Release(); } while (TRUE); n Reader do { readcount++; // add a reader counter. if(readcount==1) wrt. Acquire(); // reading is performed readcount--; if(readcount==0) wrt. Release(); } while (TRUE); 6. 29

You may also use a binary semaphore n writer do { wrt. P(); //

You may also use a binary semaphore n writer do { wrt. P(); // Use wrt semaphore with initial value=1 // writing is performed wrt. V(); What’s wrong with this? } while (TRUE); n Reader readcount is not protected do { readcount++; //initial value=0 if(readcount==1) wrt. P(); // reading is performed readcount--; if(readcount==0) wrt. V(); } while (TRUE); 6. 30

Readers-Writers Problem (Text Book) n Shared Data l Data set l Semaphore mutex initialized

Readers-Writers Problem (Text Book) n Shared Data l Data set l Semaphore mutex initialized to 1 l Semaphore wrt initialized to 1 l Integer readcount initialized to 0 6. 31

Readers-Writers Problem (textbook) n The structure of a writer process do { wrt. P()

Readers-Writers Problem (textbook) n The structure of a writer process do { wrt. P() ; //Lock wrt // writing is performed wrt. V() ; //Unlock wrt } while (TRUE); 6. 32

Readers-Writers Problem (Cont. ) n The structure of a reader process do { mutex.

Readers-Writers Problem (Cont. ) n The structure of a reader process do { mutex. P() ; readcount ++ ; if (readcount == 1) wrt. P() ; mutex. V() // reading is performed mutex. P() ; readcount - - ; if (readcount == 0) wrt. V() ; mutex. V() ; } while (TRUE); 6. 33