Objective: The dining philosophers is a classical problem to represent deadlocks and how to avoid them. Typically, you have multiple philosophers and they alternate between thinking about things, eating. In this version of the program we are only going to use 2 philosophers, and each one already has a chopstick. There will be 1 free chopstick to grab when they are ready to eat, and then put back down when they are done! This project is designed to teach you how to use semaphores with multiple threads!

Requirements: (This project will be done in C)

On the 2nd page you can see a skeleton of what your code should could be set up like, but the main requirements are as follows:

  • Your main loop (should be an infinite loop) should randomly choose a philosopher each iteration.
  • The randomly chosen philosopher will then randomly choose an action (either think, or eat)
  • You will have freedom to code the "think" function how you please, one consideration is to have the chosen philosopher randomly think about a topic from an array (see skeleton)

For the "eat" function:

  • Using semaphores, allow the first philosopher to enter the function and perform its critical section
  • The second philosopher must be trapped/or denied by the semaphore until the other philosopher finishes
  • For this function, to help see which philosopher is executing "eat" and which philosopher is trapped make sure to pass the names of the philosophers through the thread.

SAMPLE EXECUTION: see image.

SKELETON: **Hint you should only need these includes**

#include < stdio.h >
#include < stdlib.h >
#include < pthread.h >
#include < semaphore.h >

//declare your global variables
const char *a[2] = {"Socrates", "Plato"};
const char *topics[5] = {"boats", "the economy", "astronomy", "what to eat", "McDonald's Szechuan Sauce");
sem t semaphore;

void* eat(void* arg) {
/* allow the first philosopher to access their critical section (aka: "Socrates has grabbed the chopstick and started eating"). Have this philosopher eat for a random amount of time, but max 10 seconds*/
/*For the philosopher that gets trapped by the semaphore have a print statement such as "Socrates cannot grab the chopstick because it is in use*/ // At the end of the critical section don't forget to release the trapped philosopher!!
}

void think(int phil) {
// have the chosen philosopher think about something.
}

int main() {
//initialize your semaphore
//initialize your thread variables
while(1) {
//randomly decide which philosopher's turn it is
//randomly decide what action that philosopher will take
if(action == "think") {
// have the philosopher perform the think action
} else {
// this will be the "eat" block
if(randomPhilsopher == Plato) {
//create a thread passing Plato's name to eat function
// create a thread passing Socrates name to eat function
} else {
// create a thread passing Socrates name to eat function
//create a thread passing Plato's name to eat function
}
}
}
//should never reach here.
return 0;
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.