Tech Post 1: Solution to Dining Philosphers problem:

INTRODUCTION:

Five silent philosophers sit at a table around a bowl of spaghetti. A fork is placed between each pair of adjacent philosophers.

Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when he has both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it’s not being used by another philosopher. After he finishes eating, he needs to put down both the forks so they become available to others. A philosopher can grab the fork on his right or the one on his left as they become available, but can’t start eating before getting both of them.

Eating is not limited by the amount of spaghetti left: assume an infinite supply. An alternative problem formulation uses rice and chopsticks instead of spaghetti and forks.

The problem is how to design a discipline of behavior (a concurrent algorithm) such that each philosopher won’t starve, i.e. can forever continue to alternate between eating and thinking, assuming that any philosopher cannot know when others may want to eat or think.

 Algorothm:

Dining Philosophers

N=6 philosophers sitting at a circular dining table, each with a plate in front of him/her

One fork between two adjacent philophers

Each goes through following routine over and over again:

(1) Think for some time (randomise);

(2) Then feels hungry;

(3) Lift  Left&Right fork s(wait if not available);

(4) Eat for sometime (randomise);

(5) Keep both forks down;

(6) Snooze for sometime (randomise)

Demo logs: Start of thinking, Start of feeling hungry, Getting each fork, Start of eating, End of Eating & keeping forks down, Start of snooze

After each statement in your program, call SLOW()

To randomise time spent in each phase, use spendTime( int milliseconds )

Code for solution in threads using trylock/unlock:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<math.h>
//#include<dos.h>
void *func(int* n);
pthread_t philosopher[5];
pthread_mutex_t chopstick[5];
void random_wait()
{
int time = rand()%10;
time*=9910000;
int i;
for(i=0;i
}
int main()
{
int i,k;
void *msg;
for(i=0;i<5;i++)
{
k=pthread_mutex_init(&chopstick[i],NULL);
if(k==-1)
{
printf(“\n Mutex initialization failed”);
exit(1);
}
}
for(i=0;i<5;i++)
{
/*k=pthread_create(&philosopher[i],NULL,(void *)func,(int *)i);*/
if(pthread_create(&philosopher[i],NULL,(void *)func,(int *)&i)!=0)
{
printf(“\n Thread creation error \n”);
exit(1);
}
}
for(i=0;i<5;i++)
{
k=pthread_join(philosopher[i],&msg);
if(k!=0)
{
printf(“\n Thread join failed \n”);
exit(1);
}
}
for(i=0;i<5;i++)
{
k=pthread_mutex_destroy(&chopstick[i]);
if(k!=0)
{
printf(“\n Mutex Destroyed \n”);
exit(1);
}
}
return 0;
}

void *func(int *n)
{
/*printf(“\nPhilosopher %d is thinking “,n);
pthread_mutex_trylock(&chopstick[n]);//when philosopher 5 is eating he takes fork 1 and fork 5
pthread_mutex_trylock(&chopstick[(n+1)%5]);
printf(“\nPhilosopher %d is eating “,n);
random_wait(rand()%10);
pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n+1)%5]);
printf(“\nPhilosopher %d Finished eating “,n);
*/
while (1)
{
printf(“\n THINKING P%d”,*n);
random_wait();
printf(“\nfinished thinking P%d\n”,*n);

int rv=pthread_mutex_trylock(&chopstick[*n]);

if(0!=rv){random_wait(); printf(“\nP%d   unsuccessful in picking right chopsick”,*n,*n);continue;}
printf(“\nP%d picked the right chopstick %d\n”, *n,*n);

if (0!=pthread_mutex_trylock(&chopstick[(*n+1)%5]))
{
pthread_mutex_unlock(&chopstick[*n]);
printf(“P%d failed to pick up left (%d) and released the right(%d) chopstick\n”,*n,(*n+1)%5,*n);
random_wait();
continue;
}
printf(“P%d picked up %d and %d”,*n,*n,*n+1);
printf(“\nP%d is eating  from plate using %d, %d chopstick\n”,*n, *n, (*n+1)%5);
random_wait();

pthread_mutex_unlock(&chopstick[*n]);
pthread_mutex_unlock(&chopstick[(*n+1)%5]);
random_wait();
}

}

I hope it helps you.

Posted on February 13, 2013, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: