There's my test code below. I expect the output should print out all the child processes created before parent process terminates, but it does not. Please tell me why.
In the test code, it creates 10 processes and waits for them again and again. I want the program to run on the condition that I have at most 10 processes at the same time. My platform is Linux.
//#include <iostream>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>
//using namespace std;
void sig_chld(int signo)
{
pid_t pid;
int stat;
pid=wait(&stat);
fprintf(stderr, "child [%d] terminate\n",pid);
return;
}
int main(){
signal(SIGCHLD,sig_chld);
pid_t PID,old;
for(int i=0;i<1000;i++){
fprintf(stderr, "i == %d\n",i);
PID = fork();
switch(PID){
case -1:
fprintf(stderr, "fork fail");
break;
case 0:
exit(0);
break;
default:
if(i==0)old=PID;
if(i%100==99){
fprintf(stderr, "start waiting child [%d] with i = %d --------------------\n",old,i);
waitpid(old,NULL,0);
old = PID;
}
fprintf(stderr, "child [%d] create\n",PID);
}
}
fprintf(stderr, "final waiting for [%d] \n",PID);
waitpid(PID,NULL,0);
fprintf(stderr, "parent end\n");
return 0;
}
Below are the last few lines of the output. There are still a lot processes that don't terminate.
i == 992
child [13805] create
i == 993
child [13806] create
i == 994
child [13636] terminate
child [13637] terminate
child [13640] terminate
child [13810] create
i == 995
child [13811] create
i == 996
child [13812] create
i == 997
child [13642] terminate
child [13643] terminate
child [13814] create
i == 998
child [13645] terminate
child [13646] terminate
child [13816] create
i == 999
start waiting child [13666] with i = 999 --------------------
child [13817] create
final waiting for [13817]
child [13647] terminate
child [13648] terminate
child [13649] terminate
parent end