The exit system call is a fundamental operation in computer operating systems that terminates the execution of the calling process. In Unix-like operating systems, the low-level system call is typically named _exit, while the standard C library provides a higher-level function named exit.
Functionality
When a process invokes the exit system call, the operating system kernel performs several cleanup operations:
- Resource Reclamation: The kernel releases the memory allocated to the process (data, heap, and stack).
- File Descriptor Closure: All open file descriptors associated with the process are closed.
- Process State: The process transition into a "zombie" or "defunct" state. It remains in the process table until its parent process acknowledges its termination.
- Parent Notification: The kernel sends a
SIGCHLDsignal to the parent process to indicate that a child process has terminated.
Exit Status
The system call accepts an integer argument, known as the exit status or return code. By convention:
- 0: Indicates successful completion.
- Non-zero values: Indicate an error or an abnormal termination. The specific meaning of non-zero codes is defined by the application or the operating system's standard headers (e.g.,
<errno.h>).
The parent process can retrieve this status using the wait or waitpid system calls. This allows the parent to determine whether the child process succeeded or encountered a specific failure.
Implementation Differences
In modern Linux systems, the standard _exit system call is often implemented via the exit_group system call, which terminates all threads within a process's thread group.
In the C programming language, the library function exit() (defined in <stdlib.h>) differs from the raw system call _exit() (defined in <unistd.h>). The library function performs additional user-space cleanup, such as:
- Calling functions registered with
atexit. - Flushing and closing standard I/O streams (e.g.,
stdout). - Removing temporary files created with
tmpfile.
After these steps are completed, the library function invokes the underlying _exit system call to hand control back to the kernel.