exit

A process can terminate normally in five ways:

  1. return from the main function, this is equivalent to calling exit .

  2. Calling the exit function, defined by ISO C, execute all exit handler which registered by atexit function and close all standard I/O stream. note that close I/O stream not close open file descriptor.

  3. Calling _exit or _Exit quit from user space and back to kernel directly, didn’t call exit handler or close I/O stream relative to exit

  4. Executing a return from the start routine of the last thread in the process. the process exits with a termination status of 0, no matter the thread return value.

  5. Calling the `pthread_exit` function from the last thread in the process. As with the previous case, the exit status of the process in this situation is always 0.

    The three forms of abnormal termination are as follows:

  6. Calling abort . This is a special case of the next item, as it generates the SIGABRT signal.

  7. Process receives certain signals.

  8. The last thread responds to a cancellation request

Regardless of how a process terminates, the same code in the kernel is eventually executed. This kernel code closes all the open descriptors for the process, releases the memory that it was using, and so on.

Regardless of how a process terminates, the process always has an exit status. For the three exit functions(exit,_exit, _Exit) this is done by passing an exit status as the argument to the function. In the case of an abnormal termination, however, the kernel—not the process — generates a termination status to indicate the reason for the abnormal termination.

Consider two exit situation

child terminates before parent

If parent called wait or waitpid will get the exit status of child and the child terminates correctly. If parent didn’t call wait and waitpid, the child will become zombies, and the kernel will recycle these zombies process resources eventually.

parent terminates before child

In such a case, init process becomes the parent process of any process whose parent terminates, we say that the process has been inherited by init. This way, we’re guaranteed that every process has a parent.

results matching ""

    No results matching ""