subject
Engineering, 14.02.2020 21:18 snowprincess99447

Peterson’s Solution (15 points)

Execute the peterson program several times.

Examine the output carefully. You should notice a problem in the implementation. Make sure to follow the logic in main() and to read the comments carefully.

Review Peterson’s solution to achieve mutual exclusion. Pay special attention to the algorithm and code used to implement it.
You may want to refer to the prep materials for background info (section 2.3.3 in the textbook).

Correct the problem. Look for the // TODO comments and address them (i. e., implement the functionality described in the comments).

Build and run your program and make sure that it works correctly.

Expected Output:
Your program should produce the output similar to the following:
Thread #0 count = 1 Thread #1 count = 2 Thread #0 count = 3 Thread #1 count = 4 Thread #0 count = 5 Thread #1 count = 6 Thread #0 count = 7 Thread #1 count = 8 Thread #0 count = 9 Thread #1 count = 10 Thread #0 count = 11 Thread #1 count = 12 Thread #0 count = 13 Thread #1 count = 14 Thread #0 count = 15 Thread #1 count = 16 Thread #0 count = 17 Thread #1 count = 18 Thread #0 count = 19 Thread #1 count = 20 Final count = 20

Code:

#include

#include

#define TOTAL_THREADS 2

int count;

int turn; // Shared variable, indicates

// whose turn it is to execute

bool interested[TOTAL_THREADS]; // Shared variable, indicates

// processes interested in executing

// The thread_id will be either 0 or 1

void enter_region(int thread_id)

{

int other; // ID of the other thread

other = 1 - thread_id; // The oposite of thread_id

// TODO: Add the code to indicate the

// thread's interest in executing.

// TODO: Indicate the thread's turn to execute next

// TODO: Busy wait until it is the thread's turn to execute

}

void leave_region(int thread_id)

{

// TODO: Add the code to set the flag

// indicating that the thread has

// exited the critical region.

}

void* myFunction(void* arg)

{

int thread_id = *((int*) arg);

for(unsigned int i = 0; i < 10; ++i) {

// TODO:

// Make sure that the thread waits for its turn

// before it enters the critical region.

//

// HINT: You need one function call

// Beginning of the critical region

count++;

std::cout << "Thread #" << thread_id << " count = " << count << std::endl;

// End of the critical region

// TODO:

// Make sure that the other thread gets a turn

//

// HINT: You need one function call

}

pthread_exit(NULL);

}

// HINT: It is not necessary to make any changes in main()

int main()

{

int rc[TOTAL_THREADS];

pthread_t ids[TOTAL_THREADS];

int args[TOTAL_THREADS];

count = 0;

for(unsigned int i = 0; i < TOTAL_THREADS; ++i) {

args[i] = i;

rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);

}

for(unsigned int i = 0; i < TOTAL_THREADS; ++i) {

pthread_join(ids[i], NULL);

}

std::cout << "Final count = " << count << std::endl;

pthread_exit(NULL);

}

ansver
Answers: 2

Another question on Engineering

question
Engineering, 04.07.2019 18:10
Abrake has a normal braking torque of 2.8 kip in and heat-dissipating cast-iron surfaces whose mass is 40 lbm. suppose a load is brought to rest in 8.0 s from an initial angular speed of 1600 rev/min using the normal braking torque; estimate the temperature rise of the heat dissipating surfaces.
Answers: 3
question
Engineering, 04.07.2019 18:10
Thermal stresses are developed in a metal when its a) initial temperature is changed b) final temperature is changed c) density is changed d) thermal deformation is prevented e) expansion is prevented f) contraction is prevented
Answers: 2
question
Engineering, 04.07.2019 18:10
Compute the pressure drop of 30°c air flowing with a mean velocity of 8 m/s in a circular sheet-metal duct 300 mm in diameter and 15 m long. use a friction factor, f 0.02, and pair = 1.1644 kg/m a. 37.26 pa b. 25.27 pa n c. 29.34 pa d. 30.52 pa
Answers: 1
question
Engineering, 04.07.2019 19:10
Aplate of dimensions 3 m x 3 m is placed 0.37 mm apart from a fixed plate. the plate requires a force of 2n to move at speed of 45 cm/s. evaluate the viscosity of the fluid in between the plates
Answers: 3
You know the right answer?
Peterson’s Solution (15 points)

Execute the peterson program several times.

...
Questions
question
Mathematics, 20.10.2019 05:10
question
Geography, 20.10.2019 05:10
Questions on the website: 13722363