Write C C Program Using Std Thread Class Fork System Call Function Need Create 2 Processes Q43871847
.Write a C or C++ program using the std:thread class and the fork () system call function. You will need to create 2 processes – each process will create a thread and each thread adds a random value to a global variable. Firstly, create an integer global variable initialized to 0. Within the main function, create 2 sub-processes using the fork () function and a for loop. The Sub-processes will: • Print out its current process ID. Create three threads using the std::thread class that all call the same thread function. Exit the process. Create a Thread function that will do as follows: • Output the current thread ID to the screen. Create a random number between 1 and 100 and output that number to the screen. This can be done by using the rand () function to generate your randomly generated number. • Add the previously generated random number to the global variable and output the global variable value to the screen. The Parent Process (otherwise known as the main thread) will: o Wait for the first sub-process to finish before starting the second sub-process. To run this CPP program on Unix or Linux, type g++ -pthread progl.cpp -std=c+l1. o The -pthread flag is necessary to import the std:thread class library as well as the c++11 flag. Sample Output: An example of the expected output is below: NOTE: The global value will not save with the end of a process, because of the way the fork () function works. Process 1 ID: 59860 Thread ID: 0 Thread ID: 1 Thread ID: 2 Process 2 ID: 59865 Thread ID: 0 Thread ID: 1 Thread ID: 2 Random Number: 87 Global Number: 87 — – Random Number: 78 Global Number: 165 —- Random Number: 16 Global Number: 181 — Random Number: 78 Global Number: 78 — Random Number: 16 Global Number: 94 —- Random Number: 94 Global Number: 188 Show transcribed image text Write a C or C++ program using the std:thread class and the fork () system call function. You will need to create 2 processes – each process will create a thread and each thread adds a random value to a global variable. Firstly, create an integer global variable initialized to 0. Within the main function, create 2 sub-processes using the fork () function and a for loop. The Sub-processes will: • Print out its current process ID. Create three threads using the std::thread class that all call the same thread function. Exit the process. Create a Thread function that will do as follows: • Output the current thread ID to the screen. Create a random number between 1 and 100 and output that number to the screen. This can be done by using the rand () function to generate your randomly generated number. • Add the previously generated random number to the global variable and output the global variable value to the screen. The Parent Process (otherwise known as the main thread) will: o Wait for the first sub-process to finish before starting the second sub-process. To run this CPP program on Unix or Linux, type g++ -pthread progl.cpp -std=c+l1. o The -pthread flag is necessary to import the std:thread class library as well as the c++11 flag.
Sample Output: An example of the expected output is below: NOTE: The global value will not save with the end of a process, because of the way the fork () function works. Process 1 ID: 59860 Thread ID: 0 Thread ID: 1 Thread ID: 2 Process 2 ID: 59865 Thread ID: 0 Thread ID: 1 Thread ID: 2 Random Number: 87 Global Number: 87 — – Random Number: 78 Global Number: 165 —- Random Number: 16 Global Number: 181 — Random Number: 78 Global Number: 78 — Random Number: 16 Global Number: 94 —- Random Number: 94 Global Number: 188
Expert Answer
Answer to Write a C or C++ program using the std:thread class and the fork () system call function. You will need to create 2 proc…
OR