28S-Section-Solution

28S-Section-Solution - while ((numBugs > 0) &&...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 28S Spring 2007 May 11, 2007 Section Solution Solution: Sweet Hall Madness static struct ta { bool available; // all true to start Semaphore availLock; // all set to 1 to start Semaphore requested; // student-to-ta rendezvous, all set to 0 initially Semaphore finished; // ta-to-student rendezvous, all set to 0 initially int numBugs; // set by TA using Examine function } tas[NUM_TAS]; static Semaphore numTAsAvailable; // set to NUM_TAS static Semaphore numMachinesAvailable; // set to NUM_MACHINES static int numStudentsLeft = NUM_STUDENTS; static Semaphore studentsLeftLock; // initially set to 1 static void TA(int id) { while (true) { SemaphoreWait(tas[id].requested); if (numStudentsLeft == 0) return; // last student left tas[id].numBugs = Examine(); SemaphoreSignal(tas[id].finished); ReadEmail(); } } static void Student() { int numBugs = 1; int ta; SemaphoreWait(numMachinesAvailable);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while ((numBugs > 0) && (numBugs < 10)) { Debug(); SemaphoreWait(numTAsAvailable); for (ta = 0; ta < NUM_TAS; ta++) { SemaphoreWait(tas[ta].availLock); if (tas[ta].available) break; SemaphoreSignal(tas[ta].availLock); } tas[ta].available = false; SemaphoreSignal(tas[ta].availLock); SemaphoreSignal(tas[ta].requested); SemaphoreWait(tas[ta].finished); numBugs = tas[ta].numBugs; tas[ta].available = true; SemaphoreSignal(numTAsAvailable); } 2 if (numBugs == 0) Rejoice(); SemaphoreWait(studentsLeftLock); numStudentsLeft--; bool everyoneDone = (numStudentsLeft == 0); SemaphoreSignal(studentsLeftLock); // thought question: why cant the two lines above be switched? if (everyoneDone) { for (ta = 0; ta < NUM_TAS; ta++) { SemaphoreSignal(tas[ta].requested); } } SemaphoreSignal(numMachinesAvailable); }...
View Full Document

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 2

28S-Section-Solution - while ((numBugs > 0) &&...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online