I'm new to system programming and curious how the exec function works. My first question is why does the child never print "I'm the child" after calling exec. How does exec replace the child process? My second question is why does the program (after calling exec) continue and ask for one more command line argument before it completely terminates. I'm not sure what's going on here. Anyone could explain what is going on would be very appreciated. Here is the code:
#include <stdio.h>
#include <unistd.h>
int main(void) {
if(fork() == 0){
printf("Hello from child!\n");
execl("/usr/bin/sort", "sort", "talk.c",NULL);
printf("I'm the child\n");
}
else{
printf("Hello from parent!\n");
printf("Iam the parent\n");
}
return 0;
}
You can read about execl
in https://linux.die.net/man/3/execl
The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for execve(2) for further details about the replacement of the current process image.)
exec
family REPLACES the current process image with a new process image so nothing after execl
happen.