Computers and Technology, 15.07.2020 01:01 leleee10
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);
}
Answers: 2
Computers and Technology, 22.06.2019 20:00
What statement best describes operating systems? it’s possible for modern computers to function without operating systems. most operating systems are free or very inexpensive. operating systems are managed by the computer’s microprocessor (cpu). operating systems manage the computer’s random access memory (ram).
Answers: 1
Computers and Technology, 23.06.2019 06:00
Which statement is true of web-based social media? a.they allow consumers to interact with and update content. b.they cannot be updated easily, as compared to print media. c.they are expensive to produce and maintain, as compared to print and television. d.they can exist independent of the internet.
Answers: 1
Computers and Technology, 23.06.2019 06:30
Martha is designing a single-player game. her manager suggests that she plan the design to incorporate future modifications. which principle of game design relates to planning for future modifications?
Answers: 1
Computers and Technology, 23.06.2019 08:00
Michael has written an e-mail to his employees that describes a new product special that will be introduced to the customers next week. by taking time to make sure the e-mail is well written, logical, and organized, michael has made sure his message has the characteristics of a) effective communicationb) ineffective communicationc) barriers to communicationd) workplace communication
Answers: 2
Dining Philosopher’s problem is a famous problem in OS. A deadlock may happen when all philosopher...
English, 28.04.2021 23:30
Mathematics, 28.04.2021 23:30
Mathematics, 28.04.2021 23:30
Mathematics, 28.04.2021 23:30
Mathematics, 28.04.2021 23:30
Computers and Technology, 28.04.2021 23:30
History, 28.04.2021 23:30
History, 28.04.2021 23:30
Mathematics, 28.04.2021 23:30
Chemistry, 28.04.2021 23:30