Question: * * * * * Make sure to look at images i uploaded * * * * Submit a single . c source code file

*****Make sure to look at images i uploaded****
Submit a single .c source code file of your solutions. Do not forget to write your names(s).
I should be able to compile your code as follows:
gcc -o phils phils.c -lpthread
Criteria:
The number of philosophers (therefore chopsticks) and the number of eat times
should be dynamic. I will not accept a solution for a fix number of philosophers.
As stated above, you need to use semaphores, not mutexes in your solution (not
using semaphores will cause an automatic zero).
Use the sleep() function to simulate eating and thinking.
At start, all philosopher states should be randomized (between thinking and hungry).
** I would highly suggest using the template provided.
Dinning Philosophers' problem (150 points)
The dinning philosophers' problem is a classic synchronization problem. Though, it does not
notably represent a real-world problem, it provides a significant learning value, particularly in
process synchronization. It is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and starvation-free manner. There are
n philosophers dinning together at the same table. Each philosopher has their own place at
the table. There is a fork between each plate. The dish served is a kind of spaghetti which
has to be eaten with two forks. Each philosopher can only alternately think and eat.
Moreover, a philosopher can only eat their spaghetti when they have both a left and right
fork. Thus, two forks will only be available when their two nearest neighbors are thinking,
not eating. After an individual philosopher finishes eating, they will put down both forks.
An example where n=5
Write a C program (phils.c) that solves the dinning philosophers' problem where each
dinning philosopher is a modeled as a thread. Use semaphores (not mutexes) for
synchronization. Your program will take two arguments: the number of philosophers at the
table and a number of times to eat for each philosopher.
Care must be taken to prevent a deadlock. One possible solution to alleviate the deadlock is
known as "asymmetric solution", that is, an odd philosopher picks up first a left
chopstick and then the right one, while an even philosopher picks up first a right
chopstick and then the left one (or vice-versa). Your solution should exhibit some level
of fairness (A philosopher should not eat 10 times while another has not eaten a single time).
Example of output:
**********use this template**********
*******You have to fill this template out without removing any part of it*******
*******just fill in the TEMPLATE***********
#include
#include
#include
#include
#include
int numOfPhils; // number of phils, passed in as argument[1]
int numOfTimesToEat; // number of times to eat each, passed in as argument[2]
sem_t *chopsticks;
int *state;
int *phils;
// functions that may be helpful to create
void test(); // used to check state of philsopher and state of each chopstick
// if philospher is hungry and both left and right are satisifed
// then they should be able to eat now
void pickupChopstick(); // waits to grab chopsticks for philospher (denotes when philospher is hungry)
void putDownChopstick(); // puts chopsticks back down (denotes when philospher is thinking)
void *philosopher(); // must be a pointer when working with threading
// determines first action of a philospher when thread is created
int main(int argc, char *argv[]){
// thread usage
pthread_t threads[numOfPhils];
// for threads you will need to incorporate the concept of creating and
// joining to solve this problem
// memory allocation for chopsticks, state, and philosphers
chopsticks = malloc(numOfPhils * sizeof(sem_t));
state = malloc(numOfPhils * sizeof(int));
phils = malloc(numOfPhils * sizeof(int));
// create philosphers and give them a state based on numOfPhils
// then create a thread for each using
free(chopsticks);
free(state);
free(phils);
return 0;
}
 *****Make sure to look at images i uploaded**** Submit a single

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!