Implement a program on gator linux system


Implement a program on Gator Linux system for the dining philosophers' problem as follows. The program should use five Pthreads to simulate philosophers and declare an array of five semaphores to represent five forks.
1. Write the program without considering deadlock. Your program should create five Pthreads that run the same procedure as follows (Note: This is pseudo-code).
Make each philosopher print a message at every event (viz., request, allocation, release). The message should show the philosopher number and the relevant fork number, as shown in the above procedure.
Run the program and see whether it will run into deadlock.
2. Write another program that makes Philosopher 4 request the forks in the other order (the algorithm below). Observe that the deadlock will never occur.
philosopher(int i){
while (TRUE) {
// Think
// Eat
printf("Philosopher %d requests fork %dn", i, i);
P(fork[i]); /* Pick up left fork */
printf("Philosopher %D acquired fork %dn", i, i);
printf("Philosopher %d requests fork %dn", i, (i+1) mod 5);
P(fork[(i+1) mod 5]); /* Pick up right fork */
printf("Philosopher %d acquired fork %dn", i, (i+1) mod 5);
eat();
printf("Philosopher %d releases fork %dn", i, (i+1) mod 5);
V(fork[(i+1) mod 5]);
printf("Philosopher %d releases fork %dn", i, i);
V(fork[i]);
}
}
semaphore fork[5];
fork[0] = fork[1] = fork[2] = fork[3] = fork[4] = 1;
fork(philosopher, 1, 0);
fork(philosopher, 1, 1);
fork(philosopher, 1, 2);
fork(philosopher, 1, 3);
fork(philosopher, 1, 4);
Turn in the program for each version in Blackboard Vista by the due date (see the course
schedule).
philosopher(int i){
Same as above
}
philosopher4(){
while (TRUE) {
// Think
// Eat
printf("Philosopher 4 requests fork 0n");
P(fork[0]); /* Pick up right fork */
printf("Philosopher 4 acquired fork 0n");
printf("Philosopher 4 requests fork 4n");
P(fork[4]); /* Pick up left fork */
printf("Philosopher 4 acquired fork 4n");
eat();
printf("Philosopher 4 releases fork 4n");
V(fork[4]);
printf("Philosopher 4 releases fork 0n");
V(fork[0]);
}
}
semaphore fork[5];
fork[0] = fork[1] = fork[2] = fork[3] = fork[4] = 1;
fork(philosopher, 1, 0);
fork(philosopher, 1, 1);
fork(philosopher, 1, 2);
fork(philosopher, 1, 3);
fork(philosopher4, 0);  

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Implement a program on gator linux system
Reference No:- TGS081262

Expected delivery within 24 Hours