subject

Modify the following code to do the following: - Task 1 (50 pts). Eliminate the deadlock by avoiding circular waiting. (Hint: change the order to access chopsticks for some philosophers.) - Task 2 (50 pts). Eliminate the deadlock by avoiding hold and wait. In other words, if a philosopher is not able to obtain the right chopstick, he/she will release the left chopstick immediately. (Hint: Use Pthread Mutex to solve the problem.) -Task 3 (50 pts extra credit). Eliminate the deadlock by limiting that at most 3 philosophers are able to compete for the chopsticks at the same time. (Hint: Use Pthread Conditional Variable to solve the problem.)

#include
#include
#include
#include
#include

#define NUMP 5

pthread_mutex_t fork_mutex[NUMP];

int main(int argc, char* argv[])
{
int i;
pthread_t diner_thread[NUMP];
int dn[NUMP];
void *diner();
for (i=0;i pthread_mutex_init(&fork_mutex[ i], NULL);

for (i=0;i dn[i] = i;
pthread_create(&diner_thread[i] ,NULL, diner,&dn[i]);
}
for (i=0;i pthread_join(diner_thread[i],NULL);
pthread_exit(0);
}

void *diner(int *i)
{
int v;
int eating = 0;
printf("I'm diner %d\n",*i);
v = *i;
while (eating < 5) {
printf("%d is thinking\n", v);
sleep( v/2);
printf("%d is hungry\n", v);
pthread_mutex_lock(&fork_mutex[ v]);
pthread_mutex_lock(&fork_mutex[ (v+1)%NUMP]);
printf("%d is eating\n", v);
eating++;
sleep(1);
printf("%d is done eating\n", v);
pthread_mutex_unlock(&fork_mute x[(v+1)%NUMP]);
pthread_mutex_unlock(&fork_mute x[v]);
}
pthread_exit(NULL);
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 17:00
Which of the following is not contained on the slide show toolbar? a. next button b. slide button c. close button d. pen tool
Answers: 1
question
Computers and Technology, 23.06.2019 08:30
Helen's credit card has an apr of 15.32% and a grace period of 17 days and helen pays her balance in the full every month. if her last billing cycle ended on september 26, 2009, and she made her payment on october 11, 2009, did she owe any interest on her last statement's balance?
Answers: 3
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
question
Computers and Technology, 23.06.2019 14:00
What is html ? give a small description about html
Answers: 2
You know the right answer?
Modify the following code to do the following: - Task 1 (50 pts). Eliminate the deadlock by avoiding...
Questions
question
Mathematics, 23.03.2020 04:25
question
Mathematics, 23.03.2020 04:26
question
Mathematics, 23.03.2020 04:26
question
English, 23.03.2020 04:26
question
Mathematics, 23.03.2020 04:27
question
Physics, 23.03.2020 04:27