Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Thursday, June 22, 2006 "I think I've got the hang of it now.... :w :q :wq :wq! ^d X exit ^X^C ~. ^[x X Q :quitbye CtrlAltDel ~~q :~q logout save/quit.

Similar presentations


Presentation on theme: "1 Thursday, June 22, 2006 "I think I've got the hang of it now.... :w :q :wq :wq! ^d X exit ^X^C ~. ^[x X Q :quitbye CtrlAltDel ~~q :~q logout save/quit."— Presentation transcript:

1 1 Thursday, June 22, 2006 "I think I've got the hang of it now.... :w :q :wq :wq! ^d X exit ^X^C ~. ^[x X Q :quitbye CtrlAltDel ~~q :~q logout save/quit :!QUIT ^[zz ^[ZZ ZZZZ ^H ^@ ^L ^[c $q ^# ^E ^X ^I ^T ? help helpquit ^D ^d ^C ^c help ^]q exit ?Quit ?q \qy \xyy F.M.H.! YMHAOS edw@sequent.COM KA9AHQ " - Ed Wright (0x0E2 UNIX The saga Never Ends)

2 2 Wait and Signal Example P1: S  Wait(); S  Signal(); P2: S  Wait(); S  Signal();

3 3 Wait and Signal Example P1: S  Wait(); P2: S  Wait(); P1: S  Wait(); P2: S  Signal(); P1: S  Signal(); ValueQueueP1P2 2emptyexecute

4 4 Wait and Signal Example P1: S  Wait(); P2: S  Wait(); P1: S  Wait(); P2: S  Signal(); P1: S  Signal(); ValueQueueP1P2 2emptyBlock 1YNN 0YNN P1YN 0YNN 1YNN 2YNN

5 5 Can implement thread join (or Unix system call waitpid) with semaphores Semaphore S=0; Thread::Join S  Wait(); Thread::Finish S  Signal();

6 6 Semaphores can be used for three purposes: §To ensure mutual exclusion of a critical section (as locks) §To control access to a shared pool of resources (using counting semaphore) §To cause a thread/process to wait for a specific action to be signaled by another thread/process

7 7 Reader Process wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex); A couple of questions: 1.If a wrier is in Critical section and two readers arrive, where does the first one block and where does the second one block? 2.Why do we use a single mutex here?

8 8 Dining-Philosophers Problem §There are 5 philosophers sitting at a round table. Between each adjacent pair of philosophers is a chopstick. In other words, there are five chopsticks. §Each philosopher does two things: think and eat. §The philosopher thinks for a while, and then stops thinking and becomes hungry. §When the philosopher becomes hungry, he/she cannot eat until he/she owns the chopsticks to his/her left and right. §When the philosopher is done eating he/she puts down the chopsticks and begins thinking again.

9 9 Dining-Philosophers Problem §Shared data semaphore chopstick[5]; Initially all values are 1 Allocate several resources among processes in deadlock-free, starvation-free manner

10 10 §The philosophers basically go through the following steps. while(1) { think for a random number of seconds pickup forks; eat for a random number of seconds putdown forks; }

11 11 Dining-Philosophers Problem §Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1);

12 12 Dining Philosopher - Deadlock Free Solution state[] array is initialized to THINKING semaphore array s[i], i=0 … N-1, is initialized to zero

13 13 Dining Philosopher - Deadlock Free Solution

14 14 Dining Philosopher - Deadlock Free Solution void take_forks(int i) { wait(mutex); state[i]=HUNGRY; test(i); signal(mutex); wait(s[i]); }

15 15 Dining Philosopher - Deadlock Free Solution void test(i) {// i is philosopher number if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i]=EATING; signal (s[i]); }

16 16 Dining Philosopher - Deadlock Free Solution void put_forks(i) { wait(mutex); state[i]=THINKING; test(LEFT); test(RIGHT); signal(mutex); }

17 17 Sleeping Barber Problem

18 18 Sleeping Barber Problem Barber shop has one barber and n chairs If no customers are present, the barber sleeps When customer arrives, he has to wake up the sleeping barber If additional customer arrives while the barber is cutting another’s hair then: he sits down and waits if empty chairs available he leaves if all chairs are full

19 19 Sleeping Barber Problem Customer /*check if chair available, if not leave*/ /**signal that I have arrived**/ /*wait until barber cuts my hair */ } Barber /*sleep until customer wakes me up*/ /* service customer (remember to update chairs available) */ /* tell customer to leave*/ /*repeat the above for other customers*/

20 20 Sleeping Barber Problem N = 5; //number of chairs int chairs_occupied = 0; semaphore mutex = 1;

21 21 Sleeping Barber Problem N = 5; //number of chairs int chairs_occupied = 0; semaphore mutex = 1; semaphore barber_finished = 0; semaphore customer_arrived = 0;

22 22 Sleeping Barber Problem Customer{ wait(mutex); if (chairs_occupied<num) chairs_occupied ++; else{ signal(mutex); exit(); } signal(mutex); signal(customer_arrived) wait(barber_finished); } Barber{ while(true){ wait(customer_arrived); wait(mutex); chairs_occupied --; signal(mutex); cut_hair( ); signal(barber_finished); }

23 23 Another example §Motorbike assembly §One process makes tyres §Another process makes chassis and assembles motorbike §Tyre processes and chassis process should exit when a motorbike is complete Barrier

24 24 Motorbike assembly MakeTyres /* signal when tyre is ready */ /* wait until tyre is assembled in a motorbike */ MakeChassis&Assemble /*make chassis */ /*wait for both tyres to arrive*/ /*assemble motorbike*/ /*tell both tyres that they have been used*/

25 25 What happens here? MakeTyreFunction( ){ signal(tyre); wait(motorbike_done); } MakeChassis&Assemble( ){ /*make chassis */ wait(tyre); /* assemble motorbike*/ signal(motorbike_done); } semaphore tyre = 0; semaphore motorbike_done = 0;

26 26 This solution is incorrect MakeTyreFunction( ){ signal(tyre); wait(motorbike_done); } MakeChassis&Assemble( ){ wait(tyre); /*make chassis and assemble motorbike*/ signal(motorbike_done); } semaphore tyre = 0; semaphore motorbike_done = 0;

27 27 Another possible solution (incomplete)... MakeTyreFunction( ){ wait(mutex); count++; if (count = = 2){ signal(assemble); signal(tyre_exit); count = 0;} signal(mutex); wait(tyre_exit); } MakeChassis&Assemble( ){ wait(assemble); /*make chassis and assemble motorbike*/ } semaphore tyre_exit = 0; semaphore chassis_arrived = 0; semaphore assemble = 0;

28 28 Another possible solution (complete) MakeTyreFunction( ){ wait(chassis_arrived); wait(mutex); count++; if (count = = 2){ signal(assemble); signal(tyre_exit); count = 0;} signal(mutex); wait(tyre_exit); } MakeChassis&Assemble( ){ signal(chassis_arrived); wait(assemble); /*make chassis and assemble motorbike*/ } semaphore tyre_exit = 0; semaphore chassis_arrived = 0; semaphore assemble = 0;

29 29 Semaphore l Essentially shared global variables. l No control or guarantee of proper usage. Solution l Use a higher level primitive called monitors.

30 30 Monitors §Higher level synchronization primitive. §Similar to a C++ class that ties data, operations together. §Processes may call the procedures in a monitor whenever they want to, but cannot access the private data structures from functions outside the monitor.

31 31 Monitors Unlike classes: l Monitors ensure mutual exclusion Only one thread may execute a given monitor method at a time. l Monitors require all data to be private.

32 32 Monitors

33 33 Schematic View of a Monitor

34 34 Monitors §Only one process can be active in a monitor at any instant. §Monitors are programming level constructs, so compiler knows they are special and can handle calls to monitor procedures differently. §Compiler implements the mutual exclusion on monitor. §Programmer does not have to be aware of how the compiler arranges for mutual exclusion.

35 35 Monitors §Turn critical regions into monitor procedures, no two processes shall execute their critical regions at same time.

36 36 Monitors §The Java synchronized construct implements a limited form of monitor §It is simple to turn a Java class into a monitor l Make all data private l Make all methods synchronized

37 37 class Queue{ private...; // queue data } public void synchronized Add( Object item ) { put item on queue; } public Object synchronized Remove() { if queue not empty { remove item; return item; }

38 38 How can we change remove() to wait until something is on the queue? §Logically, we want to go to sleep inside of the monitor. § But if we hold on to the lock and sleep, then other threads cannot access the shared queue, add an item to it, and wake up the sleeping thread. §The thread could sleep forever.

39 39 Solution: Use condition variables §Condition variables enable a thread to sleep inside a critical section. §Any lock held by the thread is atomically released when the thread is put to sleep.

40 40 Condition variable: is a queue of threads waiting for something inside a critical section. Condition variables support three operations: 1.Wait(): atomic (release lock, go to sleep), when the process wakes up it re- acquires lock. 2. Signal(): wake up waiting thread, if one exists. Otherwise, it does nothing. 3. Broadcast(): wake up all waiting threads Rule: thread must hold the lock when doing condition variable operations.

41 41 Monitor With Condition Variables

42 42 In Java: §The wait() method causes a thread to release the lock it is holding on an object; allowing another thread to run l wait() can only be invoked from within synchronized code §Use notify() to signal that the condition a thread is waiting on is satisfied. §Use notifyAll() to wake up all waiting threads. l notify() and notifyAll() can only be used within synchronized code

43 43 In Java class Queue { private...; // queue data } public void synchronized Add( Object item ) { put item on queue; notify (); } public Object synchronized Remove() { while queue is empty wait (); // give up lock and go to sleep remove and return item; }

44 44 Monitors §Condition variables are not counters §They do not accumulate signals like semaphores do §Condition variable is signaled with no process waiting, signal is lost.

45 45 Monitors How is this different from sleep and wakeup we saw earlier?

46 46 Monitors

47 47 Monitor Operations §Encapsulates shared data you want to protect. §Acquires mutex at the start. §Operations on shared data. §Temporarily releases mutex if it can’t complete. §Re-acquires mutex when it can continue. §Releases mutex at the end.

48 48 Mesa-style: (Java, and most real operating systems) §The thread that signals keeps the lock (and thus the processor). §The waiting thread waits for the lock. Hoare-style: (most textbooks) §The thread that signals gives up the lock and the waiting thread gets the lock. §When the thread that was waiting and is now executing exits or waits again, it releases the lock back to the signaling thread.


Download ppt "1 Thursday, June 22, 2006 "I think I've got the hang of it now.... :w :q :wq :wq! ^d X exit ^X^C ~. ^[x X Q :quitbye CtrlAltDel ~~q :~q logout save/quit."

Similar presentations


Ads by Google