3

At which moment a process in Unix is terminated? Is it necessary a command such as exit(0) or return 0 to be written in a program to terminate a process? My question is provoked by the following code:

pid_t pid=fork();
if(pid < 0)
{
         perror("Fork error\n");
         return 1;  
}
else if (pid==0) /* child */
{
        printf("CHILD My pid is:%d\n",getpid());
}
else /* parrent */
{
         wait(NULL);
         exit(0);
}

In this example, in the child we do NOT call exit(0)(so I think we do NOT terminate the process) and in the parent we call wait(NULL)(so we should wait for the process to end). The program terminates, so logically at one moment the child process ends. Can you explain me when does the child process terminate? If the child terminates after

else if (pid==0) /* child */
    {
            printf("CHILD My pid is:%d\n",getpid());
    }/* Probably the child process terminates here but then what is the point of using exit(0) */

, well then what is the point of using exit(0)?

  • 2
    Is this a C program? It doesn't look like a complete program, and the answer may depend on seeing the rest of the code (IMHO the child process is exiting by virtue of completing the rest of the code that you haven't shown). exit() will end the program before executing any remaining instructions. – Jeff Schaller Sep 25 '15 at 16:09
3

A C program will end in one of three conditions:

  1. The program returns from the main function. If the return value is 0, that indicates success, otherwise the return value is the exit state. C99 also allows (but discourages) a main function with a void return type, where returning from main has the same semantics as returning 0 from a main with int return type.
  2. The program returns one of the exit functions: exit, _exit, or the new quick_exit function in C11. These functions all do not return and all have the effect of terminating the program, but what they actually do differs (e.g., quick_exit flushes buffers but does not run honour atexit calls).
  3. The OS kills the program in some way, e.g., because the program does something it is not allowed to do.
1

exit(0) (or perhaps more ideally exit(EXIT_SUCCESS)) can be used to explicitly indicate that everything went well. That is, it is more of a hint to someone reading the code as to what is expected to happen, and to prevent any subsequent code from being reached (there are compiler warnings that can be enabled for unreached code). The child should probably also have an _exit call to indicate that it is done, as otherwise the code will fall to whatever follows the error/child/parent conditional block.

Otherwise, processes may terminate normally (via exit, return in main, or _exit), or abnormally, by calling abort, or by eating a signal (via raise or from elsewhere) that causes the program to exit.

  • The child process should probably call _Exit() to avoid flushing stdio buffers in child process, and then later the same valued bytes in parent process, calling functions registered with atexit(). See stackoverflow.com/questions/2329640/… – Bruce Ediger Sep 25 '15 at 16:33
  • Huh, Linux man page indicates "The function _Exit() is equivalent to _exit()" and OpenBSD doesn't mention any differences for them. – thrig Sep 25 '15 at 16:47
  • _exit is an alternative to exit. It has different semantics in what it actually does, but if you've run exit, there's no need to also do _exit anymore. – Wouter Verhelst Sep 25 '15 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.