Forking multiple child processes in C++

  • Context: Comp Sci 
  • Thread starter Thread starter md5fungi
  • Start date Start date
  • Tags Tags
    C++ Multiple
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the forking of multiple child processes in C++ to split a file into parts, sum those parts in parallel, and return the results to a parent process. The focus includes issues related to process management, inter-process communication, and the correct implementation of the logic for summing values from a file.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem where only the first child process computes a sum correctly, while subsequent processes return zero.
  • Another participant questions the logic in the while loop, suggesting that the condition for summing values is always failing due to the initialization of variables.
  • Clarification is provided that the assignment specifically requires the use of fork() and that the sums should be piped back to the parent process.
  • There is a discussion about the intention to spawn all child processes simultaneously rather than sequentially, with suggestions to remove the wait command.
  • A participant realizes that they need to increment a variable correctly within the loop to sum values, indicating a misunderstanding of the control flow.
  • Concerns are raised about the apparent order of process execution, with one participant noting that the first child may complete before the others are created.
  • Another participant suggests adding a timed loop to test for concurrency among child processes.
  • A final question is posed regarding the program not terminating automatically in Linux, which affects timing measurements.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the implementation details and the behavior of the processes. There is no consensus on the correct approach to ensure concurrent execution or on resolving the issue of the program not terminating as expected.

Contextual Notes

Participants mention the need for proper inter-process communication through piping, and there are unresolved questions about the timing and order of process execution. The discussion reflects a range of skill levels among participants, which may influence their understanding of the concepts involved.

md5fungi
Messages
24
Reaction score
0

Homework Statement



Need to split a file up into four parts, have four child processes assigned to a different part and sum that part, and then send the data to the parent process, which will then sum the four sums.

Homework Equations



Basic Fork() Knowledge and C++ Knowledge.


The Attempt at a Solution



I keep on having the same problem, regardless of how I code the program. The first child process does what it should, but the second, third, and fourth just print 0 for a sum. There must be some fundamental misunderstanding of the material I have, but I can't figure out what that is.

Code:
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <sys/wait.h>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	int i,n=0,total_numbers=0,number, status, split_numbers; 
	int previous_number[4];
	int my_number[4];
	int current_number[4] = {1,1,1,1};
	int sum[4] = {0,0,0,0};
	pid_t pid;
	ifstream myfile;
	myfile.open("test.dat");
	while (myfile >> number) { 
		total_numbers++;
	}
	split_numbers = total_numbers/4;
	myfile.close();
	pid = fork();

	for (i=0; i<4; i++) {
		if (pid == 0) {
			cout << "Child Process " << i << " " << getpid() << endl;
			myfile.open("test.dat");
			my_number[i] = split_numbers * (i+1);
			previous_number[i] = split_numbers * i + 1;
			while (myfile >> number) {
				if (my_number[i] >= current_number[i] && previous_number[i] <= current_number[i])                      {
					sum[i]+=number;
					current_number[i]++;
				}
			}
			cout << sum[i] << endl;
			myfile.close();
			exit(0);
		} else { 
			waitpid(pid, &status, 0);
			cout << "Parent Process " << getpid() << endl;
			pid = fork();
		}
	}
	return 0;
}
 
Physics news on Phys.org
I can't figure out what you were trying to do with your while loop. But the reason the sum is zero is because the test
previous_number <= current_number

always fails, since the latter is always 1 and the latter is larger than 1. (If split_numbers is positive, anyways)


About your multi-threading, I have three questions about your setup:
(1) Was the assignment specifically to use fork()?

(2) How were you planning on having the children return their partial sum to the parent?

(3) Did you really intend to spawn children one at a time, rather than spawn all four at once?
 
I don't see how previous_number is always 1... When "i" increases, the value of previous_number should increase; I even count'd the value to make sure. Then it's supposed to go through the file line by line until previous_number <= current_number, and then start summing until my_number >= current_number.

1.) Yes, the assignment was specifically to use fork.

2.) We are supposed to "pipe" the sums back to the parent, and then the parent is supposed to add them up.

3.) Actually, I intended to spawn all the children at the same time. I'd do this, I assume, by removing the "wait" command I have in there. This way it is easier to see if it's working, though.
 
Ohh... I figured it out. I'm supposed to increment current_number every time the loop reads the next line... but I was only doing it if it passed the test, which makes no sense..

Aha, I'm an idiot. Thanks for nudging me in the right direction!
 
md5fungi said:
2.) We are supposed to "pipe" the sums back to the parent, and then the parent is supposed to add them up.

3.) Actually, I intended to spawn all the children at the same time. I'd do this, I assume, by removing the "wait" command I have in there. This way it is easier to see if it's working, though.
Okay. This is a very sound approach to the problem -- I just wanted to make sure you weren't just making a mistake. (there is a wide variety of skill levels here, and fork() can be surprising to people who are used to things like java.lang.Thread or pthreads)
 
So, I have everything correctly implemented, except possibly my processes running concurrently. I got rid of the waitpid function, and it still seems like the order is Process 0, 1, 2, and then 3. Is there something else I need to do to make them run at the same time? Is there an easy way to tell?
 
Fork is a "slow" operation -- your first child probably finished its task before the second child got created.

If you want to see the concurrency, you could try adding a timed loop to the child task -- make child #n spin-wait for 5-n seconds or something like that, just to see if child 3 really finishes first.
 
Alright; thanks a lot for your help!
 
One last quick question: When I run my program in Linux, it doesn't quit automatically; I have to Ctrl-C out of the program. This is problematic, because I want to use "time ./a.out" to see how long my program runs, and I can only see that result after I push Ctrl-C, thereby giving me an incorrect result.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K