Hey people! I just have a question about running child processes in parallel in C using fork().
So what I did was to establish several child processes via the fork() function available using a for-loop. Then, I did my calculations on each child process. An example format would be:
Given that, my problem is this: I want to stop further calculations if one of the child processes, after the wait() function is called by the parent, returns an exit status of 0. So I would like to kill everything, using the kill() function.
Note that I kept the process IDs in an array so that I can just keep track of each child process IDs.
So what would be the best way to approach that problem? Thanks!
So what I did was to establish several child processes via the fork() function available using a for-loop. Then, I did my calculations on each child process. An example format would be:
Code:
pid_t process_IDs[num_of_cores];
for (int i = 0; i < num_of_cores; i++) {
process_IDs[i] = fork();
if (process_IDs[i] == 0) {
// Do calculations here in the child.
...
} else if (process_IDs[i] < 0) {
fprintf(stderr, "fork() error: no child can be created.\n");
exit(EXIT_FAILURE);
}
}
Given that, my problem is this: I want to stop further calculations if one of the child processes, after the wait() function is called by the parent, returns an exit status of 0. So I would like to kill everything, using the kill() function.
Note that I kept the process IDs in an array so that I can just keep track of each child process IDs.
So what would be the best way to approach that problem? Thanks!