When you fork(), the operating system creates a clone of the parent (ie, it copies the parent). The parent
will not see changes to variables that the child makes.
If 1 process is chosen, the original process will continue on with the word-finding processing. If either 2 or 4 processes are chosen, the program should create these as child processes. These child processes should split the data file evenly if possible, and if not the last process should handle any extra records. Each child should only search the portion for which it is responsible, and communicate this answer back to the parent. The parent should sum the counts returned by each child and print the result. The time required to find the result should also be output.
i think i should implement pipes (just started reading about them now)
If all the child needs to do is return a number to the parent, pipes are one solution. Setting the exit code
of the child to the answer and having the parent read it via waitpid() is potentially another (though the
exit code has limited range).
Thought about using IPC Shareable memory to pass results from the children to the parent.
Since you are only updating totals - you can update the var from the children.
But this is not limited to counters, you can setup an array and then show the results of each child.
CrimsonAngel is correct - think of each child as a separate process with its own environment or scope.
The variables the children see, are not exactly what the parent sees.
Try printing the address of your counter variable in the child and parent processes.
You'll see they are different memory locations.
I've done this in Perl before and it works quite well.
I haven't tried it in C++... hmm new project. :) (learning C++ as well)
If you want the variables to be shared, you use threads, not child processes (as created by fork). Pipes or shared memory would work. The mmap call is a simpler way to create shared memory, you can allocate a blob of memory using mmap(MAP_PRIVATE|MAP_SHARED) before you fork, then write results into the shared memory from each process.