#### Archived

This topic is now archived and is closed to further replies.

# Dining Philosophers Problem

This topic is 5314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Could someone give me a good solution for Dining Philosophers Problem in UNIX C, using semaphores and shared memory.

##### Share on other sites
Homework assignment, right?

##### Share on other sites
OS course. IPC and all the other bla bla bla .

##### Share on other sites
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.

##### Share on other sites
You studied in school unix ipc. What kind of school is that ???

##### Share on other sites
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]

##### Share on other sites
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 :-)

##### Share on other sites
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;}

##### Share on other sites
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.

##### Share on other sites
utensil[0...4]

while(true)
{
think();
test(#utensils);
test(utensil);
take(utensil[i]);
test(utensil[i+1 % 5]);
take (utensil[i+1 % 5]);
eat();
put_back(utensil[i]);
free(utensil[i]);
put_back(utensil[i + 1 % 5]);
free(utensil[i + 1 % 5]);
free(#utensils);
}

[edited by - Raab314159 on May 28, 2003 3:14:11 PM]

##### Share on other sites
Just shoot the philosophers and take all the food for yourself. They really shouldn''t have been bickering about the silverware in the first place.

##### Share on other sites
quote:
Original post by null_pointer
Just shoot the philosophers and take all the food for yourself. They really shouldn''t have been bickering about the silverware in the first place.

But the philosophers use chop sticks not silverware.

##### Share on other sites
there is no spoon

##### Share on other sites
quote:

WPI is an institude, not a school.

So is MIT, but most people consider both universities since they offer 4 year degrees. A CS degree from a technical school usually carries more weight than one from a liberal arts school.

##### Share on other sites
How about you simply throw in an extra set of chopsticks and let the philosophers deal with that?

Hell, if they can set up a meeting and order chinese food for 5, they sure as hell can get an other set of chopsticks! Bloody philosophers... messing us up to find a solution to their problem because they are too lazy to do it and just want to go about their own buisness...

Hey, I think there''s a lesson to be learned from all this...

##### Share on other sites
Why are you eating with philosophers if you''re a CS major?

##### Share on other sites
The solution is in every threading book in existence. e.g. Posix Threads on Addison Wesley.

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984325

• 23
• 11
• 9
• 13
• 14