Write a C++ program that creates n child threads, where each child thread prints a message into STDOUT with the assigned number by the parent thread. The range of the integer values assigned by the main thread to the child threads goes from 0 until n- 1. Your program must use synchronization mechanisms to guarantee that the child threads print the message in the opposite order they were created, starting with child threads with odd numbers and finishing with child threads with even numbers. Your program will receive from STDIN the number of child threads (nchildthreads). For nchildthreads = 5, the expected output is: I am Thread 3 I am Thread 1 I am Thread 4 I am Thread 2 I am Thread 0 NOTES 1. You must create n child threads (where n is the value that your program receives from STDIN). 2. The integer number assigned to each child thread has a value from 0 to n-1. 3. Zero is considered an even number. 4. You can add as many global variables as needed. 5. You can only use POSIX semaphores, pthreads mutex semaphore, or pthreads condition variables to achieve synchronization (A penalty of 100% will be applied to solutions using mechanisms other than the synchronization mechanisms listed before).
Write a C++ program that creates n child threads, where each child thread prints a message into STDOUT with the assigned number by the parent thread. The range of the integer values assigned by the main thread to the child threads goes from 0 until n- 1. Your program must use synchronization mechanisms to guarantee that the child threads print the message in the opposite order they were created, starting with child threads with odd numbers and finishing with child threads with even numbers. Your program will receive from STDIN the number of child threads (nchildthreads). For nchildthreads = 5, the expected output is: I am Thread 3 I am Thread 1 I am Thread 4 I am Thread 2 I am Thread 0 NOTES 1. You must create n child threads (where n is the value that your program receives from STDIN). 2. The integer number assigned to each child thread has a value from 0 to n-1. 3. Zero is considered an even number. 4. You can add as many global variables as needed. 5. You can only use POSIX semaphores, pthreads mutex semaphore, or pthreads condition variables to achieve synchronization (A penalty of 100% will be applied to solutions using mechanisms other than the synchronization mechanisms listed before).
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...
Related questions
Question
100%
Need help completing C++ program
#include
#include
#include
#include
#include
static pthread_mutex_t bsem;
static pthread_cond_t waitTurn = PTHREAD_COND_INITIALIZER;
static int turn;
static int nthreads;
void *print_in_reverse_order_odd_then_even(void *void_ptr_argv)
{
// std::cout << "I am Thread " << /*variable identifier*/ << std::endl;
return NULL;
}
int main()
{
std::cin >> nthreads;
pthread_mutex_init(&bsem, NULL); // Initialize access to 1
pthread_t *tid= new pthread_t[nthreads];
int *threadNumber=new int[nthreads];
//HINT: this code determines the starting thread (thread with the highest odd number).
//You can erase this if statement if your solution does not need to know the starting child thread number.
if ((nthreads-1)%2!=0)
turn = nthreads -1;
else
turn = nthreads -2;
for(int i=0;i
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
Recommended textbooks for you
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
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
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY