subject

Using this Skeleton Code, what is the correct code to run the dining philosophers program?

#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: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 01:40
Kali, a python programmer, is using the turtle module to write the word “hello.” which code should she use to indicate the location to begin writing the word? a # pick up the turtle and move it to its starting location. b penup(-100, 200) goto() pendown() c penup() goto(-100, 200) pendown() d # pick up the turtle and move it to (-100, 200)
Answers: 2
question
Computers and Technology, 23.06.2019 08:30
Based on your knowledge of a good network, describe what you think is a perfect network would be. what kind of information and resources could users share on this network. what would the network administrator do? what kind of communication would be used?
Answers: 1
question
Computers and Technology, 23.06.2019 15:00
Based on the current economic situation do you expect the employment demand for graduating engineers to increase or decrease? explain the basis for your answer. with a significant economic recovery, what do you think will happen to future enrollments in graduating engineering programs?
Answers: 1
question
Computers and Technology, 23.06.2019 17:00
In which of the following ways can using test-taking tips you? a. you can focus on the information that you need to study. b. you will see the answers to the test. c. you will study more. d. you will be less organized.
Answers: 1
You know the right answer?
Using this Skeleton Code, what is the correct code to run the dining philosophers program?
Questions
question
Mathematics, 19.06.2020 22:57
question
Mathematics, 19.06.2020 22:57
question
Mathematics, 19.06.2020 22:57
Questions on the website: 13722360