Write a C or C++ program using the std: thread class and the fork () system...
Fantastic news! We've Found the answer you've been seeking!
Question:
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: . . . 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 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
Expert Answer:
Answer rating: 100% (QA)
include include include include include int global 0 voi... View the full answer
Related Book For
Numerical Methods With Chemical Engineering Applications
ISBN: 9781107135116
1st Edition
Authors: Kevin D. Dorfman, Prodromos Daoutidis
Posted Date:
Students also viewed these computer network questions
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
The lengths of the three sides of a triangle (in feet )are consecutive even integers. If the perimeter is 96 feet, find the value of the shortest of the three side lengths.
-
A converging lens of short focal length can take light diverging from a small source and refract it into a beam of parallel rays. A Fresnel lens, as shown in Figure 36.29, is used for this purpose in...
-
Loggins Lumber Company experienced net losses during the first two years of its operations. Year 3 was the companys first profitable year. Loggins uses the same accounting meth-ods for financial...
-
Stone Brewing Co. is a San Diego brewer that has sold its beers for over two decades. Stone has maintained its trademark and brand from the beginning, registering the STONE mark in 1998. Stone has...
-
Union Bay Plastics is investigating the purchase of automated equipment that would save $100,000 each year in direct labor and inventory carrying costs. This equipment costs $750,000 and is expected...
-
3. (25 points) Consider two firms out of a competitive industry. They have the following technologies: C(y) = y + 2y; C2(y) = 1.5y + 3y. Show these firms' individual supply functions on a...
-
In order to prepare the statement of cash flows for Tinker Toys Corporation for 2001, The accountant has compiled the following data regarding cash flows. ANSWER THE FOLLOWING QUESTIONS. Cash paid to...
-
Using C++ Standard Template Library (STL) Discussion (requirement for STL): 1. #include 2. Operations on queue q: a. q.empty() - test if queue is empty b. q.size()- how many items are queue c....
-
George needs to calculate the segment margin. The sales are $900,000, the variable costs are $250,000 and the direct fixed costs $175,000. What will Ben's segment margin be?
-
Reichman Manufacturing Company had the following account balances for the quarter ending September 30, unless otherwise noted: Amortization of manufacturing equipment $88,000 Amortization of office...
-
Judith sells a shop used in her business in July 2019 for 70,000. The shop had been bought by her mother in March 2006 for 25,000 and had been given to Judith in November 2008 when its market value...
-
Using the monthly return data given is the arithmetic mean excess return? what
-
The persons responsible for maintenance of the charge description master (CDM) at a facility should know how frequently permanent national HCPCS Level II codes are updated so that the CDM can be...
-
Part I. Label each part of speech in the numbered sentences below with the appropriate letter in parentheses to the right of each word (or groups of words indicated in square brackets).t The parts of...
-
To balance the chemical equation SiH3 + O2 SiO2 + HO, you could introduce coefficients a, b, c, d and write aSiH3 + bO2 cSiO + dHO then write linear equations for each element. The equation for Si...
-
Perform linear regression for the following data set. X 1.1 1.6 2.0 2.3 2.8 2.4 2.4 3.8 5.2 6.0 7.9
-
To test our claim about the accuracy of these methods, use Program 8.4 to calculate the integral which we know has the value I = 1 for any positive integer m. Program 8.4 We can write a nice compact...
-
Solve the system by Jacobis method with an initial guess (0, 0). 2x1 + x = 2 x12x = -2 (2.13.7)
-
The deletion rules for enforcing referential integrity include both Delete. Cascade and Delete Restrict In general, what criteria should a DBA use in deciding whether to use a Delete: Cascade or...
-
What is metadata? if database administrators need to define metadata, what kind of language should they use (DDL or DML)? Why?
-
What is a relational database?
Study smarter with the SolutionInn App