Question: Dinning Philosophers problem The dinning philosophers problem is a classic synchronization problem. Though, it does not notably represent a real-world problem, it provides a significant

Dinning Philosophers problem

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.

Dinning Philosophers problem The dinning philosophers problem is a classic synchronization problem.

Problem: 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. 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: $ ./dphil 7 10 //7 philosophers, each eats 10 times total. Philosopher 0 is thinking... Philosopher 1 is eating... Philosopher 3 is thinking... Philosopher 4 is thinking... P3 Figure 6.11 Dining Arrangement for Philosophers P0 P2 P4 P1

Submit a single .c source code file of your solutions.

I should be able to compile your code as follows: gcc -o phils phils.c -lpthread

An example where n=5

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 Databases Questions!