Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Cor C++ program using the std:thread class and the fork() system call function You will need to create 2 processes - each process

image text in transcribed
Write a Cor 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 D. 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 D 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 progi.cpp -std-c+11. 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 --- Global Number: 87 Thread ID: 1 --- Random Number: 78 --- Global Number: 165 Thread ID: 2 --- Random Number: 16 --- Global Number: 181 Process 2 ID: 59865 Thread ID: 0 --- Random Number: 78 --- Global Number: 78 Thread ID: 1 --- Random Number: 16 --- Global Number: 94 Thread ID: 2 --- Random Number: 94 --- Global Number: 188

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions