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, 21.06.2019 16:00
Choice of type is influenced primarily by these two factors?
Answers: 3
question
Computers and Technology, 22.06.2019 10:10
3. bob is arguing that if you use output feedback (ofb) mode twice in a row to encrypt a long message, m, using the same key each time, it will be more secure. explain why bob is wrong, no matter what encryption algorithm he is using for block encryption (15 points).
Answers: 3
question
Computers and Technology, 22.06.2019 21:00
Kirk found a local community college with a two-year program and he is comparing the cost with that of an out-of-state two-year school. what is the expected total cost for one year at the local community college if kirk lives at home? what is the expected total cost for one year at the out-of-state school if kirk lives on campus?
Answers: 2
question
Computers and Technology, 23.06.2019 11:20
Http is the protocol that governs communications between web servers and web clients (i.e. browsers). part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. some of the codes and their meanings are listed below: 200, ok (fulfilled)403, forbidden404, not found500, server errorgiven an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.
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, 07.04.2021 20:20
question
Mathematics, 07.04.2021 20:20
question
Mathematics, 07.04.2021 20:20
question
Mathematics, 07.04.2021 20:20
Questions on the website: 13722367