I have this code:
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
pid_t pid = fork();
if ( !pid ) {
char *aux[] = {argv[i], NULL};
execvp(aux[0], aux);
printf("\nChild %d executed %s with exit code %d\n", getpid(), aux[0], i);
_exit(i);
}
}
for (int i = 1; i < argc; i++) {
printf("Child dead\n");
wait(NULL);
}
return 0;
}
When running, I noticed printf(...)
after execvp on child process never runs. I checked man exec
where I found:
"The exec() family of functions replaces the current process image with a new process image."
Does this mean that besides printf
, _exit(i)
is also not executed? If so, how can I kill child process? Does execvp new process image takes care of that?
EDIT: Assuming I want to do a printf (or any other instruction) after execvp line, as shown in the code, is there any way?
EDIT[2]: To make it more clear. If I have this chunk:
int main(void) {
for (int i = 0; i < 10; i++) {
pid_t pid = fork();
if (!pid) {
printf("child %d\n", getpid());
//_exit(1);
}
else {
wait(NULL);
printf("parent here\n");
}
}
return 0;
}
Things go wild when exit(1)
is commented. So, my doubt is: is there any scenario where execvp
executes correctly and the origin child process (the one that calls execvp) does not exit like in _exit(n)
call, producing results like in the above chunk.