Question: need help with my computer science assignment The Dining Philosophers Problem The Dining Philosophers Problem In this project, you need to write a program to
need help with my computer science assignment
The Dining Philosophers Problem
The Dining Philosophers Problem
In this project, you need to write a program to simulate the famous dining philosophers problem.
This problem will require implementing a solution using Pthreads mutex locks and condition variables.
Begin by creating five philosophers, each identified by a number 0, 1, 2, 3 and 4. Each philosopher will run as a separate thread.
Philosophers alternate between thinking and eating. To simulate both activities, have the thread sleep for a random period from one to three seconds.
Each philosopher should think for a while and then become hungry.
If the philosopher is able to eat, the job she should do is go sleeping.
When a philosopher wishes to eat, she invokes the function
Philosopher number identifies the ID of the philosopher wishing to eat.
When a philosopher finishes eating, she invokes
When we want to make a philosopher try to eat, she invokes the function test(int philosopher_number)
Since Pthreads is typically used in C programsand since C does not have a monitor we accomplish locking by associating a condition variable with a mutex lock.
Condition variables in Pthreads use the pthread_cond_t data type and are initialized using the pthread_cond_init() function.
E.g.
The thread of each philosopher will be created and joined in order.
0, 1, 2, , 4
Use a philosophers function as the input of pthread_create() to control the philosophers actions.
The thread of each philosopher will be created and joined in order.
0, 1, 2, , 4
Use a function as the input of pthread_create() to control the philosophers actions.
You should print these lines out in the correct situations:
Philosopher %d is now THINKING for %d seconds.
Philosopher %d is now HUNGRY and trying to pick up forks.
Philosopher %d cant pick up forks and start waiting.
Philosopher %d returns forks and then starts TESTING %d and %d.
Philosopher %d is now EATING.
the output i need must be as above and i need the code to be executed in linux.kindly help out
rohan@rohan-VirtualBox /Desktop$ ./hw3.out Philosopher 0 is now THINKING for 2 seconds Philosopher 1 is now THINKING for 2 seconds Philosopher 2 is now THINKING for 1 seconds Philosopher 3 is now THINKING for 2 seconds Philosopher 4 is now THINKING for 3 seconds Philosopher 2 is now HUNGRY and trying to pick up forks. Philosopher 2 IS NOW EATING Philosopher 0 is now HUNGRY and trying to pick up forks. Philosopher 0 IS NOW EATING Philosopher 1 is now HUNGRY and trying to pick up forks. Philosopher 1 fails to pick up forks and then starts waiting Philosopher 3 is now HUNGRY and trying to pick up forks. Philosopher 3 fails to pick up forks and then starts waiting. Philosopher 4 is now HUNGRY and trying to pick up forks. Philosopher 4 fails to pick up forks and then starts waiting. Philosopher 2 returns forks and then starts TESTING 1 and 3. Philosopher 3 IS NOW EATING Philosopher o returns forks and then starts TESTING 4 and 1. Philosopher 1 IS NOW EATING Philosopher 3 returns forks and then starts TESTING 2 and 4. Philosopher 4 IS NOW EATING. Philosopher 1 returns forks and then starts TESTING 0 and 2. Philosopher 4 returns forks and then starts TESTING 3 and rohan@rohan-VirtualBox:~/Desktops
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
