Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objnetives Write a program to simulate the Dinning Philosophers Problem with ran

ID: 3909924 • Letter: O

Question

Objnetives Write a program to simulate the Dinning Philosophers Problem with random waiting time per philosopher. modify the problem by making the philosopher wait for a random amount of time instead of same time after failing to acquire right hand fork THEORY Five philosophers are seated around a circular table. Each philosopher has a place of spaghetti and he need two forks to eat. Between each plate there is a fork. The life of a philosopher consists of alternate period of eating& thinking. When a philosopher gets hungry he tries to acquire his left fork, if he gets it, it tries to acquire right fork In this solution, we check after picking the left fork whether the right fork is available or not If not, then philosopher puts down the left fork & continues to think. Even this can fail if all the philosophers pick the left fork simultaneously & no right forks available &putting; the lef fork down again. This repeats& leads to starvation. Now, we can modify the problem by making the philosopher wait for a random amount of time instead of same time after failing to acquire right hand fork. This will reduce the problem of starvatkn VAR me: semaphore, initially 1: s[5]: semaphore s|5]. initially 0 pflagl5]: (THINK, HUNGRY, EAT, initially THINK: philosopher flag * for mutual exclusion As before, each philosopher is an endless cycle of thinking and eating procedure philosopheri) while TRUE do THINKING take chopsticksi) EATING drop chopsticks()

Explanation / Answer

Hello Sir/Madam

Dinning philospher problem is the classic solution for the process synchronization in the operating system.There are 5 philosphers in a rooom whose work is to always eating and thinking...For eating the philosphers have 1 chopsticks each and they want two chopsticks to eat ..The Philosphers first acquire the left chopstick first and then right chopstick if both the chopsticks are then only teh philospher will eat otherwise the philosphers keeps on waiting for the acquistion of both the chopsticks ...Here is the code the Dinning philospher problem implemented using the random wait time instead of constant wait time

#define N 6
#define LEFT (i-1)
#define RIGHT (i+1)
#define THINKING 0
#define HUNGRY 1
#define EATING 2
typedef int semaphore;
int state[N];
semaphore mutex = 1;
semaphore s[N] ;
void philosopher(int i)
{
while (TRUE)
{
think();
take_sticks(i);
eat();
put_sticks(i);
}
}
void take_sticks(int i)
{
down(mutex);
state[i] = HUNGRY;
test(i);
up(mutex);
down(s[i]) ;
}
void put_sticks(int i)
{
down(mutex);
state[i] = THINKING;
test(LEFT);
test(RIGHT);
up(mutex);
}
void test(int i)
{
if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
{
state[i] = EATING;
up(s[i]);
}
}

Here we have a array to monitor the state of the philospher
Hope it help and looking forward to help if u ahve any doubts...
Thank u...