Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Petersons Solution (15 points) Execute the peterson program several times. Examine the output carefully. You should notice a problem in the implementation. Make

Part 1: Petersons 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 Petersons 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 (Note: threads may, but need not strictly alternate since we are using Petersons solution): image text in transcribed

*** USE THE BASE CODE PROVIDED

#include

#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

// 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

// Random wait - This code is just to ensure that the threads

// show data sharing problems

int max = rand() % 100000;

for (int x = 0; x

// End of random wait code

// End of the critical region

// TODO:

// Make sure that the other thread gets a turn

//

// HINT: You need one function call

}

pthread_exit(NULL);

}

int main()

{

int rc[2];

pthread_t ids[2];

int args[2];

count = 0;

for(unsigned int i = 0; i

args[i] = i;

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

}

for(unsigned int i = 0; i

pthread_join(ids[i], NULL);

}

std::cout

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

args[i] = i;

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

}

for(unsigned int i = 0; i

pthread_join(ids[i], NULL);

}

std::cout

pthread_exit(NULL);

}

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

Explain exothermic and endothermic reactions with examples

Answered: 1 week ago

Question

Write a short note on rancidity and corrosiveness.

Answered: 1 week ago