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, 21.06.2019 19:10
Select the correct answer. emma, the quality control manager in a software development company, asks the testing team to check whether the user interface is friendly. to which type of testing is emma referring? a. usability testing b. data comparison c. validation testing d. stress testing e. destruction testing
Answers: 1
question
Computers and Technology, 21.06.2019 22:00
Match the steps of the process to julia's analysis. 1. analyze choices. current costs for making phone calls to foreign countries averages between five and ten cents a minute. 2. determine the goals. julia needs to reduce the cost of telecommunications without reducing her employees' ability to do their jobs. 3. gather data. the corporate computer network will be able to handle the increased traffic that will occur as a result of voip. 4. evaluate the decision. julia will have her it department set up voip in the smallest domestic office. 5. make the decision. julia will have employees document the benefits and problems that result from using the new technology.
Answers: 1
question
Computers and Technology, 22.06.2019 19:20
Terri needs to insert a cover page into her document. where should she go to access the commands to do so? o insert tab, objects group o insert tab, illustrations group o insert tab, pages group o insert tab, media group submit
Answers: 1
question
Computers and Technology, 23.06.2019 09:30
Light travels at a speed of 186,000 miles a second. the distance light travels in a year is 5,865,690,000,000 miles/year 5,865,695,000,000 miles/year 58,656,950,000,000 miles/year 6,789,000,0000 miles/year
Answers: 1
You know the right answer?
Dining Philosopher’s problem is a famous problem in OS. A deadlock may happen when all philosopher...
Questions
question
Mathematics, 06.11.2020 04:10
question
Social Studies, 06.11.2020 04:10