Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need some help fixing these error , they are driving me crazy the program is in C, and has these errors ------ what program

I need some help fixing these error , they are driving me crazy the program is in C, and has these errors ------

what program should do----

Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers' problem by using Pthreads mutex locks and condition variables. To implement the program, please follow the hints below. (a) The detailed 'Dining-Philosophers' problem statement: Refer to Page 71 ~ Page 72 in 'Lecture-08.pptx' (b) The introduction to Pthreads mutex locks and condition variables: Refer to Page 17 ~ Page 39 in 'Lecture-08.pptx' (c) Creating five philosophers, each of which will run as a separate thread. Philosophers alternate between thinking and eating: The activities of thinking and eating can be simulated by having the thread sleep for a random period between one and three seconds. When a philosopher wishes to eat, he/she invokes the function: Pickup_Forks (int Philosopher_ID) When a philosopher finishes eating, he/she invokes the function: Return_Forks (int Philosopher_ID) There are many possible output sequences. Here is one possible output sequence. Note: I place the output sequence into two columns, actually the five on the left column should be placed before the rest five on the right column. ./Dining-Philosophers Philosopher 0 is eating Philosopher 1 is eating Philosopher 2 is eating Philosopher 1 is thinking Philosopher 2 is thinking Philosopher 3 is thinking Philosopher 3 is eating Philosopher 4 is eating Philosopher 0 is thinking Philosopher 4 is thinking

-----------errors -------

prog.c: In function 'main': prog.c:53:54: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] pthread_create( &philosophers[i], NULL, philosopher, (void*)i); ^ prog.c:53:41: warning: passing argument 3 of 'pthread_create' from incompatible pointer type [-Wincompatible-pointer-types] pthread_create( &philosophers[i], NULL, philosopher, (void*)i); ^ In file included from prog.c:2:0: /usr/include/pthread.h:233:12: note: expected 'void * (*)(void *)' but argument is of type 'void (*)(int)' extern int pthread_create (pthread_t *__restrict __newthread, ^ /tmp/ccwnzH68.o: In function `main': 53b5b6a8283c35b36cc9c164e53d178e.c:(.text+0x23d): undefined reference to `pthread_create' 53b5b6a8283c35b36cc9c164e53d178e.c:(.text+0x26a): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status

----------- code below---------

#include #include #include #include

#define NO_OF_PHILOSOPHERS 5

pthread_t philosophers[NO_OF_PHILOSOPHERS]; pthread_mutex_t mutex_forks = PTHREAD_MUTEX_INITIALIZER;; int forks[NO_OF_PHILOSOPHERS];

void init() { int i; for(i=0; i

void philosopher(int i) { int right = i; int left = (i - 1 == -1) ? NO_OF_PHILOSOPHERS - 1 : (i - 1); int locked; while(1) { locked = 0; while(!locked) { pthread_mutex_lock(&mutex_forks); if(forks[right] || forks[left]) { pthread_mutex_unlock(&mutex_forks); // give up the forks unless you can take both at once. printf("Philosopher %d cannot take forks. Giving up and thinking. ",i); usleep(random() % 1000); // think. continue; } forks[right] = 1; // take forks. forks[left] = 1;

pthread_mutex_unlock(&mutex_forks); locked = 1; }

printf("Philosopher %d took both forks. Now eating :) ",i); usleep(random() % 500); printf("Philosopher %d done with eating. Giving up forks. ",i); pthread_mutex_lock(&mutex_forks); // give up forks. forks[right] = 0; forks[left] = 0; pthread_mutex_unlock(&mutex_forks); usleep(random() % 1000); }

}

int main() { init(); int i; for(i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions