Creating multiple child processes in a program

clamum

Lifer
Feb 13, 2003
26,256
406
126
I don't understand how you would create multiple child processes within a single program and have the parent wait for them to finish some calculation (say they multiply some numbers together, something simple). Then the parent prints out the results of their calculations.

I'm using Linux and C++... I just can't wrap my head around the problem. It's probably rather simple, I'm just getting tired and annoyed at the noisy high school kids in the next door computer lab. :confused:
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You need to understand how to create a child process and lock/suspend the parent until all child processes have checked in via some type of indicator flag.

Then the parent is free to output the results of the child processes.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Well I got it but it's probably a stupid way of doing it.

I have the pids in an array and fork()ed them in a loop. Then had several else-if statements checking if each pid was 0 and if so, did what the process was supposed to do. Finally, I had the wait( pidsarray) in a loop, then printed out the data. All I care is it works, I'm outta here.

Thanks for your help. :)