Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

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
[画像:4:32 Done Scanned Documents 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 e 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). ]
expand button
Transcribed Image Text:4:32 Done Scanned Documents 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 e 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).
[画像:main.cpp #include <pthread.h> #include <iostream> #include <string.h> #include <stdlib.h> #include <fcntl.h> 1 2. static pthread_mutex_t bsem; static pthread_cond_t waitTurn = PTHREAD_COND_INITIALIZER; static int turn; static int nthreads; 7 10 11 void *print_in_reverse_order_odd_then_even(void *void_ptr_argv) 13 { 12 // std::cout << "I am Thread " return NULL; 14 <</*variable identifier*/ << std::endl; 15 16 17 int main() 19 { 18 std::cin >> nthreads; pthread_mutex_init(&bsem, NULL); // Initialize access to 1 pthread t *tid= new pthread_t[nthreads]; int threadNumber=new int[nthreads] 20 21 22 23 24 //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 25 26 27 if ((nthreads-1)%2!=0) turn = nthreads -1; else 28 29 30 31 turn = nthreads -2; 32 33 for(int i=0;i<nthreads; i++) 34 - 35 36 // wait for the other threads to finish. for (int i = 0; i < nthreads; i++) pthread join(tid[i], NULL); 37 38 39 delete [] threadNumber; delete [] tid; return e; 40 41 42 43 ]
expand button
Transcribed Image Text:main.cpp #include <pthread.h> #include <iostream> #include <string.h> #include <stdlib.h> #include <fcntl.h> 1 2. static pthread_mutex_t bsem; static pthread_cond_t waitTurn = PTHREAD_COND_INITIALIZER; static int turn; static int nthreads; 7 10 11 void *print_in_reverse_order_odd_then_even(void *void_ptr_argv) 13 { 12 // std::cout << "I am Thread " return NULL; 14 <</*variable identifier*/ << std::endl; 15 16 17 int main() 19 { 18 std::cin >> nthreads; pthread_mutex_init(&bsem, NULL); // Initialize access to 1 pthread t *tid= new pthread_t[nthreads]; int threadNumber=new int[nthreads] 20 21 22 23 24 //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 25 26 27 if ((nthreads-1)%2!=0) turn = nthreads -1; else 28 29 30 31 turn = nthreads -2; 32 33 for(int i=0;i<nthreads; i++) 34 - 35 36 // wait for the other threads to finish. for (int i = 0; i < nthreads; i++) pthread join(tid[i], NULL); 37 38 39 delete [] threadNumber; delete [] tid; return e; 40 41 42 43
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
    Recommended textbooks for you
    Text book image
    Computer Networking: A Top-Down Approach (7th Edi...
    Computer Engineering
    ISBN:9780133594140
    Author:James Kurose, Keith Ross
    Publisher:PEARSON
    Text book image
    Computer Organization and Design MIPS Edition, Fi...
    Computer Engineering
    ISBN:9780124077263
    Author:David A. Patterson, John L. Hennessy
    Publisher:Elsevier Science
    Text book image
    Network+ Guide to Networks (MindTap Course List)
    Computer Engineering
    ISBN:9781337569330
    Author:Jill West, Tamara Dean, Jean Andrews
    Publisher:Cengage Learning
    Text book image
    Concepts of Database Management
    Computer Engineering
    ISBN:9781337093422
    Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
    Publisher:Cengage Learning
    Text book image
    Prelude to Programming
    Computer Engineering
    ISBN:9780133750423
    Author:VENIT, Stewart
    Publisher:Pearson Education
    Text book image
    Sc Business Data Communications and Networking, T...
    Computer Engineering
    ISBN:9781119368830
    Author:FITZGERALD
    Publisher:WILEY