Engineering
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

answer
Answers: 1

Other questions on the subject: Engineering

image
Engineering, 04.07.2019 18:10, abdirahmansoloman
Air is to be cooled in the evaporator section of a refrigerator by passing it over a bank of 0.8-cm-outer-diameter and 0.4-m-long tubes inside which the refrigerant is evaporating at -20°c. air approaches the tube bank in the normal direction at 0°c and 1 atm with a mean velocity of 4 m/s. the tubes are arranged in-line with longitudinal and transverse pitches of sl- st 1.5 cm. there are 30 rows in the flow direction with 15 tubes in each row. determine (a) the refrigeration capacity of this system and (b) pressure drop across the tube bank. evaluate the air properties at an assumed mean temperature of -5°c and 1 atm. is this a good assumption?
Answers: 1
image
Engineering, 04.07.2019 18:10, 19deleonl
Coiled springs ought to be very strong and stiff. si3n4 is a strong, stiff material. would you select this material for a spring? explain.
Answers: 2
image
Engineering, 04.07.2019 18:10, skpdancer1605
Ariver flows from north to south at 8 km/h. a boat is to cross this river from west to east at a speed of 20 km/h (speed of the boat with respect to the earth/ground). at what angle (in degrees) must the boat be pointed upstream such that it will proceed directly across the river (hint: find the speed of the boat with respect to water/river)? a 288 b. 21.8 c. 326 d. 30.2
Answers: 3
image
Engineering, 04.07.2019 18:10, selenamr
Ifa component is made of two or more materials with different modulus of elasticity (e), it is called a composite member and we calculate the factor·n". mention the formula for calculating n". also, ifn> 1, explain what will happen to the 1. transformed. gi) ifn 1, what will happen to the material when transformed material when
Answers: 1
Do you know the correct answer?
Dining Philosopher Problem (Please use skeleton code)

Task 1 Eliminate the deadlock by a...

Questions in other subjects:

Konu
History, 14.01.2021 18:30
Konu
Mathematics, 14.01.2021 18:30
Konu
English, 14.01.2021 18:30
Konu
Mathematics, 14.01.2021 18:30
Konu
Mathematics, 14.01.2021 18:30