#include #include #include #define TOTAL_THREADS 4 int count; pthread_mutex_t the_mutex; // phread mutex variable - initialize here if using the initializer macro void* myFunction(void* arg) { int actual_arg = *((int*) arg); for(unsigned int i = 0; i < 10; ++i) { // TODO: // Use a Pthread mutex to control // access to the critical region. // Beginning of the critical region count++; std::cout << "Thread #" << actual_arg << " count = " << count << std::endl; // End of the critical region // TODO: // Relinquish access to the Pthread mutex // since critical region is complete. // Random wait - This code is just to ensure that the threads // show data sharing problems int max = rand() % 100000; for (int x = 0; x < max; x++); // End of random wait code } pthread_exit(NULL); } int main() { int rc[TOTAL_THREADS]; pthread_t ids[TOTAL_THREADS]; int args[TOTAL_THREADS]; // TODO: Initialize the pthread mutex here if using the initialization function. 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); }   --------------------------- #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 < 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   // Random wait - This code is just to ensure that the threads // show data sharing problems int max = rand() % 1000000; for (int x = 0; x < max; x++); // End of random wait code } 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); }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

#include <iostream>
#include <pthread.h>
#include <stdlib.h>


#define TOTAL_THREADS 4


int count;
pthread_mutex_t the_mutex; // phread mutex variable - initialize here if using the initializer macro


void* myFunction(void* arg)
{
int actual_arg = *((int*) arg);

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

// TODO:
// Use a Pthread mutex to control
// access to the critical region.



// Beginning of the critical region

count++;
std::cout << "Thread #" << actual_arg << " count = " << count << std::endl;

// End of the critical region

// TODO:
// Relinquish access to the Pthread mutex
// since critical region is complete.


// Random wait - This code is just to ensure that the threads
// show data sharing problems
int max = rand() % 100000;

for (int x = 0; x < max; x++);

// End of random wait code



}

pthread_exit(NULL);
}


int main()
{
int rc[TOTAL_THREADS];
pthread_t ids[TOTAL_THREADS];
int args[TOTAL_THREADS];


// TODO: Initialize the pthread mutex here if using the initialization function.


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

 

---------------------------

#include <iostream>
#include <pthread.h>
#include <stdlib.h>

#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

 


// Random wait - This code is just to ensure that the threads
// show data sharing problems
int max = rand() % 1000000;

for (int x = 0; x < max; x++);

// End of random wait code
}

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

Part 1:
1. Execute the peterson program several times.
2. 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.
3. 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).
4. Correct the problem. Look for the // TODO comments and address
them (i.e., implement the functionality described in the comments).
5. Build and run your program and make sure that it works correctly.
Expected Output:
Your program should produce the output similar
threads may, but need not strictly alternate since we are using
Peterson's solution):
the following (Note:
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
Take a screenshot of a sample output and upload the picture as part of
your assignment submission.
Transcribed Image Text:Part 1: 1. Execute the peterson program several times. 2. 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. 3. 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). 4. Correct the problem. Look for the // TODO comments and address them (i.e., implement the functionality described in the comments). 5. Build and run your program and make sure that it works correctly. Expected Output: Your program should produce the output similar threads may, but need not strictly alternate since we are using Peterson's solution): the following (Note: 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 Take a screenshot of a sample output and upload the picture as part of your assignment submission.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY