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

60,686

Solution 1

You should definitely use _Exit(). exit() calls the functions you added with atexit() and deletes files created with tmpfile(). 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.

Solution 2

The child of fork() should always call _exit().

Calling exit() instead is a good way to cause pending stdio buffers to be flushed twice.

Solution 3

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

Solution 4

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.

Share:
60,686
helpermethod
Author by

helpermethod

Updated on May 31, 2020

Comments

  • helpermethod
    helpermethod almost 4 years

    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()?

  • John Kugelman
    John Kugelman about 14 years
    +1 You want the failed child process to exit quietly as if it had never been created.
  • vpalmu
    vpalmu about 14 years
    Well since the old C standard allowed the linker to resolve case insensitive _Exit() cannot be a standard library function as it would conflict with the older _exit(). Sorry.
  • Variable Length Coder
    Variable Length Coder about 14 years
    On the contrary. _Exit(3) is a standard C library (ISO C99). _exit(2) is a POSIX.1 system call and not a C standard.
  • vpalmu
    vpalmu about 10 years
    exec doesn't return except in error, in which you call _exit.
  • Dr. Jan-Philip Gehrcke
    Dr. Jan-Philip Gehrcke over 7 years
    "_exit() is a Linux Kernel function" is not correct, see pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.htm‌​l
  • ImanKh
    ImanKh over 7 years
    You are right about that, but the rest of my comment is true. See: Unix Systems Programming by David Curry, page 291.
  • Yongwei Wu
    Yongwei Wu over 6 years
    Whether open resources such as files are closed after calling _Exit is implementation defined. You probably do not want this behaviour (regarding unflushed streams). All man pages I have seen say that _exit does not flush streams, so it is safer if a file has unflushed content when forking happens. The fact that _exit is not standard C is a moot point, as fork is Unix-specific too. The C standard committee may have special reasons to introduce _Exit, but there is no reason for Unix programmers to change _exit to _Exit at all. @VariableLengthCoder