Ok, so I checked the exit status and that's what I found (please correct me if I'm wrong):
the father waits with this function wait() until one child terminates. If this child is doing an exec, the father waits until this exec procedure is done.
I edited my code a bit, to test it:
Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid ;
if ( ( pid = fork () ) < 0 ) {
perror ("fork");
return 1;
}
if ( pid == 0 ) {
close (1);
if ( open("XXX", O_WRONLY | O_CREAT | O_TRUNC, 0666) < 0) {
perror("open");
return 2;
}
sleep(2); //EDITED: to see if the father waits
execl("/bin/ls", "ls" , ".", NULL); //EDITED as ls exit automatically
perror("exec");
return 3;
} else {
int status;
wait (&status); //EDITED to see what's going on
if (WIFEXITED(status))
printf("CHILD exited with %d\n", WEXITSTATUS(status));
if (WIFSIGNALED(status))
printf("signaled by %d\n", WTERMSIG(status));
if (WIFSTOPPED(status))
printf("stopped by %d\n", WSTOPSIG(status));
printf("DONE FATHER\n");
return 0;
}
}
So, I played around a bit and when giving a wrong command to exec leads to a return value of 3, which is correct. But the thing is that the father always waited till it got anything from his child.
TurboToJo