exit
A process can terminate normally in five ways:
return
from the main function, this is equivalent to calling exit .Calling the
exit
function, defined by ISO C, execute all exit handler which registered byatexit
function and close all standard I/O stream. note that close I/O stream not close open file descriptor.Calling
_exit
or_Exit
quit from user space and back to kernel directly, didn’t call exit handler or close I/O stream relative toexit
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.
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:
Calling abort . This is a special case of the next item, as it generates the SIGABRT signal.
Process receives certain signals.
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.