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 namess
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 points
The dinning philosophers' problem is a classic synchronization problem. Though, it does not
notably represent a realworld 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 deadlockfree and starvationfree manner. There are
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
Write a C program philsc 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 viceversa Your solution should exhibit some level
of fairness A philosopher should not eat 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
int numOfTimesToEat; number of times to eat each, passed in as argument
semt 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 mainint argc, char argv
thread usage
pthreadt threadsnumOfPhils;
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 mallocnumOfPhils sizeofsemt;
state mallocnumOfPhils sizeofint;
phils mallocnumOfPhils sizeofint;
create philosphers and give them a state based on numOfPhils
then create a thread for each using
freechopsticks;
freestate;
freephils;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
