Answered step by step
Verified Expert Solution
Link Copied!

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 processes - each

image text in transcribed
image text in transcribed
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 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 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 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

Step: 3

blur-text-image

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

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions