Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a Cygwin or Unix console program that implements a solution to the Dining Philosophers problem discussed in section 2.5.1 of the textbook. Use Threads
Create a Cygwin or Unix console program that implements a solution to the Dining Philosophers problem discussed in section 2.5.1 of the textbook. Use Threads and Semaphores in the pthreads library to implement the solution in Figure 2-46 (repeated below).
Create five philosophers by creating threads using the philosopher() function as the entry point (you need to add a main function that creates the threads).
Use printf() statements to show what is happening to each philosopher (when thinking and eating).
/* Dining Philosophers */ /* Code Reference: "Modern Operating Systems, Tanenbaum */ /* Required includes go here */ #define N 5 /* number of philosophers */ #define LEFT (i+N-1)%N /* number of i's left neighbor */ #define RIGHT (i+1)%N /* number of i's right neighbor */ #define THINKING 0 /* philosopher is thinking */ #define HUNGRY 1 /* philosopher is trying to get forks */ #define EATING 2 /* philosopher is eating */ typedef int semaphore; /* NOTE: need to use real semaphores */ int state[N]; /* array to keep track of everyone's state */ semaphore mutex = 1; /* mutual exclusion for critical regions */ semaphore s[N]; /* one semaphore per philosopher */ void philosopher(int i) /* i: philosopher number, from 0 to N-1 */ { while (TRUE) { /* repeat forever */ think(); /* philosopher is thinking */ take_forks(i); /* acquire two forks or block */ eat(); /* philosopher is eating */ put_forks(i); /* put both forks back on the table */ } /* end while loop */ } /* end void philosopher() */ void take_forks(int i) /* i: philosopher number, from 0 to N-1 */ { down(&mutex); /* enter critical region */ state[i]=HUNGRY; /* record fact that philosopher is hungry */ test(i); /* try to acquire 2 forks */ up(&mutex); /* exit critical region */ down(&s[i]); /* block if forks not acquired */ } /* end void take_forks() */ void put_forks(int i) /* i: philosopher number, from 0 to N-1 */ { down(&mutex); /* enter critical region */ state[i]=THINKING; /* philosopher has finished eating */ test(LEFT); /* see if left neighbor can eat now */ test(RIGHT); /* see if right neighbor can eat now */ up(&mutex); /* exit critical region */ } void test(int i) /* i: philosopher number, from 0 to N-1 */ { if (state[i]==HUNGRY && state[LEFT] !=EATING && state[RIGHT] != EATING) { state[i] = EATING; up(&s[i]); } } /* Add main function here that creates semaphores and philosopher threads */ int main() { pthread_t thd; /* You could have an array of these */ int index; /* I wonder what you can use this for? */ /* Create your semaphores here */ sem_init(&mutex, 0, 1); /* still need the S[] semaphore array initialized */ /* Create N Philosopher threads here */ pthread_create(&thd, NULL, philosopher, (void*)index); /* this is just one */ /* Insert code here for the main thread to wait for the philosopher threads */ pthread_join(thd, NULL); /* this is just for one */ /* Destroy your semaphores here */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started