Fork() and Waitpid()

Heres my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int parent=0, child=0, i, result, pid;
        pid = fork(); 
         
        	switch (pid)
           {
        		 case -1:
        			/* fork error */
        			printf("Error occoured with fork()\n");
        			exit(99);
        		 case 0:
        			/* child process */
        			   num += process(wordlist2,word);
        			   cout << "im in the child " << num << endl;
                       exit(0); 
        		 default:
        			 /* parent process*/
        			{
                     
        			 num2 += process(wordlist, word);
        			 cout << "im in the parent " << num2 << endl;

        			}
           


1
2
waitpid(-1, NULL, 0);   
    cout << num + num2 + num3 + num4 << endl;



for some reason it prints:
im in the child: 5
im in the parent: 4
4



... it should print:
im in the child: 5
im in the parent: 4
9

Im new to forks and waitpids .... could some one please give me a hand ^ ^

Thanks,
Crim
don't double post. although this is the correct bored to post this problem in.
Yeah, sorry i had posted it in the general board first then noticed this one and realized that this was the correct place.
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.
Interesting .. but my project is to use forks do you know a way around this? so i can get information from the child
Last edited on
Perhaps you should post the assignment so we can understand what you are being asked to do.
heres the bulk of it

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)
That's why I asked for the assignment details.

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.


http://digiassn.blogspot.com/2006/07/cc-unix-ipc-shared-memory-example.html

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.
Topic archived. No new replies allowed.