I understand that this is famous question and I understand the logic. But I am p
ID: 3913221 • Letter: I
Question
I understand that this is famous question and I understand the logic. But I am provided with 0 codes and have never coded using threads. Any help would be greatly appreciated.
Thank you
required implement interactive of Dining Philosophers You are to an version the problem using the Resource Hierarchy solution to avoid deadlock. The Resource Hierarchy solution will be presented shortly. Five philosophers are numbered from 0 through 4. Each philosopher is at the state of thinking at the beginningExplanation / Answer
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include<time.h>
// Defines constants
#define PHILOSOPHER 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFTSIDE (currentPhilosopher + 4) % PHILOSOPHER
#define RIGHTSIDE (currentPhilosopher + 1) % PHILOSOPHER
// Array to store philosopher status
int philosopherState[PHILOSOPHER];
// Array to store philosopher position
int philosopherPos[PHILOSOPHER] = { 0, 1, 2, 3, 4 };
// Declares a mutex
sem_t mutex;
// Declares signal
sem_t S[PHILOSOPHER];
// Function for delay the display
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}// End of function
// Function to apply philosopher algorithm
void apply(int currentPhilosopher)
{
// Checks the philosopher status as hungry or left or right philosopher not eating status
if (philosopherState[currentPhilosopher] == HUNGRY && philosopherState[LEFTSIDE] != EATING && philosopherState[RIGHTSIDE] != EATING)
{
// State the current philosopher status as eating
philosopherState[currentPhilosopher] = EATING;
// Calls the function to delay the display
delay(1000);
// Displays the philosopher number takes two fork numbers
printf("Philosopher %d takes fork %d and %d ", currentPhilosopher + 1, LEFTSIDE + 1, currentPhilosopher + 1);
// Displays the philosopher number who is eating
printf("Philosopher %d is Eating ", currentPhilosopher + 1);
// sem_post(&S[currentPhilosopher]) has no effect during takeFork()
// used to wake up hungry philosophers during putFork()
sem_post(&S[currentPhilosopher]);
}// End of if condition
}// End of function
// Function to take a chopsticks
void takeFork(int currentPhilosopher)
{
// Calls the method to wait mutex
sem_wait(&mutex);
// Sets the philosopher state as hungry
philosopherState[currentPhilosopher] = HUNGRY;
// Displays the philosopher number who is hungry
printf("Philosopher %d is Hungry ", currentPhilosopher + 1);
// Call the function to eat if neighbours are not eating
apply(currentPhilosopher);
// Calls the method to signal mutex
sem_post(&mutex);
// If unable to eat wait to be signal
sem_wait(&S[currentPhilosopher]);
// Calls the function to delay the display
delay(1000);
}// End of function
// Function to put down chopsticks
void putFork(int currentPhilosopher)
{
// Calls the method to wait mutex
sem_wait(&mutex);
// Sets the philosopher state to thinking
philosopherState[currentPhilosopher] = THINKING;
// Displays the philosopher number and two fork number put down
printf("Philosopher %d putting fork %d and %d down ", currentPhilosopher + 1, LEFTSIDE + 1, currentPhilosopher + 1);
// Displays the philosopher number who is thinking
printf("Philosopher %d is thinking ", currentPhilosopher + 1);
// Call the function to eat if left side are not eating
apply(LEFTSIDE);
// Call the function to eat if right side are not eating
apply(RIGHTSIDE);
// Calls the method to signal mutex
sem_post(&mutex);
}// End of function
// Function for philosopher action
void *philospherAction(void* num)
{
// Sentinel loop
while (1)
{
int* counter = num;
// Calls the function to delay the display
delay(100);
// Calls the function to take fork for a philosopher number
takeFork(*counter);
// Calls the function to delay the display
delay(0);
// Calls the function to put fork for a philosopher number
putFork(*counter);
}// End of while loop
}// End of function
// main function definition
int main()
{
// Loops counter
int counter;
// Creates thread id array for number of philosophers
pthread_t threadId[PHILOSOPHER];
// Initialize the semaphores
sem_init(&mutex, 0, 1);
// Loops till number of philosophers
for (counter = 0; counter < PHILOSOPHER; counter++)
sem_init(&S[counter], 0, 0);
// Loops till number of philosophers
for (counter = 0; counter < PHILOSOPHER; counter++)
{
// Create philosopher processes
pthread_create(&threadId[counter], NULL, philospherAction, &philosopherPos[counter]);
printf("Philosopher %d is thinking ", counter + 1);
}// End of for loop
// Loops till number of philosophers
for (counter = 0; counter < PHILOSOPHER; counter++)
// To join the thread
pthread_join(threadId[counter], NULL);
}// End of main function
Sample Output:
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 2 is Hungry
Philosopher 3 is Hungry
Philosopher 4 is Hungry
Philosopher 1 is Hungry
Philosopher 5 is Hungry
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 5 putting fork 4 and 5 down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 1 takes fork 5 and 1
Philosopher 1 is Eating
Philosopher 4 putting fork 3 and 4 down
Philosopher 4 is thinking
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 5 is Hungry
Philosopher 1 putting fork 5 and 1 down
Philosopher 1 is thinking
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 4 is Hungry
Philosopher 3 putting fork 2 and 3 down
Philosopher 3 is thinking
Philosopher 2 takes fork 1 and 2
Philosopher 2 is Eating
Philosopher 1 is Hungry
Philosopher 5 putting fork 4 and 5 down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 3 is Hungry
Philosopher 2 putting fork 1 and 2 down
Philosopher 2 is thinking
Philosopher 1 takes fork 5 and 1
Philosopher 1 is Eating
Philosopher 5 is Hungry
Philosopher 4 putting fork 3 and 4 down
Philosopher 4 is thinking
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 2 is Hungry
Philosopher 1 putting fork 5 and 1 down
Philosopher 1 is thinking
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 4 is Hungry
Philosopher 3 putting fork 2 and 3 down
Philosopher 3 is thinking
Philosopher 2 takes fork 1 and 2
Philosopher 2 is Eating
Philosopher 1 is Hungry
Philosopher 5 putting fork 4 and 5 down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 3 is Hungry
Philosopher 2 putting fork 1 and 2 down
Philosopher 2 is thinking
Philosopher 1 takes fork 5 and 1
Philosopher 1 is Eating
Philosopher 5 is Hungry
Philosopher 4 putting fork 3 and 4 down
Philosopher 4 is thinking
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 2 is Hungry
Philosopher 1 putting fork 5 and 1 down
Philosopher 1 is thinking
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 4 is Hungry
Philosopher 3 putting fork 2 and 3 down
Philosopher 3 is thinking
Philosopher 2 takes fork 1 and 2
Philosopher 2 is Eating
Philosopher 1 is Hungry
Philosopher 5 putting fork 4 and 5 down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 3 is Hungry
Philosopher 2 putting fork 1 and 2 down
Philosopher 2 is thinking
Philosopher 1 takes fork 5 and 1
Philosopher 1 is Eating
Philosopher 5 is Hungry
Philosopher 4 putting fork 3 and 4 down
Philosopher 4 is thinking
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 2 is Hungry
Philosopher 1 putting fork 5 and 1 down
Philosopher 1 is thinking
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 4 is Hungry
Philosopher 3 putting fork 2 and 3 down
Philosopher 3 is thinking