The Hungry Students concurrent programming assignment using semaphores is summarized below.
Consider a dorm floor of hungry undergraduate students. To help save students money, the dorm RA has cooked a small pot that can hold MAX_SERVINGS of Ramen Noodles. When a student wants to eat, the student can get a serving from the pot, unless it is empty. If the pot is empty, the students wakes up the RA and then waits until the RA has refilled the pot.
The synchronization constraints include:
In this problem, the RA represents the operating system and the students represent the processes. The operating system should allocate the required resources to the processes and, at the same time, avoid deadlock.
Implement the Hungry Students problem in C (not C++). The program must compile and execute under Ubuntu.
Global variables/constants (and no others)
unsigned integers
REFILL_SIZE = 5
MAX_REFILLS = 5
currPotServings
currPotRefills
semaphores
studentMtx
empty pot
full pot
The main() function should perform the following actions:
The RA() function will perform the following actions:
while true
P(emptyPot)
putServingsInPot()
V(fullPot)
The hungryStudents() function should perform the following actions.
while true
P(studentMtx)
if currPortServings == 0
V(emptyPot)
P(fullPot)
getServingFromPot()
V(studentMtx)
studentEats(studentNumber)
In addition to the basic synchronization outlined, you will need to add the code to count the number of times the pot has been re-filled. When the pot has been re-filled MAX_REFILLS times, the RA and student threads should terminate.
The various support functions include
The RA messages should be printed in red and the student messages printed in green.
printf("\033[0;31mI'm a message in red\033[0m\n", 0); // red
printf("\033[0;32mI'm a message in green\033[0m\n", 0); // green
In order to use threading functions and semaphores in C, you will need the below include files:
#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < signal.h >
#include < pthread.h >
#include < semaphore.h >
#include < time.h >
#include < stdbool.h >
Use the following compiler options
gcc -Wall -pedantic -pthread -o hungryStudents hungryStudents.c
ed-vm% ./hungryStudents 3
Hungry Students
Student Count: 3
Refill Count: 5
OK, fine, here are some more Ramen Noodles (5)...
Student 0 eating, yum...
Student 2 eating, yum...
Student 1 eating, yum...
Student 0 eating, yum...
Student 2 eating, yum...
OK, fine, here are some more Ramen Noodles (5)...
Student 1 eating, yum...
Student 2 eating, yum...
Student 0 eating, yum...
Student 2 eating, yum...
Student 0 eating, yum...
OK, fine, here are some more Ramen Noodles (5)...
Hey, this is the last of the Ramen Noodles...
Student 1 eating, yum...
Student 2 eating, yum...
Student 0 eating, yum...
Student 0 eating, yum...
Student 1 eating, yum...
Game over, thank you for playing.
ed-vm%
Note, actual mileage may vary.