In the dining philosophers problem Implementing this c code, how would it be alt
ID: 3836394 • Letter: I
Question
In the dining philosophers problem Implementing this c code, how would it be altered to include user input for number of philosophers?
#define N #define LEFT number of philosophers (i+N-1)%N fx number of i's left neighbor*/ #define RIGHT (i+1)% #define THINKING 0 /A number of i's right neighbor #define HUNGRY philosopher is thinking t/ philosopher is trying to get forks philosopher is eating EATING typedef int semaphore: nt statelN); semaphores are a special kind of int array to keep track of everyone's state mutual exclusion for regions Semaphore mutex 1; Semaphore s A one semaphore per philosopher void philosopher(int i) i: philosopher number, from 0 to N-1 while (TRUE) repeat forever *f think() philosopher is thinking take forks two forks or block eat() yum-yum, spaghetti put forks(i) put both forks back on table void take-forks(int i) i: philosopher number, from 0 to N-1 down(&mutex;); enter critical region staten HUNGRY; i record fact that philosopher i is hungry try to acquire 2 forks test up(&mutex;); exit critical region down (&s;[]) block if forks were not acquired void put forks(i) i: philosopher number, from o to N-1 down(&mutex;); enter critical region statelij THINKING: /A philosopher has finished eating if left neighbor can now eat test (LEFT) test (RIGHT see if right neighbor can now eat up(&mutex;); fx exit critical region void test(i) I i: philosopher number, from 0 to N-1 w/ if (statelin HUNGRY && statefLEFTI la EATING && state(RIGHT) EATING) Stateli EATING Figure 2-46. A solution to the dining philosophers problem.Explanation / Answer
For the required functionality, first you have to remove symbolic constant "#define N 5" at the top of the program and other symbolic constants which includes N i.e., LEFT, RIGHT.
Now, in main function, you need to implement these lines:
int n;
printf("Enter the number of philosphers: ";
scanf("%d",&n);
Hope it helps, Feel free to comment in case of any query.