1 / 19

Operating Systems Review for Chap.6 & 7

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. Solution to Critical-Section Problem.

onofre
Download Presentation

Operating Systems Review for Chap.6 & 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. Exercise 6.1

  3. 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

  4. Exercise 6.4 • Mutual exclusion using Swap( ) with bounded waiting • swap (&address, register) { /* x86 */ temp = M[address]; M[address] = register; register = temp;}

  5. 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. Exercise 6.4 • Solution: similar to fig. 6.10 using TestAndSet( )

  7. 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. int guard = 0; int value = FREE; Acquire() { // Short busy-wait timewhile (test&set(guard)); if (value == BUSY) { put thread on wait queue; go to sleep() & guard = 0; } else {value = BUSY;guard = 0; }} 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 timewhile (test&set(guard)); if anyone on wait queue { take thread off wait queue Place on ready queue; } else {value = FREE; }guard = 0;

  9. 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. 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. 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. 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. 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. 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. 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. 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. Simple solution semaphore ok_to_cross = 1; void enter bridge() { ok to cross.wait(); } void exit bridge() { ok to cross.signal(); }

  18. Solution without deadlock & starvation

  19. Want More? :D

More Related