Exit (operating system)
Encyclopedia
On many computer operating systems, a computer process terminates its execution
by making an exit system call
. More generally, an exit in a multithreading
environment means that a thread
of execution has stopped running. The operating system
reclaims resources
(memory
, files
, etc.) that were used by the process. The process is said to be a dead process after it terminates.
and Unix-like
operating systems, a process is started when its parent process
executes a fork
system call
. The parent process may then wait
for the child process
to terminate, or may continue execution (possibly forking off other child processes). When the child process terminates ("dies"), either normally by calling exit, or abnormally
due to a fatal error
or signal
(e.g., SIGTERM
, SIGINT
, SIGKILL
), an exit status
is returned to the operating system and a SIGCHLD
signal is sent to the parent process. The exit status can then be retrieved by the parent process via the wait
system call.
Most operating systems allow the terminating process to provide a specific exit status
to the system, which is made available to the parent process. Typically this is a small integer value, although some operating systems (e.g., Plan 9) allow a character string to be specified.
It is sometimes possible to bypass the usual cleanup; C99
offers the
routine when the
becomes a child of a special root process
, which then waits for the child process to terminate. Likewise, a similar strategy is used to deal with a zombie process
, which is a child process that has terminated but whose exit status is ignored by its parent process. Such a process becomes the child of a special parent process, which retrieves the child's exit status and allows the operating system to complete the termination of the dead process. Dealing with these special cases keeps the system process table in a consistent state.
to the system.
C
:
or:
C++
:
COBOL
:
IDENTIFICATION DIVISION.
PROGRAM-ID. SUCCESS-PROGRAM.
PROCEDURE DIVISION.
MAIN.
MOVE ZERO TO RETURN-CODE.
END PROGRAM.
Java
:
DOS
Batch file
:
Perl
:
PHP
:
Python
:
Unix shell
:
Pascal
:
DOS
Assembly
:
; For MASM/TASM
.MODEL SMALL
.STACK
.CODE
main PROC NEAR
MOV AH, 4Ch ; Service 4Ch - Terminate with Error Code
MOV AL, 0 ; Error code
INT 21h ; Interrupt 21h - DOS General Interrupts
main ENDP
END main ; Starts at main
Some programmers may prepare everything for INT 21h at once:
MOV AX, 4C00h ; replace the 00 with your error code in HEX
Linux
Assembly:
; For NASM
MOV AL, 1 ; Function 1: exit
MOV EBX, 0 ; Return code
INT 80h ; The only interrupt Linux uses!
# For GAS
.text
.globl _start
_start:
movl $1, %eax # System call number 1: exit
movl $0, %ebx # Exits with exit status
0
int $0x80 # Passes control to interrupt vector
# invokes system call—in this case system call
# number 1 with argument 0
Execution (computers)
Execution in computer and software engineering is the process by which a computer or a virtual machine carries out the instructions of a computer program. The instructions in the program trigger sequences of simple actions on the executing machine...
by making an exit system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...
. More generally, an exit in a multithreading
Thread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...
environment means that a thread
Thread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...
of execution has stopped running. The operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...
reclaims resources
Resource (computer science)
A resource, or system resource, is any physical or virtual component of limited availability within a computer system. Every device connected to a computer system is a resource. Every internal system component is a resource...
(memory
Computer storage
Computer data storage, often called storage or memory, refers to computer components and recording media that retain digital data. Data storage is one of the core functions and fundamental components of computers....
, files
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...
, etc.) that were used by the process. The process is said to be a dead process after it terminates.
How it works
Under UnixUnix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
and Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....
operating systems, a process is started when its parent process
Parent process
In computing, a parent process is a process that has created one or more child processes.- Unix :In the operating system Unix, every process except is created when another process executes the fork system call. The process that invoked fork is the parent process and the newly-created process is...
executes a fork
Fork (operating system)
In computing, when a process forks, it creates a copy of itself. More generally, a fork in a multithreading environment means that a thread of execution is duplicated, creating a child thread from the parent thread....
system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...
. The parent process may then wait
Wait (operating system)
In modern computer operating systems, a process may wait on another process to complete its execution. In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait system call, which suspends the execution of the parent process...
for the child process
Child process
A child process in computing is a process created by another process .A child process inherits most of its attributes, such as open files, from its parent. In UNIX, a child process is in fact created as a copy of the parent...
to terminate, or may continue execution (possibly forking off other child processes). When the child process terminates ("dies"), either normally by calling exit, or abnormally
Abnormal end
An ABEND is an abnormal termination of software, or a program crash.This usage derives from an error message from the IBM OS/360, IBM zOS operating systems. Usually capitalized, but may appear as "abend"...
due to a fatal error
Fatal error
In computing, a fatal error or fatal exception error is an error that causes a program to abort and may therefore return the user to the operating system. When this happens, data that the program was processing may be lost. A fatal error is usually distinguished from a fatal system error...
or signal
Signal (computing)
A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. Essentially it is an asynchronous notification sent to a process in order to notify it of an event that occurred. When a signal is sent to a process, the operating system...
(e.g., SIGTERM
SIGTERM
On POSIX-compliant platforms, SIGTERM is the signal sent to a process to request its termination. The symbolic constant for SIGTERM is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms, however on the vast majority of systems,...
, SIGINT
SIGINT (POSIX)
On POSIX-compliant platforms, SIGINT is the signal sent to a process by its controlling terminal when a user wishes to interrupt the process. In source code, SIGINT is a symbolic constant defined in the header file signal.h...
, SIGKILL
SIGKILL
On POSIX-compliant platforms, SIGKILL is the signal sent to a process to cause it to terminate immediately. The symbolic constant for SIGKILL is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms, however on the vast majority of...
), an exit status
Exit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...
is returned to the operating system and a SIGCHLD
SIGCHLD
On POSIX-compliant platforms, SIGCHLD is the signal sent to a process when a child process terminates. The symbolic constant for SIGCHLD is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms....
signal is sent to the parent process. The exit status can then be retrieved by the parent process via the wait
Wait (operating system)
In modern computer operating systems, a process may wait on another process to complete its execution. In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait system call, which suspends the execution of the parent process...
system call.
Most operating systems allow the terminating process to provide a specific exit status
Exit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...
to the system, which is made available to the parent process. Typically this is a small integer value, although some operating systems (e.g., Plan 9) allow a character string to be specified.
Clean up
The exit operation typically performs clean-up operations within the process space before returning control back to the operating system. Some systems and programming languages allow user subroutines to be registered so that they are invoked at program termination before the process actually terminates for good. As the final step of termination, a primitive system exit call is invoked, informing the operating system that the process has terminated and allows it to reclaim the resources used by the process.It is sometimes possible to bypass the usual cleanup; C99
C99
C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...
offers the
_exit
function which terminates the current process without any extra program clean-up. This may be used, for example, in a fork-execFork-exec
Fork-exec is a commonly used technique in Unix whereby an executing process spawns a new program. fork is the name of the system call that the parent process uses to "divide" itself . After calling fork, the created child process is an exact copy of the parent except for the return value...
routine when the
execExecExec may refer to:* Exec, short for "executive officer"* exec , an operating system function for running a program* Exec , the OS kernel of Amiga computers...
call fails to replace the child process; calling atexit
routines would erroneously release resources belonging to the parent.Orphans and zombies
Some operating systems handle a child process whose parent process has terminated in a special manner. Such an orphan processOrphan process
An orphan process is a computer process whose parent process has finished or terminated, though itself remains running.In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process. This operation is called re-parenting and occurs automatically...
becomes a child of a special root process
Init
init is a program for Unix-based computer operating systems that spawns all other processes. It runs as a daemon and typically has PID 1. The boot loader starts the kernel and the kernel starts init...
, which then waits for the child process to terminate. Likewise, a similar strategy is used to deal with a zombie process
Zombie process
On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the process that started the process to read its exit status. The term zombie process...
, which is a child process that has terminated but whose exit status is ignored by its parent process. Such a process becomes the child of a special parent process, which retrieves the child's exit status and allows the operating system to complete the termination of the dead process. Dealing with these special cases keeps the system process table in a consistent state.
Examples
The following programs terminate and return a success exit statusExit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...
to the system.
C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
:
or:
C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
:
COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....
:
IDENTIFICATION DIVISION.
PROGRAM-ID. SUCCESS-PROGRAM.
PROCEDURE DIVISION.
MAIN.
MOVE ZERO TO RETURN-CODE.
END PROGRAM.
Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
:
DOS
DOS
DOS, short for "Disk Operating System", is an acronym for several closely related operating systems that dominated the IBM PC compatible market between 1981 and 1995, or until about 2000 if one includes the partially DOS-based Microsoft Windows versions 95, 98, and Millennium Edition.Related...
Batch file
Batch file
In DOS, OS/2, and Microsoft Windows, batch file is the name given to a type of script file, a text file containing a series of commands to be executed by the command interpreter....
:
Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
:
PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
:
Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
:
Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...
:
Pascal
Pascal (programming language)
Pascal is an influential imperative and procedural programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structuring.A derivative known as Object Pascal...
:
DOS
DOS
DOS, short for "Disk Operating System", is an acronym for several closely related operating systems that dominated the IBM PC compatible market between 1981 and 1995, or until about 2000 if one includes the partially DOS-based Microsoft Windows versions 95, 98, and Millennium Edition.Related...
Assembly
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...
:
; For MASM/TASM
Tasm
TASM can refer to:*Turbo Assembler, Borland's x86 assembler* Turbo Assembler, Omikron's Commodore 64-based 6502 assembler*Table Assembler, a table driven cross-assembler for small microprocessors.*Tomahawk Anti-Ship Missile...
.MODEL SMALL
.STACK
.CODE
main PROC NEAR
MOV AH, 4Ch ; Service 4Ch - Terminate with Error Code
MOV AL, 0 ; Error code
INT 21h ; Interrupt 21h - DOS General Interrupts
main ENDP
END main ; Starts at main
Some programmers may prepare everything for INT 21h at once:
MOV AX, 4C00h ; replace the 00 with your error code in HEX
Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...
Assembly:
; For NASM
MOV AL, 1 ; Function 1: exit
MOV EBX, 0 ; Return code
INT 80h ; The only interrupt Linux uses!
# For GAS
GNU Assembler
The GNU Assembler, commonly known as GAS , is the assembler used by the GNU Project. It is the default back-end of GCC. It is used to assemble the GNU operating system and the Linux kernel, and various other software. It is a part of the GNU Binutils package.GAS' executable is named after as, a...
.text
.globl _start
_start:
movl $1, %eax # System call number 1: exit
movl $0, %ebx # Exits with exit status
Exit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...
0
int $0x80 # Passes control to interrupt vector
Interrupt vector
An interrupt vector is the memory address of an interrupt handler, or an index into an array called an interrupt vector table that contains the memory addresses of interrupt handlers...
# invokes system call—in this case system call
# number 1 with argument 0
See also
- exit (command)Exit (command)exit is a command used in many operating system command line shells and scripting languages. The command causes the shell or program to terminate. If performed within an interactive command shell, the user is logged out of their current session, and/or user's current console or terminal connection...
- Child processChild processA child process in computing is a process created by another process .A child process inherits most of its attributes, such as open files, from its parent. In UNIX, a child process is in fact created as a copy of the parent...
- ExecutionExecution (computers)Execution in computer and software engineering is the process by which a computer or a virtual machine carries out the instructions of a computer program. The instructions in the program trigger sequences of simple actions on the executing machine...
- Exit commandExit (command)exit is a command used in many operating system command line shells and scripting languages. The command causes the shell or program to terminate. If performed within an interactive command shell, the user is logged out of their current session, and/or user's current console or terminal connection...
- Exit statusExit statusThe exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...
- ForkFork (operating system)In computing, when a process forks, it creates a copy of itself. More generally, a fork in a multithreading environment means that a thread of execution is duplicated, creating a child thread from the parent thread....
- Kill commandKill (Unix)In computing, kill is a command that is used in several popular operating systems to send signals to running processes, for example to request the termination of this process.-Unix and Unix-like:...
- Orphan processOrphan processAn orphan process is a computer process whose parent process has finished or terminated, though itself remains running.In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process. This operation is called re-parenting and occurs automatically...
- ProcessProcess (computing)In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...
- Parent processParent processIn computing, a parent process is a process that has created one or more child processes.- Unix :In the operating system Unix, every process except is created when another process executes the fork system call. The process that invoked fork is the parent process and the newly-created process is...
- SIGCHLDSIGCHLDOn POSIX-compliant platforms, SIGCHLD is the signal sent to a process when a child process terminates. The symbolic constant for SIGCHLD is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms....
- TaskTask (computers)A task is an execution path through address space. In other words, a set of program instructions that are loaded in memory. The address registers have been loaded with the initial address of the program. At the next clock cycle, the CPU will start execution, in accord with the program. The sense is...
- ThreadThread (computer science)In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...
- WaitWait (operating system)In modern computer operating systems, a process may wait on another process to complete its execution. In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait system call, which suspends the execution of the parent process...