Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Operating Systems Review for Chap.6 & 7 Hung Q. Ngo KyungHee University Spring 2009 http://uclab.khu.ac.kr/lectures/2009-1-os.html Exercise 6.1 Operating Systems Hung Q. Ngo, KHU Spring’09 2 Solution to Critical-Section Problem 1.Mutual Exclusion - If process Pi 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 Operating Systems Hung Q. Ngo, KHU Spring’09 3 Exercise 6.4 • Mutual exclusion using Swap( ) with bounded waiting • swap (&address, register) { /* x86 */ temp = M[address]; M[address] = register; register = temp; } Operating Systems Hung Q. Ngo, KHU Spring’09 4 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); Operating Systems Hung Q. Ngo, KHU Spring’09 5 Exercise 6.4 • Solution: similar to fig. 6.10 using TestAndSet( ) Operating Systems Hung Q. Ngo, KHU Spring’09 6 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 Operating Systems Hung Q. Ngo, KHU Spring’09 7 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 int guard = 0; int value = FREE; Release() { Acquire() { // Short busy-wait time // Short busy-wait time while (test&set(guard)); while (test&set(guard)); if anyone on wait queue { if (value == BUSY) { take thread off wait queue put thread on wait queue; Place on ready queue; go to sleep() & guard = 0; } else { } else { value = FREE; value = BUSY; } guard = 0; guard = 0; } }• Note: sleep has to be sure to reset the guard variable – Why can’t we do it just before or just after the sleep? Operating Systems Hung Q. Ngo, KHU Spring’09 8 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. Operating Systems Hung Q. Ngo, KHU Spring’09 9 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) } Operating Systems Hung Q. Ngo, KHU Spring’09 10 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 } } Operating Systems Hung Q. Ngo, KHU Spring’09 11 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. Operating Systems Hung Q. Ngo, KHU Spring’09 12 Operating 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; } }Systems Hung Q. Ngo, KHU Spring’09 13 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 } Operating Systems Hung Q. Ngo, KHU Spring’09 14 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(); } Operating Systems Hung Q. Ngo, KHU Spring’09 } 15 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). Operating Systems Hung Q. Ngo, KHU Spring’09 16 Simple solution semaphore ok_to_cross = 1; void enter bridge() { ok to cross.wait(); } void exit bridge() { ok to cross.signal(); } Operating Systems Hung Q. Ngo, KHU Spring’09 17 Solution without deadlock & starvation Operating Systems Hung Q. Ngo, KHU Spring’09 18 Want More? :D Operating Systems Hung Q. Ngo, KHU Spring’09 19