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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites