Section Solution - Stanford University
[Pages:2]CS107 Spring 2008
Section Solution
Handout 28S May 13, 2008
Solution: RSS News Feed Madness
static struct ta { bool available; Semaphore availLock; Semaphore requested; Semaphore finished; int numBugs;
} tas[NUM_TAS];
// all true to start // all set to 1 to start // student-to-ta rendezvous, all set to 0 initially // ta-to-student rendezvous, all set to 0 initially // set by TA using Examine function
static Semaphore numTAsAvailable; static Semaphore numMachinesAvailable; static int numStudentsLeft = NUM_STUDENTS; static Semaphore studentsLeftLock;
// set to NUM_TAS // set to NUM_MACHINES
// 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);
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 can't the two lines above be switched?
if (everyoneDone) { for (ta = 0; ta < NUM_TAS; ta++) { SemaphoreSignal(tas[ta].requested); }
}
SemaphoreSignal(numMachinesAvailable); }
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- stanford university philosophy department
- stanford university plato
- stanford university encyclopedia of philosophy
- stanford university philosophy encyclopedia
- stanford university philosophy
- stanford university ein number
- stanford university master computer science
- stanford university graduate programs
- stanford university computer science ms
- stanford university phd programs
- stanford university phd in education
- stanford university online doctoral programs