Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a C or C++ program using the std: thread class and the fork () system call function. You will need to create 2
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: . . . 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. Print out its current process ID. Create three threads using the std::thread class that all call the same thread function. Exit the process. . 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+11. 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 Random Number: 87 Thread ID: 1 Random Number: 78 Thread ID: 2 -- Random Number: 16 Process 2 ID: 59865 Thread ID: 0 --- Random Number: 78 -- Thread ID: 1 Random Number: 16 Thread ID: 2 Random Number: 94 Global Number: 87 Global Number: 165 -- Global Number: 181 Global Number: 78 Global Number: 94 Global Number: 188
Step by Step Solution
★★★★★
3.36 Rating (146 Votes )
There are 3 Steps involved in it
Step: 1
include include include include include int global 0 voi...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started