




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Monitor BarberShop { Condition barberAvail, customersAvail, charisAvail, doneCutting; int freeChairs = n; void startHaircut() { while ( freeChairs == n ) { customersAvail.wait(); } barberAvail.signal(); // Take a customer freeChairs++; } void finishHaircut() { doneCutting.signal(); } bool getHaircut() { if ( freeChairs == 0 ) { return FALSE; } freeChairs--; if (freeChairs == n-1) { customerAvail.signal(); } barberAvail.wait(); doneCutting.wait(); return TRUE; } } Barber { while (1) { shop.startHaircut(); // cut hair shop.finishHaircut(); } } BarberShop shop; Customer { shop.getHaircut(); }
Semaphore paper_matches = 0; Semaphore matches_tobacco = 0; Semaphore tobacco_paper = 0; Semaphore doneSmoking = 0; Agent { doneSmoking.wait(); randomSignal( paper_matches, tobacco_paper, matches_tobacco ); } Tobacco_Smoker { paper_matches.wait(); //smoke doneSmoking.signal(); } Other two smokers are similar
Delay( int time ) Called by a single thread Tick() Called by a hardware clock interrupt in ANOTHER THREAD (i.e., the thread that goes to sleep cannot wake itself up). No busy waiting
Semaphore nfree; // Initialized to NMACHINES. int [] available = new int[NMACHINES]; // All initialized to 1. int allocate() /* Returns index of available machine./ { int i; nfree.wait(nfree); / Wait until a machine is available / / same as nfree.wait(); / for (i=0; i < NMACHINES; i++) if (available[i] != 0) { available[i] = 0; return i; } } release(int machine) / Releases machine */ { available[machine] = 1; nfree.signal(); }
Implement either using semaphores or using primitive operations To get started: Look at Semaphore code If you use primitives, do something very similar for locks If you use Semaphores, think about what a lock is (it’s very similar to a semaphore…)
Lock issues A thread cannot Acquire a lock it already holds A thread cannot Release a lock it does not hold A lock cannot be deleted if a thread is holding it You need to keep track of who holds a lock CV issues A thread can only call Wait and Signal if it holds the mutex Wait must RELEASE the mutex before the thread sleeps Wait must ACQUIRE the mutex after the thread wakes up A CV cannot be deleted if a thread is waiting on it
Issues A thread can only be joined if specified during creation A thread can only be joined AFTER it has forked Only one thread can call Join on another A thread cannot call Join on itself A thread should be able to call Join on a thread that have already terminated This is the tricky part Should delay deleting thread object if it will be joined
Look to see how threads are deleted now Where should joined threads be deleted? Need to delete synch primitives used by Join as well.
These are synchronization problems just like we have been looking at in class You do not need to modify Nachos, but you need to use your synchronization primitives You will implement functions (e.g., Male(), Female(), Matchmaker()) and then fork threads to execute these functions in order to test them and show they work. You can do this in threadtest.cc