subject
Engineering, 15.04.2020 20:49 christinavelez26

Dining Philosopher Problem (Please use skeleton code)

Task 1 Eliminate the deadlock by avoiding circular waiting. (Hint: change the order to access chopsticks for some philosophers.)

Task 2 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.)

Please provide a source program for each task, like task.1 & task.2

skeleton code ( In C):

#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);

Expert

ansver
Answers: 1

Another question on Engineering

question
Engineering, 03.07.2019 14:10
When at a point two solid phase changes to one solid phase on cooling then it is known as a) eutectoid point b) eutectic point c) peritectic point d) peritectoid point
Answers: 3
question
Engineering, 04.07.2019 18:10
The mass flow rate of the fluid remains constant in all steady flow process. a)- true b)- false
Answers: 1
question
Engineering, 04.07.2019 18:10
Water at 70°f and streams enter the mixing chamber at the same mass flow rate, determine the temperature and the quality of the exiting stream. 0 psia is heated in a chamber by mixing it with saturated water vapor at 20 psia. if both streams enters the mixing chamber at the same mass flow rate, determine the temperature and the quality of the existing system.
Answers: 2
question
Engineering, 04.07.2019 18:10
For the closed feedwater heater below, feedwater enters state 3 at a pressure of 2000 psia and temperature of 420 °f at a rate of ix10 ibhr. the feedwat extracted steam enters state 1 at a pressure of 1000 psia and enthalpy of 1500 btu/lbm. the extracted er leaves at an enthalpy of 528.7 btu/lbm steam leaves as a saturated liquid. (16) a) determine the mass flow rate of the extraction steam used to heat the feedwater (10) b) determine the terminal temperature difference of the closed feedwater heater
Answers: 3
You know the right answer?
Dining Philosopher Problem (Please use skeleton code)

Task 1 Eliminate the deadlock by a...
Questions
question
Geography, 28.03.2021 02:40
question
Mathematics, 28.03.2021 02:40
question
Mathematics, 28.03.2021 02:40
question
English, 28.03.2021 02:40
Questions on the website: 13722359