#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
#include <signal.h>
#include <list>
using namespace std;
void handler(int){
pid_t pid = wait(NULL);
// cout << pid << " exit" << endl;
printf("%d exit\n",pid);
}
int main(){
signal(SIGCHLD,handler);
// cout << getpid() << endl;
// list<pid_t> pids;
pid_t pid = -1;
for(int i=0;i<10;++i){
if(pid==0){
sleep(5);
// cout << "pid:" << getpid() << endl;
printf("pid:%d\n",getpid());
}else{
pid = fork();
/*if(pid!=0){
pids.push_front(pid);
}*/
}
}
sleep(50);
// pause();
/*
if(pid!=0){
for(auto pid:pids){
cout << "before wait:" << pid << endl;
waitpid(pid,NULL,0);
cout << "end wait:" << pid << endl;
}
pause();
}*/
}