I have one, bad smell problem :(
I have this code:
int main(){
pid_t child, parent;
int status=0;
int i;
printf("parent = %d\n", getpid());
for(i=1;i<=5;i++){
if( (child = fork()) == 0){
sleep(i);
printf("i=%d, %d\n",i, getpid());
}
}
wait(0);
while( (parent =wait(&status)) > 0){
printf("Exit = %d, child = %d\n", status/256, parent);
}
}
and the output is similar to:
1, 21320
2, 21321
Exit = 0, child = 21321
3, 21322
Exit = 0, child = 21322
4, 21323
Exit = 0, child = 21323
5, 21324
Exit = 0, child = 21324
And I think that wait (0) does not wait for the entire subprocess, but only waits for the first exit and writes everything (Exit = ...).
Is there any way to do this:
1, 21320
2, 21321
3, 21322
4, 21323
5, 21324
Exit = 0, child = 21320
Exit = 0, child = 21321
Exit = 0, child = 21322
Exit = 0, child = 21323
Exit = 0, child = 21324
?