AIM:
To write a program to implement dining philosophers problem
ALGORITHM:
- start
- create 5 semaphores and keep them in an array sem[ ]
- initialize each semaphore by value 1
- for i=0 to 4 do steps 5 and 6
- create a child process using fork( )
- call the function philosopher(i )by the child process
- wait for a key press by a parent process
- kill all processes
- stop
function philosopher (id)
- start
- take forks if state is hungry.if forks are not available wait until it is available.it is done by down operation on semaphore
- if forks are ready eat food.
- after completion of eating,put forks,it is done by an up operation on semaphore
- think philosopher.it is done by calling sleeping function for a delay
- if again hungry, go to step 2.this process continues in an infinite loop.
function getfork(id)
- start
- take left fork,it is done by calling down operation like down( semaphore[ id ] )
- take right fork by calling down ( semaphore[( id+1)%5])
- return to called program
function putfork(id)
- start
- put right fork by up(semaphore [ (id + 1)%5] )
- put left fork by up ( semaphore [id ])
- return
Leave a comment