Dining Philosopher\'s problem with user input for number of philosopher\'s and t
ID: 3837158 • Letter: D
Question
Dining Philosopher's problem with user input for number of philosopher's and the number of times they eat. The code compiles, but i do not get the expected result. Any help in fixing this would be greatly appreciated.
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define THINKING 0
#define EATING 2
#define LEFT (ph_num+(A-1))%A
#define RIGHT (ph_num+1)%A
sem_t mutex;
sem_t S[32];
void * philospher(void *arg_l);
void take_chop(int,int);
void put_chop(int,int);
void test(int,int);
int state[32];
int phil_num[12]={0,1,2,3,4,5,6,7,8,9,10,11};
int main()
{
int i,A,B;
scanf("%10d", &A);
scanf("%10d", &B);
printf("You got %d phils and %d turns ",A,B);
int args[3];
args[0] = A;
args[1] = B;
pthread_t thread_id[A];
sem_init(&mutex,0,1);
for(i=0;i<A;i++)
sem_init(&S[i],0,0);
for(i=0;i<A;i++)
{
int L = phil_num[i];
args[2] = L;
pthread_create(&thread_id[i],NULL,philospher,&args);
printf("Philosopher %d is thinking ",i+1);
}
for(i=0;i<A;i++)
pthread_join(thread_id[i],NULL);
}
void *philospher(void *arg_l)
{
int *args = arg_l;
int A = args[0];
int B = args[1];
int b = 0;
while(b != B)
{
int i = args[2];
sleep(1);
take_chop(i,A);
sleep(0);
put_chop(i,A);
b++;
}
}
void take_chop(int ph_num,int A)
{
sem_wait(&mutex);
test(ph_num,A);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}
void test(int ph_num,int A)
{
if (state[LEFT] != EATING && state[RIGHT] != EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d takes chop %d and %d ",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is Eating ",ph_num+1);
sem_post(&S[ph_num]);
}
}
void put_chop(int ph_num,int A)
{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting chop %d and %d down ",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinking ",ph_num+1);
test(LEFT,A);
test(RIGHT,A);
sem_post(&mutex);
}
Explanation / Answer
Description :
The input to the program is the number of philosophers to be seated around the table. Output shows the various stages that each philosopher passes through within a certain time. A philosopher can be in anyone of the three stages at a time: thinking, eating or finished eating.
Data Structures and Functions used in the code :
1.The main data structures used here are: Arrays.
2. The arrays represent the philosophers and corresponding chopsticks for them.Each element in the philosopher’s array corresponds to a thread and each element in the chopstick’s array corresponds to a mutex variable.
3.The functions used here are:
pthread_mutex_init (&mutex, NULL) – initialization of mutex variable
pthread_mutex_lock (&mutex) – attempt to lock a mutex
pthread_mutex_unlock (&mutex) – unlock a mutex
pthread_create (ptr to thread, NULL, (void*) func, (void*) )
pthread_join (ptr to thread, &msg)-This function will make the main program wait until the called thread is finished executing it’s task.
pthread_mutex_destroy (ptr to thread)-
pthread_exit(NULL)
Note: while compiling this program use the following:
[root@Linux test]# gcc –o dining dining.c -lpthread
Algorithm for process:
1. Start.
2. Declare and initialize the thread variables (philosophers) as required.
3. Declare and initialize the mutex variables (chopsticks) as required.
4. Create the threads representing philosophers.
5. Wait until the threads finish execution.
6. Stop.
Algorithm for thread (philosopher i) function:
Start.
Philosopher i is thinking.
Lock the left fork spoon.
Lock the right fork spoon.
Philosopher i is eating.
sleep
Release the left fork spoon.
Release the right fork spoon.
Philosopher i Finished eating.
Stop.
Code :
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
void *func(int n);
pthread_t philosopher[5];
pthread_mutex_t chopstick[5];
int main()
{
int i,k;
void *msg;
for(i=1;i<=5;i++)
{
k=pthread_mutex_init(&chopstick[i],NULL);
if(k==-1)
{
printf(“ Mutex initialization failed”);
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_create(&philosopher[i],NULL,(void *)func,(int *)i);
if(k!=0)
{
printf(“ Thread creation error ”);
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_join(philosopher[i],&msg);
if(k!=0)
{
printf(“ Thread join failed ”);
exit(1);
}
}
for(i=1;i<=5;i++)
{
k=pthread_mutex_destroy(&chopstick[i]);
if(k!=0)
{
printf(“ Mutex Destroyed ”);
exit(1);
}
}
return 0;
}void *func(int n)
{
printf(“ Philosopher %d is thinking “,n);
pthread_mutex_lock(&chopstick[n]);//when philosopher 5 is eating he takes fork 1 and fork 5
pthread_mutex_lock(&chopstick[(n+1)%5]);
printf(“ Philosopher %d is eating “,n);
sleep(3);
pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n+1)%5]);
printf(“ Philosopher %d Finished eating “,n);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.