I want my parent process waits for all child processes to finish before continuing, and I have one solution.
int status;
pid_t pid = 0;
int num = 0;
for (int i = 0; i < NUMBER_OF_PROCESSES; i++)
{
pid = fork();
if (pid == 0)
{
//printf("Hello from Child\n");
sleep(5 - i);
printf("Hello from Child %d\n",i + 1);
num++;
return 0;
}
else if (pid)
{
waitpid(pid, &status, 0);
continue;
}
else
{
printf("Error\n");
exit(1);
}
}
printf("Hello from the process, currentPid : %d, pid : %d\n", getpid(), pid);
return 0;
But it seems that I have to wait each child process before it finish, is there any way can make all child processes have to be able to run in parallel?