how to exit a child process - _exit() vs. exit

前端 未结 5 866
粉色の甜心
粉色の甜心 2020-11-29 19:24

Consider this code snippet:

pid_t cpid = fork();

if (cpid == -1) {
    perror(\"fork\");
    exit(EXIT_FAILURE);
}

if (cpid == 0) { // in child
    execvp(         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 19:58

    execvp will exit the child if successfull so you don't have to exit.

    On execve failure I simply use exit(EXIT_FAILURE); in the child.

    Edit : i found that after some research : http://www.unixguide.net/unix/programming/1.1.3.shtml

    So it's looks like it's better to use _exit() in a fork child specially when you are in C++ :p Thanks for your question i learned something :D

提交回复
热议问题