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
-
Now that you have completed the Interpersonal Skills Project this semester, let's take some time to reflect on all that you have accomplished. Consider your experience with the term-long skills...
-
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.
-
Consider a fusion reaction in which a proton fuses with a neutron to form a deuterium nucleus. How much energy is released in this reaction?
-
One subunit of Hazelton Sports Company has the following financial results last month: Requirements 1. Complete the performance evaluation report for this subunit (round to four decimals). 2. Based...
-
Alpine Ski Shops Prepaid Rent balance is $4,500 on June 1. This prepaid rent rep resents six months rent. Journalize and post the adjusting entry on June 30 to record one months rent. Compute the...
-
The Biological Insect Control Corporation (BICC) has hired you as a consultant to evaluate the NPV of its proposed toad ranch. BICC plans to breed toads and sell them as ecologically desirable insect...
-
y = x = (b) Using the formula for a circle centered at the origin with radius 3, which is x+ y = 9, identify the lower half y a function of x and separately x as a function of y. A(y) = W = 0 X 3...
-
Paoli Consulting is a consulting firm owned and operated by Mary Paoli. The following end-of-period spreadsheet was prepared for the year ended March 31, 2019: During the year ended March 31, 2019,...
-
The following data represent the discrete probability distribution for the number of stars that reviewers gave a first edition statistics reference book. Stars 5 4 3 2 1 Probability 25% 35% 20% 10%...
-
The Bakery at the Lake produces organic bread that is sold by the loaf. Each loaf requires 1/2 of a pound of flour. The bakery pays $3.40 per pound of the organic flour used in its loaves. The bakery...
-
... Fix this program so that it compiles and prints the volume of a cube of side length 2, then the 12. volume of a cube and tetrahedron of side length 10. FixMe.java 1 public class FixMe 2 { 2345678...
-
True-breeding flies with red eyes and long wings were crossed to flies with white eyes and miniature wings. All F1 offspring had red eyes and long wings. The F1 females flies were then crossed to...
-
what ways can organizations align their business strategies with sustainability and CSR goals, and how does this alignment impact stakeholder relationships and organizational reputation ?
-
From the strongest chemical bond to the weakest, list the three types of chemical bonds. For each type: 1) briefly describe what causes the chemicals to bind AND 2) give one specific example of a...
-
In which of the following geographical areas has the greatest decline in share of worldwide output occurred over the past 40 years? A. U.S.A B. Europe. C. Latin America. D. Asia.
-
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)
-
Applying the criterion for equilibrium, derive the Clausius-Clapeyron equation.
-
A binary liquid mixture consists of \(60 \mathrm{~mol}\) per cent ethylene and \(40 \mathrm{~mol}\) per cent propylene. At \(423 \mathrm{~K}\), the vapour pressure of ethylene and propylene are...
-
The pure component vapour pressure of two organic liquids \(\mathrm{X}\) and \(\mathrm{Y}\) by Antoine equations are given by \[ \ln P_{1}^{\text {Sat }}=14.35-\frac{2942}{T+220} \] and \[...
Study smarter with the SolutionInn App