Docsity

Memory Management and Synchronization: Operating Systems Lecture 13, Study Guides, Projects, Research of Operating Systems

Partial preview of the text

Download Memory Management and Synchronization: Operating Systems Lecture 13 and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

COMP 310:

Operating Systems

Lecture 13: Review &

Memory Management

October 1, 2004

Christine Alvarado

Today’s Topics

 Synchronization problem: Sleeping barber

 Project 1: tips and issues

 Begin Memory Management

Practice: Sleeping barber

Waiting room

Barber chair

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

Homework: Timer

 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

Monitor Timer {

int ellapsedTime = 0;

Condition chillOut;

void delay( int time ) {

int wakeUpTime = time + ellapsedTime;

while ( wakeUpTime > ellapsedTime ) {

chillOut.wait();

void tick()

ellapsedTime++;

chillOut.broadcast();

Practice: Laundromat

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(); }

Problem 1: Locks and CVs

 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…)

Locks and CVs

 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

Make sure your implementation is SAFE!

Thread::Join

 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

 If no join, no change to how it is deleted

 Look to see how threads are deleted now  Where should joined threads be deleted?  Need to delete synch primitives used by Join as well.

Whales and Bridge

 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