Ancient CS 61 Content Warning!!!!!1!!!
This is not the current version of the class.
This site was automatically translated from a wiki. The translation may have introduced mistakes (and the content might have been wrong to begin with).

Help the House Elves

Dobby has been put in charge of all the kitchen House Elves at Hogwarts. Each elf must complete some the following series of tasks before it can leave for the day:

1. Prepare sumptious feast.

2. Clean up after messy Hogwarts students.

3. Say, "Harry Potter is the greatest Wizard Ever."

Whenever an elf completes one of its tasks, it announces what it just did. When an elf has completed all of its tasks, the supervisor dismisses the elf by saying "Thanks for your work, Elf N!" where N corresponds to the elf's ID number.

At the beginning of the day, Dobby (the main program) opens the kitchen and lets the elves inside (starts their threads). At any given moment, Dobby and possibly multiple other elves are working. Dobby is not allowed to dismiss an elf until that elf has finished working, but Dobby must dismiss an elf soon after the elf completed his/her work; he can't wait until all the elfs are done with their work.

We have provided most of the code to implement Dobby and the elves, but alas, we've forgotten to synchronize them! Can you possibly help us by selecting the right synchronization primitive(s) to solve this problem and adding them to the code in dobby.c? At the end of the main function, you'll see a comment "TODO Figure out how to wait on elves and when to print out Dobby's message." That's where a lot of your synchronization code will go, although there will undoubtedly be some initialization code in main as well as some synchronization code in the elf thread function.

Take a principled approach! Here are some questions to help guide you to a solution:

1. What shared state must you maintain?

2. How will you protect that state?

3. How will you detect that an elf is finished (this is tricky; the obvious answer won't work and you'll undoubtedly need to update your answer to #2 to address this)?

4. How will you make dobby (main) wait?

When Dobby and the House Elves are done ...

Please complete this survey and have a great Harvard/Yale weekend!