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
Amass of m 1.5 kg of steam is contained in a closed rigid container. initially the pressure and temperature of the steam are: p 1.5 mpa and t 240°c (superheated state), respectively. then the temperature drops to t2= 100°c as the result of heat transfer to the surroundings. determine: a) quality of the steam at the end of the process, b) heat transfer with the surroundings. for: p1.5 mpa and t 240°c: enthalpy of superheated vapour is 2900 kj/kg, specific volume of superheated vapour is 0. 1483 m/kg, while for t 100°c: enthalpy of saturated liquid water is 419kj/kg, specific volume of saturated liquid water is 0.001043m/kg, enthalpy of saturated vapour is 2676 kj/kg, specific volume of saturated vapour is 1.672 m/kg and pressure is 0.1 mpa.
Answers: 3
question
Engineering, 03.07.2019 14:10
The y form of iron is known as: a) ferrite b) cementite c) perlite d) austenite
Answers: 3
question
Engineering, 04.07.2019 18:20
Find the kinematic pressure of 160kpa. for air, r-287 j/ kg k. and hair al viscosity of air at a temperature of 50°c and an absolute (10 points) (b) find the dynamic viscosity of air at 110 °c. sutherland constant for air is 111k
Answers: 3
question
Engineering, 04.07.2019 19:20
Afan that can provide air speeds up to 60 m/s is to be used in a low-speed wind tunnel with atmospheric air at 35 c. if one wishes to use the wind tunnel to study flat-plate boundary layer behavior up to reynolds numbes of re 10, what is the minimum plate length that should be used? at what distance from the leading edge would transition occur if the critical reynolds nurnber were rer,e = 5 × 105?
Answers: 2
You know the right answer?
Dining Philosopher Problem (Please use skeleton code)

Task 1 Eliminate the deadlock by a...
Questions
Questions on the website: 13722363