Help needed to solve problem with given code. language is C and should compile i
ID: 3815801 • Letter: H
Question
Help needed to solve problem with given code. language is C and should compile in PuTTY.
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5 //the number of philosophers
sem_t S[N]; //semaphores for chopsticks
void * philospher(void *num);
void take_chopsticks(int);
void put_chopsticks(int);
int phil_num[N]={0,1,2,3,4}; //philosopher ID
int main()
{
int i;
pthread_t thread_id[N];
for(i=0;i<N;i++)
sem_init(&S[i],0,1);
for(i=0;i<N;i++)
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}
void *philospher(void *num)
{
while(1)
{
int *i = num;
take_chopsticks(*i);
put_chopsticks(*i);
}
}
void take_chopsticks(int ph_num)
{
printf(
"Philosopher %d is Hungry ",ph_num);
sem_wait(&S[ph_num]); //take the left chopstick
printf("Philosopher %d takes chopstick%d ",ph_num, ph_num);
sleep(1);
sem_wait (&S[(ph_num+1)%N]); //take the right chopstick
printf("Philosopher %d takes chopstick%d ",ph_num,(ph_num+1)%N);
printf("Philosopher %d is eating ",ph_num);
sleep(1);
}
void put_chopsticks(int ph_num)
{
sem_post (&S[ph_num]); //put the left chopstick
printf("Philosopher %d putting chopstick%d ",ph_num, ph_num);
sleep(1);
sem_post (&S[(ph_num+1)%N]); //put the right chopstick
printf("Philosopher %d putting chopstick%d ",ph_num,(ph_num+1)%N);
printf("Philosopher %d is thinking ",ph_num);
sleep(1);
}
Explanation / Answer
Ans:
# include<stdio.h> // STDIO.H
# include<pthread.h> // pthread
# include<stdlib.h> // stdlib
# include<unistd.h> // unistd
# include<ctype.h> // ctype
# include<sys/types.h> // sys/types
# include<sys/wait.h> // wait
# include<semaphore.h> // semaphore.h
// defines N 5
#define N 5
// left
#define LEFT (i+4)%N
// right
#define RIGHT (i+1)%N
// thinking
#define THINKING 0
// hungry
#define HUNGRY 1
// eating_2
#define EATING 2
// spoon
sem_t spoon;
// semaphore phil
sem_t phil[N];
// int state[N]
int state[N];
// 0,1,2,3,4
int phil_num[N]={0,1,2,3,4};
// fd[N][2]
int fd[N][2]; // file descriptors for the pipes
pid_t pid, pids[N]; // proces_id's
// int i
int i;
//int num
int num;
// void phil
void philosopher(int i);
// test
void test(int i);
void take_spoon(int i);
// spoon
void put_spoon(int i);
// buffer char
char buffer[100];
// main
int main(void)
{
// for loop
for(i=0;i<N;++i)
{
// pipe
pipe(fd[i]);
// pids
pids[i] = fork();
// printf
printf("i=%d ",i);
// prints pids
printf("pids[i]=%d ",pids[i]);
// pid[i] == 0
if(pids[i]==0)
{
//for the child
//dup2
dup2(fd[i][1],1);
// close
close(fd[i][0]);
// close [i] [1]
close(fd[i][1]);
// phil(i)
philosopher(i);
// exit
_exit(0);
}
// else
else if(pids[i]>0)
{
//for the parent
// dup2 fd,0
dup2(fd[i][0],0);
// close
close(fd[i][0]);
// close [i][1]
close(fd[i][1]);
}
}
// wait for the child proceses to become end
// for loop , wait
for(i=0;i<N;++i) waitpid(pids[i],NULL,0);
// return
return 0;
}
// void pholospher()
void philosopher(int i)
{
// while 1
while(1)
{
// sleep(1)
sleep(1);
// spoon(1)
take_spoon(i);
// sleep2
sleep(2);
// put spoon
put_spoon(i);
// sleep 1
sleep(1);
}
}
// void take spoon fun
void take_spoon(int i)
{
// wait
sem_wait(&spoon);
// hungry
state[i] = HUNGRY;
// prints hungry
printf("philosopher %d hungry ",i+1);
// tets
test(i);
// // post
sem_post(&spoon);
// wait. phil[1]
sem_wait(&phil[i]);
}
// put-sppoon()
void put_spoon(int i)
{
// semaphore_wait
sem_wait(&spoon);
// thinking cond
state[i] = THINKING;
// prints message
printf("philosopher %d put_down the spoon %d & %d hin ",i+1,LEFT+1,i+1);
// prints message
printf("philosophr %d think's ",i+1);
// test_)left
test(LEFT);
// test_right
test(RIGHT);
// sem post()
sem_post(&spoon);
}
// void_test()
void test(int i)
{
// if hungry and not eating
if( state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
{
// eating
state[i] = EATING;
// prints the condition
printf("philosopher %d take's the spoon %d & %d ",i+1,LEFT+1,i+1);
// prints eats
printf("philosophr %d eat's ",i+1);
// sem post phil[i]
sem_post(&phil[i]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.