140 likes | 310 Views
CSE 451. Section February 3, 2000. Agenda. Project comments Homework 3 review Sample exam questions. Project comments. How did it go? What was difficult? How did the hand in go? What can we do to make it easier?. Homework 3. General comments:
E N D
CSE 451 Section February 3, 2000
Agenda • Project comments • Homework 3 review • Sample exam questions
Project comments • How did it go? • What was difficult? • How did the hand in go? • What can we do to make it easier?
Homework 3 • General comments: • You need a mutex around all shared variables • If you have a condition variable and no monitor, you need a mutex • You can’t broadcast on a condition variable in a monitor • Don’t invent new synchronization constructs, unless you implement them with semaphores, mutexes, or condition variables: • “Sleep”, “Awaken”, “Call asynchronous”
Homework 3 problems • Sleeping Barbers • Need to keep a count of waiters, with a mutex • Need to wait for barber to finish cutting hair • Smokers • Easiest to use a monitor. • Need either 3 conditions, or one, and then serial wake-ups • If using semaphores, need to consider case where ingredients arrive out of order
Homework 3 problems (cont) • Line printers: • Best solution: use a condition variable with a priority queue • Other solutions: have one condition variable per caller, and order those • File access • Need to consider case where multiple waiters should be awoken
Sample Problems • Scheduling – SJF, RR, FIFO
Deadlock • Bankers Algorithm
Process States • What is possible? Running Ready Blocked
Safety Void AtomicTransfer(Queue *queue1, Queue *queue2) { Item thing; // thing being transferred queeu1->lock.Acquire(); thing=queue1->dequeue(); if (thing != NULL) { queue2->lock.acquire(); queue2->Enqueue(thing); queue2->lock.release(); } queue1->lock.release(); }
Making Water Int numHydrogen = 0; Semaphore pairOfHydrogen(0); Semaphore oxygen(0); Void hready() { numHydrogen++; If ((numHydrogen%2 == 0) { pairofHydrogen->Signal(); } Oxygen->Wait(); }
Making Water Void oReady() { pairofHydrogen->Wait(); makeWater(); Oxygen->Signal(); Oxygen->Signal(); }
Making Water Semaphore hPresent(0); Semaphore waitForWater(0); Void hReady() { hPresent->signal(); waitForWater->wait(); } Void oReady() { hPresent->wait(); hPresent->wait(); makeWater(); waitForWater->Signal(); waitforWater->Signal(); }
Cannibals and Missionaries • Problem: • Rowboats hold 3 people • Can’t carry 2 cannibals and 1 missionary in a boat • Boats only leave when full