![Page 1: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/1.jpg)
CSE 451Section
Thursday, October 30
![Page 2: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/2.jpg)
Questions from Lecture?
![Page 3: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/3.jpg)
Questions from the Project?
![Page 4: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/4.jpg)
Homeworks
• Barbershop Requirements:– Barber sleeps if no customers waiting
– Customers leave if no chairs for waiting
– Waiting customers can’t leave until haircut done.
• Solution: we use semaphores– Mutex = 1
– counting: barber_sleeping, customer_queue, cut_done
![Page 5: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/5.jpg)
State Diagram
Sleep Cut hair
Check Queue Wait for chair Wait for cut
Signal cut doneIf customers == 0 sleep
if space
![Page 6: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/6.jpg)
Barber Code
wait(mutex)if (customers_waiting == 0) { signal(mutex); wait(barber_sleeping); wait(mutex);} customers_waiting--;signal(mutex);signal(customer_queue);do_cut_hair();signal(cut_done);
![Page 7: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/7.jpg)
Customer Code
wait(mutex);if (customers_waiting == n) { signal(mutex); return;}customers_waiting++;if (customers_waiting == 1) { signal(barber_sleeping);} signal(mutex);wait(customer_queue);get_hair_cut();wait(cut_done);
![Page 8: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/8.jpg)
As a monitor
monitor barbershop { int num_waiting; condition get_cut; condition barber_asleep; condition in_chair; condition cut_done;
![Page 9: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/9.jpg)
Barber routine
barber() { while (1); while (num_waiting == 0) { barber_asleep.wait(); } customer_waiting.signal(); in_chair.wait(); give_hait_cut(); cut_done.signal(); }
![Page 10: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/10.jpg)
Customer routine
custome () { if (num_waiting == n) { return; } if (num_waiting == 0) { barber_asleep.signal(); } customer_waiting.wait(); in_char.signal(); get_hair_cut(); cut_done.wait(); }
![Page 11: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/11.jpg)
File access
• Requirements– Each file has a number N, and the sum of the
PID’s of the processes accessing the file must be <= N.
– Processes wishing to access the file block until the file is available
![Page 12: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/12.jpg)
File Monitor
monitor file_access { condition wake_up; int max_sum; int curr_sumaccess(int pid) { while (curr_sum + pid > max_sum) wake_up.wait(); curr_sum += pid; }release(int pid) { curr_sum = curr_sum - pid; wake_up.broadcast();}
![Page 13: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/13.jpg)
What’s wrong
• How long can a process wait?• What if the accesor doesn’t want to wait?• Do we need to wake everybody up every time?
![Page 14: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/14.jpg)
Cigarette Smokers
semaphore agent = 1;sempahore tobacco_paper = 1;sempahore paper_matches = 1;sempahore matches_tobacco = 1;agent() { while (1) { signal(paper_matches); wait(agent); }}
tobacco_guy() { while(1) { wait(paper_matches); signal(agent); }
![Page 15: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/15.jpg)
Memory Management - paging
![Page 16: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/16.jpg)
Memory management - segments
![Page 17: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/17.jpg)
Memory management - both
![Page 18: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/18.jpg)
Why both?
![Page 19: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/19.jpg)
Unixex bathroom (1)
monitor unisex_bathroom: int num_male_in; int num_female_in; condition male_wait; condition female_wait;
![Page 20: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/20.jpg)
Men enter
male_enter() { while (num_female_in != 0) { male_wait.wait(); } num_male_in++;}
male_exit() { num_male_in--; if (num_male_in == 0) { female_wait.broadcast(); }}
![Page 21: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/21.jpg)
Unixex bathroom (2)
monitor unisex_bathroom: int num_male_in; int num_female_in; int num_male_wait; int num_female_wait; condition male_wait; condition female_wait; boolean male_in;
![Page 22: CSE 451 Section Thursday, October 30. Questions from Lecture?](https://reader035.vdocuments.us/reader035/viewer/2022081516/5697c0271a28abf838cd6030/html5/thumbnails/22.jpg)
Women
female_enter() { num_female_wait++; if (num_male_wait > 0) female_wait.wait(); while (num_male_in != 0) female_wait.wait();
num_female_wait--; num_female_in++;
}