subject

Dining Philosopher’s problem is a famous problem in OS. A deadlock may happen when all philosophers want to start eating at the same time and pick up one chopstick and wait for the other chopstick. We can
use semaphores to simulate the availability of chopsticks.

To prevent the deadlock, in class we have discussed three solutions:
1. Allow at most 4 philosophers to be sitting simultaneously at the table.
2. Allow a philosopher to pick up her chopsticks only if both are available
3. Use an asymmetric solution: an odd-numbered philosopher picks up first the left chopstick and
then the right chopstick. Even-numbered philosopher picks up first the right chopstick and then
the left chopstick.

The following program can lead to a deadlock.
? Run the program and observe the deadlock condition.
? Based on the program, please implement the above three solutions to prevent the deadlock. Each
solution should be saved as an individual C program
(e. g. solution1.c, solution2.c, and solution3.c).

Include necessary comments in your programs.
Submission: solution2.c and solution3.c on Blackboard

#include
#include
#include

#define N 5 //the number of philosophers

sem_t S[N]; //semaphores for chopsticks

void * philospher(void *num);
void take_chopsticks(int);
void put_chopsticks(int);

int phil_num[N]={0,1,2,3,4}; //philosopher ID

int main()
{
int i;
pthread_t thread_id[N];
for(i=0;i sem_init(&S[i],0,1);
for(i=0;i pthread_create(&thread_id[i],NU LL, philospher,&phil_num[i]);

for(i=0;i pthread_join(thread_id[i],NULL);
}
void *philospher(void *num)
{
while(1)
{
int *i = num;
take_chopsticks(*i);
put_chopsticks(*i);
}
}
void take_chopsticks(int ph_num)
{

printf("Philosopher %d is Hungry\n",ph_num);
sem_wait(&S[ph_num]); //take the left chopstick
printf("Philosopher %d takes chopstick %d \n",ph_num, ph_num);

sleep(1);

sem_wait (&S[(ph_num+1)%N]); //take the right chopstick
printf("Philosopher %d takes chopstick %d \n",ph_num,(ph_num+1)%N);

printf("Philosopher %d is eating\n",ph_num);
sleep(1);
}

void put_chopsticks(int ph_num)
{
sem_post (&S[ph_num]); //put the left chopstick
printf("Philosopher %d putting chopstick %d \n",ph_num, ph_num);
sleep(1);
sem_post (&S[(ph_num+1)%N]); //put the right chopstick
printf("Philosopher %d putting chopstick %d \n",ph_num,(ph_num+1)%N);
printf("Philosopher %d is thinking\n",ph_num);
sleep(1);

}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 03:00
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". any value that is the same as the immediately preceding value is considered a consecutive duplicate. in this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. note that the last 3 is not a consecutive duplicate because it was preceded by a 7. write some code that uses a loop to read such a sequence of non-negative integers , terminated by a negative number. when the code finishes executing, the number of consecutive duplicates encountered is printed. in this case, 3 would be printed. assume the availability of a variable, stdin, that references a scanner object associated with standard input. that is, stdin = new scanner(system.in); is given.
Answers: 1
question
Computers and Technology, 23.06.2019 04:00
Laire writes a letter to her grandmother, in which she describes an amusement park she visited last week. she adds pictures of that place in her letter. which feature of a word processing program will claire to remove unwanted parts of the pictures?
Answers: 3
question
Computers and Technology, 23.06.2019 18:00
Apunishment or the threat of punishment used to enforce conformity. select the best answer from the choices provided t f
Answers: 1
question
Computers and Technology, 23.06.2019 19:30
Of the following pieces of information in a document, for which would you most likely insert a mail merge field?
Answers: 3
You know the right answer?
Dining Philosopher’s problem is a famous problem in OS. A deadlock may happen when all philosopher...
Questions
question
Chemistry, 12.06.2020 04:57
question
Mathematics, 12.06.2020 04:57
question
Mathematics, 12.06.2020 04:57
question
English, 12.06.2020 04:57
Questions on the website: 13722363