Dining Philosophers Problem

Started by
15 comments, last by xxx_shinobi 20 years, 10 months ago
Could someone give me a good solution for Dining Philosophers Problem in UNIX C, using semaphores and shared memory.
Advertisement
Homework assignment, right?
OS course. IPC and all the other bla bla bla .
I figured, I remember doing this one back in school. Any particular reason you just aren''t doing this one yourself?

It only takes 50 or so lines as I recall.
You studied in school unix ipc. What kind of school is that ???
I went to WPI. They cover this in an operating systems course, same as you're taking.

[edited by - cheesegrater on May 28, 2003 2:47:55 PM]
WPI is an institude,not a school. Anyway,do you know an efficient algorithm to my problem cos i have now power and time for it :-)
my C is a bit rusty, but here goes:

  void dine(int n){  if(n==0)  {    printf("Fuck it, let''s order pizza\n");  }  else  {    printf("I want pepperoni!\n");  }}int main(){  for(int i=0;i<5;i++)  {    if(fork())    {      dine(i);      return 0;    }  }  return 0;}  
Hey, we all called it a ''school'' while we were going there.

If you don''t have to worry about fairness, it''s sufficient to represent each utensil as a binary semaphore. You also add an additional semaphore you need to lock before picking up any utensils. That''ll make your philosophers eat one at a time and keep them from getting deadlocked.
utensil[0...4]

while(true)
{
think();
test(#utensils);
test(utensil);
take(utensil);<br> test(utensil[i+1 % 5]);<br> take (utensil[i+1 % 5]);<br> eat();<br> put_back(utensil);<br> free(utensil);<br> put_back(utensil);<br> free(utensil);<br> free(#utensils);<br>} </i> <br><br><br><br><SPAN CLASS=editedby>[edited by - Raab314159 &#111;n May 28, 2003 3:14:11 PM]</SPAN>

This topic is closed to new replies.

Advertisement