{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

28S-Section-Solution

28S-Section-Solution - while(numBugs>...

Info icon This 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);
Image of page 1

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

View Full Document Right Arrow Icon
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 can’t the two lines above be switched? if (everyoneDone) { for (ta = 0; ta < NUM_TAS; ta++) { SemaphoreSignal(tas[ta].requested); } } SemaphoreSignal(numMachinesAvailable); }...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern