Consider this code snippet:
pid_t cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { // in child
execvp(argv[1], argv + 1);
perror("execvp");
_exit(EXIT_FAILURE);
}
// in parent
How shall I exit the child process if execvp returns? Shall I use exit() or _exit()?
You should definitely use
_Exit().exit()calls the functions you added withatexit()and deletes files created withtmpfile(). Since the parent process is really the one that wants these things done when it exists, you should call_Exit(), which does none of these.Notice
_Exit()with a capital E._exit(2)is probably not what you want to call directly.exit(3)and_Exit(3)will call this for you. If you don’t have_Exit(3), then yes,_exit()is what you wanted.