Question
Write a C/C++ program that creates N child processes and use the child processes to count number in a file. The input file has one
Write a C/C++ program that creates N child processes and use the child processes to count number in a file. The input file has one number in each line, and different lines can have identical numbers. Given N, you need to divide the input file evenly into N parts and count the numbers in each part with a child process. For example, an input file may look like:
1
2
2
10
5
3
10
If N=2, the file will be divided into two parts: the first four lines and the last three lines. The output of the first child process is
1 1
2 2
10 1
It means that 1 appears once, 2 appears twice, and 10 appears once in the first four lines. Similarly, the output of the second process is
5 1
3 1
10 1
Each of the child processes will write the output pairs into an output file. After all child process finish, the main process will read in the output files created by the child processes and merge the results. In the above example, the main process will read in the outputs for the two parts and print the final result to console.
1 1
2 2
3 1
5 1
10 2
The order of the numbers in the final result does not matter. The number of child processes should be specified as an input argument. Suppose your program is compile to a.out. If you run ./a.out input_filename 5, the program should create 5 child processes to do the task.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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