#### Archived

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

# Dining Philosophers Problem

This topic is 5530 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]

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002063
×