Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions

Question

=+ Have they changed the way employers view IP?

Answered: 1 week ago