Sei sulla pagina 1di 1

The problem-:

The dining philosophers problem is summarized as five philosophers sitting at a


table doing one of two things
eating or thinking.
While eating, they are not thinking, and while thinking, they are not eating.
The five philosophers sit at a circular table with a large bowl of spaghetti in
the center.
A fork is placed in between each philosopher, and as such, each philosopher has
one fork to his or her left and one fork to his or her right.
As spaghetti is difficult to serve and eat with a single fork, it must be assum
ed that in order for a philosopher to eat, the philosopher must have two forks.
In the case of the dining philosopher, the philosopher can only use the fork on
his or her left or right.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Algorithm
* The algorithm for the process is

.
1.Start.
2.Initialise the thread variable

s as required.
3.Create the threads representin
g philosophers.
4.Wait until the threads finishe
s execution.
5.Stop.
.
1.Start.
2.Wait for the left fork/spoon.
3.Wait for the right fork/spoon.
4.Start eating.
5 Release the left fork/spoon.
6.Release the right fork/spoon.
7.Stop.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program
The algorithm for the philosopher is as

/* Program to implement the solution to dinning philosophers

problem.

* This program uses five thread functions as five philosophers.


* The spoons or forks are represented with five semaphore variables.
* To use shared memory just include the code after the sem_wait() in each thr
ead.
* To compile use cc diningphilosopher.c -lpthread .
* To run ./a.out .
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------problem in it--->1.)The most serious problem of this program is that deadlock co
uld occur.
2.)Starvation is also a problem.

Potrebbero piacerti anche