So I'm working on a program that will do a certain task multiple times. I've written the program once already using threads, but now I'm required to do it using processes with fork().
With threads, I'd simply create the specified number of threads, have them execute a function (changing a variable in shared memory), and then finally execute something that used the final value in shared memory.
Now however I want to do the same thing but I'm not sure how to do it as everytime I call fork() it essentially doubles itself. So obviously if I call fork() in a forloop, each parent and child process will then go through the loop calling fork() on their own.
So my question in a nutshell: how could I create an exact number of child processes using fork (lets say I need 5 children), and have my parent process wait until these 5 are done executing before it moves on?
Thanks for the help!