dining philosophers problem

24 06 2008

AIM:

To write a program to implement dining philosophers problem

ALGORITHM:

  1. start
  2. create 5 semaphores and keep them in an array sem[ ]
  3. initialize each semaphore by value 1
  4. for i=0 to 4 do steps 5 and 6
  5. create a child process using fork( )
  6. call the function philosopher(i )by the child process
  7. wait for a key press by a parent process
  8. kill all processes
  9. stop

function philosopher (id)

  1. start
  2. take forks if state is hungry.if forks are not available wait until it is available.it is done by down operation on semaphore
  3. if forks are ready eat food.
  4. after completion of eating,put forks,it is done by an up operation on semaphore
  5. think philosopher.it is done by calling sleeping function for a delay
  6. if again hungry, go to step 2.this process continues in an infinite loop.

function getfork(id)

  1. start
  2. take left fork,it is done by calling down operation like down( semaphore[ id ] )
  3. take right fork by calling down ( semaphore[( id+1)%5])
  4. return to called program

function putfork(id)

  1. start
  2. put right fork by up(semaphore [ (id + 1)%5] )
  3. put left fork by up ( semaphore [id ])
  4. return

Actions

Information

Leave a comment




Design a site like this with WordPress.com
Get started