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

前端 未结 5 863
粉色の甜心
粉色の甜心 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 20:00

    It depends on the behavior you want: man -s 3 exit and man _exit for more details on your system. In general I believe the _exit doesn't run functions that are registered with atexit() whereas exit does (these functions better not call exit - otherwise you get recursion).

    In general I would prefer exit over _exit except for in functions registered with atexit, in those I would call _exit, if needed.

提交回复
热议问题