cse 451 section thursday, october 30. questions from lecture?

22
CSE 451 Section Thursday, October 30

Upload: lee-carter

Post on 19-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CSE 451 Section Thursday, October 30. Questions from Lecture?

CSE 451Section

Thursday, October 30

Page 2: CSE 451 Section Thursday, October 30. Questions from Lecture?

Questions from Lecture?

Page 3: CSE 451 Section Thursday, October 30. Questions from Lecture?

Questions from the Project?

Page 4: CSE 451 Section Thursday, October 30. Questions from Lecture?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

Memory Management - paging

Page 16: CSE 451 Section Thursday, October 30. Questions from Lecture?

Memory management - segments

Page 17: CSE 451 Section Thursday, October 30. Questions from Lecture?

Memory management - both

Page 18: CSE 451 Section Thursday, October 30. Questions from Lecture?

Why both?

Page 19: CSE 451 Section Thursday, October 30. Questions from Lecture?

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?

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?

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?

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++;

}