C - Program to Implement Dinning Philosophers Problem
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
pthread_mutex_t work_mutex[5];
struct chops
{
int left;
int right;
}p[5];
void *philosopher5()
{
while(1)
{
if((p[5].left==1) || (p[5].right==1))
{
printf("\n\tPhilosopher5 is thinking...");
sleep(2);
}
if((p[5].left==0) && (p[5].right==0))
{
pthread_mutex_lock(&work_mutex[4]);
pthread_mutex_lock(&work_mutex[0]);
p[5].left=1;
p[1].right=1;
p[5].right=1;
p[4].left=1;
printf("\n\tPhilosopher5 is eating....");
sleep(6);
printf("\n\tphilosopher5 is releasing chopstics.");
p[5].left=0;
p[1].right=0;
p[5].right=0;
p[4].left=0;
pthread_mutex_unlock(&work_mutex[4]);
pthread_mutex_unlock(&work_mutex[0]);
sleep(5);
}
}
}
void *philosopher4()
{
while(1)
{
if((p[4].left==1) || (p[4].right==1))
{
printf("\n\tphilosopher4 is thinking...");
sleep(2);
}
if((p[4].left==0) && (p[4].right==0))
{
pthread_mutex_lock(&work_mutex[3]);
pthread_mutex_lock(&work_mutex[4]);
p[4].left=1;
p[5].right=1;
p[4].right=1;
p[3].left=1;
printf("\n\tPhilosopher4 is eating....");
sleep(6);
printf("\n\tphilosopher4 is releasing chopstics.");
p[4].left=0;
p[5].right=0;
p[4].right=0;
p[3].left=0;
pthread_mutex_unlock(&work_mutex[3]);
pthread_mutex_unlock(&work_mutex[4]);
sleep(5);
}
}
}
void *philosopher3()
{
while(1)
{
if((p[3].left==1) || (p[3].right==1))
{
printf("\n\tPhilosopher3 is thinking...");
sleep(2);
}
if((p[3].left==0) && (p[3].right==0))
{
pthread_mutex_lock(&work_mutex[3]);
pthread_mutex_lock(&work_mutex[2]);
p[3].left=1;
p[4].right=1;
p[3].right=1;
p[2].left=1;
printf("\n\tPhilosopher3 is eating....");
sleep(6);
printf("\n\tphilosopher3 is releasing chopstics.");
p[3].left=0;
p[4].right=0;
p[3].right=0;
p[2].left=0;
pthread_mutex_unlock(&work_mutex[3]);
pthread_mutex_unlock(&work_mutex[2]);
sleep(5);
}
}
}
void *philosopher2()
{
while(1)
{
if((p[2].left==1) || (p[2].right==1))
{
printf("\n\tPhilosopher2 is thinking...");
sleep(2);
}
if((p[2].left==0) && (p[2].right==0))
{
pthread_mutex_lock(&work_mutex[2]);
pthread_mutex_lock(&work_mutex[1]);
p[2].left=1;
p[3].right=1;
p[2].right=1;
p[1].left=1;
printf("\n\tPhilosopher2 is eating....");
sleep(6);
printf("\n\tphilosopher2 is releasing chopstics.");
p[2].left=0;
p[3].right=0;
p[2].right=0;
p[1].left=0;
pthread_mutex_unlock(&work_mutex[2]);
pthread_mutex_unlock(&work_mutex[1]);
sleep(5);
}
}
}
void *philosopher1()
{
while(1)
{
if((p[1].left==1) || (p[1].right==1))
{
printf("\n\tPhilosopher1 is thinking...");
sleep(2);
}
if((p[1].left==0) && (p[1].right==0))
{
pthread_mutex_lock(&work_mutex[1]);
pthread_mutex_lock(&work_mutex[0]);
p[1].left=1;
p[2].right=1;
p[1].right=1;
p[5].left=1;
printf("\n\tPhilosopher1 is eating....");
sleep(6);
printf("\n\tphilosopher1 is releasing chopstics.");
p[1].left=0;
p[2].right=0;
p[1].right=0;
p[5].left=0;
pthread_mutex_unlock(&work_mutex[1]);
pthread_mutex_unlock(&work_mutex[0]);
sleep(5);
}
}
}
main()
{
int res,i;
pthread_t ph1,ph2,ph3,ph4,ph5;
for(i=0;i<5;i++)
{
res=pthread_mutex_init(&work_mutex[i],NULL);
if(res!=0)
{
printf("\n\tError in creation of mutex.");
//exit(EXIT_FAILURE);
}
}
for(i=1;i<=5;i++)
{
p[i].left=0;
p[i].right=0;
}
printf("\n\tProgram to demonstrate thread:");
printf("\n\tCreating 5 new threads...");
pthread_create(&ph1,NULL,philosopher1,NULL);
pthread_create(&ph2,NULL,philosopher2,NULL);
pthread_create(&ph3,NULL,philosopher3,NULL);
pthread_create(&ph4,NULL,philosopher4,NULL);
pthread_create(&ph5,NULL,philosopher5,NULL);
printf("\n\tWaiting for thread to join....");
res=pthread_join(ph1,NULL);
exit(EXIT_SUCCESS);
}
#include<unistd.h>
#include<pthread.h>
pthread_mutex_t work_mutex[5];
struct chops
{
int left;
int right;
}p[5];
void *philosopher5()
{
while(1)
{
if((p[5].left==1) || (p[5].right==1))
{
printf("\n\tPhilosopher5 is thinking...");
sleep(2);
}
if((p[5].left==0) && (p[5].right==0))
{
pthread_mutex_lock(&work_mutex[4]);
pthread_mutex_lock(&work_mutex[0]);
p[5].left=1;
p[1].right=1;
p[5].right=1;
p[4].left=1;
printf("\n\tPhilosopher5 is eating....");
sleep(6);
printf("\n\tphilosopher5 is releasing chopstics.");
p[5].left=0;
p[1].right=0;
p[5].right=0;
p[4].left=0;
pthread_mutex_unlock(&work_mutex[4]);
pthread_mutex_unlock(&work_mutex[0]);
sleep(5);
}
}
}
void *philosopher4()
{
while(1)
{
if((p[4].left==1) || (p[4].right==1))
{
printf("\n\tphilosopher4 is thinking...");
sleep(2);
}
if((p[4].left==0) && (p[4].right==0))
{
pthread_mutex_lock(&work_mutex[3]);
pthread_mutex_lock(&work_mutex[4]);
p[4].left=1;
p[5].right=1;
p[4].right=1;
p[3].left=1;
printf("\n\tPhilosopher4 is eating....");
sleep(6);
printf("\n\tphilosopher4 is releasing chopstics.");
p[4].left=0;
p[5].right=0;
p[4].right=0;
p[3].left=0;
pthread_mutex_unlock(&work_mutex[3]);
pthread_mutex_unlock(&work_mutex[4]);
sleep(5);
}
}
}
void *philosopher3()
{
while(1)
{
if((p[3].left==1) || (p[3].right==1))
{
printf("\n\tPhilosopher3 is thinking...");
sleep(2);
}
if((p[3].left==0) && (p[3].right==0))
{
pthread_mutex_lock(&work_mutex[3]);
pthread_mutex_lock(&work_mutex[2]);
p[3].left=1;
p[4].right=1;
p[3].right=1;
p[2].left=1;
printf("\n\tPhilosopher3 is eating....");
sleep(6);
printf("\n\tphilosopher3 is releasing chopstics.");
p[3].left=0;
p[4].right=0;
p[3].right=0;
p[2].left=0;
pthread_mutex_unlock(&work_mutex[3]);
pthread_mutex_unlock(&work_mutex[2]);
sleep(5);
}
}
}
void *philosopher2()
{
while(1)
{
if((p[2].left==1) || (p[2].right==1))
{
printf("\n\tPhilosopher2 is thinking...");
sleep(2);
}
if((p[2].left==0) && (p[2].right==0))
{
pthread_mutex_lock(&work_mutex[2]);
pthread_mutex_lock(&work_mutex[1]);
p[2].left=1;
p[3].right=1;
p[2].right=1;
p[1].left=1;
printf("\n\tPhilosopher2 is eating....");
sleep(6);
printf("\n\tphilosopher2 is releasing chopstics.");
p[2].left=0;
p[3].right=0;
p[2].right=0;
p[1].left=0;
pthread_mutex_unlock(&work_mutex[2]);
pthread_mutex_unlock(&work_mutex[1]);
sleep(5);
}
}
}
void *philosopher1()
{
while(1)
{
if((p[1].left==1) || (p[1].right==1))
{
printf("\n\tPhilosopher1 is thinking...");
sleep(2);
}
if((p[1].left==0) && (p[1].right==0))
{
pthread_mutex_lock(&work_mutex[1]);
pthread_mutex_lock(&work_mutex[0]);
p[1].left=1;
p[2].right=1;
p[1].right=1;
p[5].left=1;
printf("\n\tPhilosopher1 is eating....");
sleep(6);
printf("\n\tphilosopher1 is releasing chopstics.");
p[1].left=0;
p[2].right=0;
p[1].right=0;
p[5].left=0;
pthread_mutex_unlock(&work_mutex[1]);
pthread_mutex_unlock(&work_mutex[0]);
sleep(5);
}
}
}
main()
{
int res,i;
pthread_t ph1,ph2,ph3,ph4,ph5;
for(i=0;i<5;i++)
{
res=pthread_mutex_init(&work_mutex[i],NULL);
if(res!=0)
{
printf("\n\tError in creation of mutex.");
//exit(EXIT_FAILURE);
}
}
for(i=1;i<=5;i++)
{
p[i].left=0;
p[i].right=0;
}
printf("\n\tProgram to demonstrate thread:");
printf("\n\tCreating 5 new threads...");
pthread_create(&ph1,NULL,philosopher1,NULL);
pthread_create(&ph2,NULL,philosopher2,NULL);
pthread_create(&ph3,NULL,philosopher3,NULL);
pthread_create(&ph4,NULL,philosopher4,NULL);
pthread_create(&ph5,NULL,philosopher5,NULL);
printf("\n\tWaiting for thread to join....");
res=pthread_join(ph1,NULL);
exit(EXIT_SUCCESS);
}