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

I am trying to Implement an input/output version of the Dining Philosophers prob

ID: 3914625 • Letter: I

Question

I am trying to Implement an input/output version of the Dining Philosophers problem. To solve the problem, I am to use the Resource hierachy solution meaning, I have to sign a partial order to the resources ( forks).

Since there are 5 philosophers, (0 to 4) AND because it has to be interactive, I believe I need total of 6 threads.

I need help setting up the question since I am provided with 0 codes. Any help would be greatly appreciated.

Resource hierarchy solution This solution to the problem is the one originally proposed by Dijkstra. It assigns a partial order to the resources (the forks, in this case), and establishes the convention that all resources will be requested in order, and that no two resources unrelated by order will ever be used by a single unit of work at the same time Here, the resources (forks) will be numbered 0 through 4 and each unit of work (philosopher) will always pick up the lower-numbered fork first, and then the higher-numbered fork, from among the two forks they plan to use. The order in which each philosopher puts down the forks does not matter. In this case, if four of the five philosophers simultaneously pick up their lower-numbered fork, only the highest-numbered fork will remain on the table, so the fifth philosopher will not be able to pick up any fork. Moreover, only one philosopher will have access to that highest-numbered fork, so they will be able to eat using two forks Project Guidance and Requirements: By being interactive, vour program supports user inputs, which will determine which philosopher wants to eat or think

Explanation / Answer

Run this code, your program will run perfectly. try running this code on any c compiler.

#include<stdio.h>
#define n 4 // define n according to the number of philosophers you want to solve this problem.

int compltedPhilo = 0,i;

struct fork{
int taken;
}ForkAvil[n];

struct philosp{
int left;
int right;
}Philostatus[n];

void goForDinner(int philID){ //threads concept implemented here.
if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
printf("Philosopher %d completed his dinner ",philID+1);
//if already completed dinner
else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
//if just taken two forks
printf("Philosopher %d completed his dinner ",philID+1);

Philostatus[philID].left = Philostatus[philID].right = 10; //Let he completed dinner by assigning value 10.
int otherFork = philID-1;

if(otherFork== -1)
otherFork=(n-1);

ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks with help of this statement.
printf("Philosopher %d released fork %d and fork %d ",philID+1,philID+1,otherFork+1);
compltedPhilo++;
}
else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ // if left already taken, try for right fork
if(philID==(n-1)){
if(ForkAvil[philID].taken==0){ // Last philosopher trying in the reverse direction.
ForkAvil[philID].taken = Philostatus[philID].right = 1;
printf("Fork %d taken by philosopher %d ",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d ",philID+1,philID+1);
}
}else{ // exception if only one philosopher left.
int dupphilID = philID;
philID-=1;

if(philID== -1)
philID=(n-1);

if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
printf("Fork %d taken by Philosopher %d ",philID+1,dupphilID+1);
}else{
printf("Philosopher %d is waiting for Fork %d ",dupphilID+1,philID+1);
}
}
}
else if(Philostatus[philID].left==0){
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by philosopher %d ",philID,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d ",philID+1,philID);
}
}else{ // exception taken for last philosopher case.
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by Philosopher %d ",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for Fork %d ",philID+1,philID+1);
}
}
}else{}
}

int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;

while(compltedPhilo<n){ // loop will go on until all the philosopher completes thei dinner.

for(i=0;i<n;i++)
goForDinner(i);
printf(" Till now num of philosophers completed dinner are %d ",compltedPhilo);
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote