Download presentation
Presentation is loading. Please wait.
Published byStella Strickland Modified over 9 years ago
1
Operating Systems Review for Chap.6 & 7 Hung Q. Ngo KyungHee University Spring 2009 http://uclab.khu.ac.kr/lectures/2009-1-os.html
2
2 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.1
3
3 Operating SystemsHung Q. Ngo, KHU Spring’09 Solution to Critical-Section Problem 1.Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2.Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3.Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes
4
4 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.4 Mutual exclusion using Swap( ) with bounded waiting swap (&address, register) { /* x86 */ temp = M[address]; M[address] = register; register = temp; }
5
5 Operating SystemsHung Q. Ngo, KHU Spring’09 Lock solution using Swap Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key. Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section lock = FALSE; // remainder section } while ( TRUE);
6
6 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.4 Solution: similar to fig. 6.10 using TestAndSet( )
7
7 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.7 Implement semaphore in multiprocessor environments using TestAndSet( ) with minimal busy waiting Hint: Similar to “better lock” but here “value” is more than Free/Busy Use a guard shared variable for acquiring critical section to modify the semaphore value Put waiting threads to sleep
8
8 Operating SystemsHung Q. Ngo, KHU Spring’09 Better Locks using test&set Can we build test&set locks without busy-waiting? –Can’t entirely, but can minimize! –Idea: only busy-wait to atomically check lock value Note: sleep has to be sure to reset the guard variable –Why can’t we do it just before or just after the sleep? Release() { // Short busy-wait time while (test&set(guard)); if anyone on wait queue { take thread off wait queue Place on ready queue; } else { value = FREE; } guard = 0; int guard = 0; int value = FREE; Acquire() { // Short busy-wait time while (test&set(guard)); if (value == BUSY) { put thread on wait queue; go to sleep() & guard = 0; } else { value = BUSY; guard = 0; } }
9
9 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.8 The Sleeping-Barber Problem: A barbershop consists of awaiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.
10
10 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.8 Semaphore mutex = 1; Semaphore customers = 0; Semaphore haircut = 0; int waiting = 0 void customer() //khách đ ế n c ắ t tóc { wait( mutex ); if( waiting == N ) //n ế u s ố khách đ ợ i = N thì r ờ i kh ỏ i ti ệ m { signal( mutex ); return ; } waiting ++; //tăng s ố khách đ ợ i signal( mutex ); signal(customers); //đánh th ứ c barber n ế u đang ng ủ wait(haircut); //đang c ắ t nh ư ng ch ư a xong (ch ờ trên gh ế c ắ t tóc) }
11
11 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.8 Semaphore mutex = 1; Semaphore customers = 0; Semaphore haircut = 0; int waiting = 0 void barber() //th ợ c ắ t tóc { while( 1 ) //c ắ t liên t ụ c, h ế t khách này đ ế n khách khác { wait( customers ); //n ế u không có khách, barber s ẽ ng ủ wait( mutex ); waiting --; //gi ả m 1 khách đ ợ i signal(mutex); cut_hair(); signal(haircut); //cho khách đã c ắ t tóc xong r ờ i kh ỏ i ti ệ m } }
12
12 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 6.9 & 6.10 Write a bounded-buffer monitor in which the buffers (portions) are embedded within the monitor itself. Problem with previous solution? Modify the solution to improve.
13
13 Operating SystemsHung Q. Ngo, KHU Spring’09 monitor bounded_buffer { int items[MAX_ITEMS]; int numItems = 0; condition full, empty; void produce(int v) { while (numItems == MAX_ITEMS) full.wait(); items[numItems++] = v; empty.signal(); } int consume() { int retVal; while (numItems == 0) empty.wait(); retVal = items[--numItems]; full.signal(); return retVal; } }
14
14 Operating SystemsHung Q. Ngo, KHU Spring’09 Producing Water! MakeH() { while (true) Make-Hydro(); // t ạ o 1 nguyên t ử H } MakeO() { while (true) Make-Oxy(); //t ạ o 1 nguyên t ử O } /* Ti ế n trình MakeWater ho ạ t đ ộ ng đ ồ ng hành v ớ i các ti ế n trình MakeH, MakeO, ch ờ có đ ủ 2 H và 1 O đ ể t ạ o H2O */ MakeWater() { while (True) Make-Water(); //T ạ o 1 phân t ử H2O }
15
15 Operating SystemsHung Q. Ngo, KHU Spring’09 Producing Water! Semaphore s1=0, s2=0; MakeH() { while (true) { Make-Hydro(); signal(s1);} } MakeO() { while (true) { Make-Oxy(); signal(s2);} } /* Ti ế n trình MakeWater ho ạ t đ ộ ng đ ồ ng hành v ớ i các ti ế n trình MakeH, MakeO, ch ờ có đ ủ 2 H và 1 O đ ể t ạ o H2O */ MakeWater() { while (True) { wait(s1); wait(s1); wait(s2); Make-Water(); } }
16
16 Operating SystemsHung Q. Ngo, KHU Spring’09 Exercise 7.10 A single-lane bridge connects the two Vermont villages of North Tunbridge and South Tunbridge. Farmers in the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Vermont farmers are stubborn and are unable to back up.) Using semaphores, design an algorithm that prevents deadlock. Hint: Initially, do not be concerned about starvation (the situation in which northbound farmers prevent southbound farmers from using the bridge, or vice versa).
17
17 Operating SystemsHung Q. Ngo, KHU Spring’09 Simple solution semaphore ok_to_cross = 1; void enter bridge() { ok to cross.wait(); } void exit bridge() { ok to cross.signal(); }
18
18 Operating SystemsHung Q. Ngo, KHU Spring’09 Solution without deadlock & starvation
19
19 Operating SystemsHung Q. Ngo, KHU Spring’09 Want More? :D
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.