Programming Assignment II

Due on Friday, October 17, 2008 @ 11pm

70 points

Hungry Eagles

A family of eagles has one mother eagle, n baby eagles, and m feeding pots initially empty, where 0 < m <= n. Each baby eagle must eat using a feeding pot and each feeding pot can only serve one baby eagle at any time. Therefore, no more than m baby eagles can eat at the same time. The mother eagle eats elsewhere and does not require a feeding pot.

Each baby eagle plays for a while and eats. Before a baby eagle can eat, he must wait until a feeding pot has food. After eating, the corresponding feeding pot becomes empty and has to be refilled by the mother eagle.

The mother eagle is very tired and needs sleep. So, she goes to sleep until a baby eagle wakes her up. Should this happen, the mother eagle hunts for food and fill all feeding pots. Then, she sleeps again. It is possible that when a baby eagle wants to eat and finds out that all feeding pots are empty. This baby eagle should wake up the mother eagle. As mentioned in the previous paragraph, the mother eagle takes some time to hunt for food and fill all feeding pots. Once all feeding pots are filled, no more than m waiting hungry baby eagles can eat. Since the mother eagle does not want to wake up too often, she insists that exactly one baby eagle who found out all feeding pots being empty can wake her up. More precisely, even though two or more hungry baby eagles may find out all feeding pots being empty, only one of them can wake her up and all the others just wait until food will become available.

After providing the t-th meal, the mother eagle retires and sends all of her baby eagles away to start an independent life. Perhaps, she needs a vacation, and, therefore, the feeding game ends!

In the system, eagles are simulated by threads. Initially, the m feeding pots are all empty. Each baby eagle thread has the following pattern:

while (1) {              
     Delay();            // play for a while     
     ready_to_eat(...);  // get hungry          
     Delay();            // eat for a while     
     finish_eating(...); // finish eating       
                         // do some other thing 
}

The mother eagle thread has the following pattern:

while (not time to retire) {            
     goto_sleep(...);    // take a nap         
     Delay();            // prepare food       
     food_ready(...);    // make food available
     Delay();            // do something else  
}

Here, functions ready_to_eat(), finish_eating(), goto_sleep() and food_ready() are functions written by you. They have the following meaning:

In this way, all controls and synchronization mechanisms are localized in these four functions and the eagle threads look very clean. Write a C++ program using the semaphore capability of ThreadMentor to simulate this activities.

Your implementation must correctly handle the following important situations among others:

You can only use semaphores and mutex locks. The use of any other synchronization primitives will risk low or even no credit.

Input and Output

The input to your program consists of the following:

Submission Guidelines

  1. All programs must be written in ANSI C++.
  2. Use the submit command to submit your work. You can submit as many times as you want, but only the last one will be graded. A makefile without visual is required. Name your executable prog2.
  3. I will use the following approach to compile and test your programs:
    make                <-- make your program
    prog2 m n t         <-- test your program
    
    I may repeat the above procedure several times with different command line arguments to see if your program performs correctly.
  4. Your implementation should fulfill the program specification as stated. Any deviation from the specification will cause you to receive zero point.
  5. You should aim for maximum parallelism. That is, implement your solution as stated and make sure all required threads will execute simultaneously. Without doing so, you risk very low grade.
  6. No late submission will be graded.
  7. My rule of grading is deduction based. I assume you always receive 100%. If you missed something, some points will be taken off based on the following rules. Read these rules carefully. Otherwise, you could receive low or very low grade. Click here to see how your program will be graded.
  8. Since the rules are all clearly stated above, no leniency will be given. So, if you have anything in doubt, please ask for clarifications.
  9. Click here to see how your program will be graded.