On many computer
operating systems, a
computer process terminates its
execution by making an exit
system call
In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
. More generally, an exit in a
multithreading environment means that a
thread
Thread may refer to:
Objects
* Thread (yarn), a kind of thin yarn used for sewing
** Thread (unit of measurement), a cotton yarn measure
* Screw thread, a helical ridge on a cylindrical fastener
Arts and entertainment
* ''Thread'' (film), 2016 ...
of execution has stopped running. For
resource management, the
operating system
An operating system (OS) is system software that manages computer hardware, software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ef ...
reclaims
resources (
memory
Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembered ...
,
files
File or filing may refer to:
Mechanical tools and processes
* File (tool), a tool used to ''remove'' fine amounts of material from a workpiece
**Filing (metalworking), a material removal process in manufacturing
** Nail file, a tool used to gent ...
, etc.) that were used by the process. The process is said to be a ''
dead process'' after it terminates.
How it works
Under
Unix
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
and
Unix-like
A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating systems, a process is started when its
parent process executes a ''
fork''
system call
In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
. The parent process may then
wait
Wait or WAIT may refer to:
Music
* Wait (musician), British town pipers
Albums and EPs
* ''Wait'' (The Polyphonic Spree EP), by The Polyphonic Spree
* ''Wait'' (Emanuel Nice EP), a 2002 EP released by the band Emanuel Nice
* ''Wait'' (Stee ...
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 exception or
signal (e.g.,
SIGTERM
Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-co ...
,
SIGINT
Signals intelligence (SIGINT) is intelligence-gathering by interception of '' signals'', whether communications between people (communications intelligence—abbreviated to COMINT) or from electronic signals not directly used in communication ...
,
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
Wait or WAIT may refer to:
Music
* Wait (musician), British town pipers
Albums and EPs
* ''Wait'' (The Polyphonic Spree EP), by The Polyphonic Spree
* ''Wait'' (Emanuel Nice EP), a 2002 EP released by the band Emanuel Nice
* ''Wait'' (Stee ...
'' 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 an integer value, although some operating systems (e.g.,
Plan 9 from Bell Labs) allow a
character string to be returned. Systems returning an integer value commonly use a zero value to indicate successful execution and non-zero values to indicate error conditions. Other systems (e.g.,
OpenVMS
OpenVMS, often referred to as just VMS, is a multi-user, multiprocessing and virtual memory-based operating system. It is designed to support time-sharing, batch processing, transaction processing and workstation applications. Customers using Ope ...
) use even-numbered values for success and odd values for errors. Still other systems (e.g., IBM
z/OS and its predecessors) use ranges of integer values to indicate success, warning, and error completion results.
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
A programming language is a system of notation for writing computer program, computer programs. Most programming languages are text-based formal languages, but they may also be visual programming language, graphical. They are a kind of computer ...
allow user
subroutines
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Functions may ...
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 offers the
_exit()
function which terminates the current process without any extra program clean-up. This may be used, for example, in a
fork-exec routine when the
exec
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 process
An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.
Unix-like
In a Unix-like operating system any orphaned process will be immediately adopted by an implementation-defin ...
'' 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
On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the " Terminated state ...
'', 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 status to the system.
COBOL
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily ...
:
IDENTIFICATION DIVISION.
PROGRAM-ID. SUCCESS-PROGRAM.
PROCEDURE DIVISION.
MAIN.
MOVE ZERO TO RETURN-CODE.
END PROGRAM.
Fortran:
program wiki
call exit(0)
end program wiki
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
:
public class Success
Pascal:
program wiki;
begin
ExitCode := 0;
exit
end.
DR-DOS batch file:
exit 0
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
:
#!/usr/bin/env perl
exit;
PHP
PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
:
Python:
#!/usr/bin/env python3
import sys
sys.exit(0)
Unix shell
A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system t ...
:
exit 0
DOS assembly
Assembly may refer to:
Organisations and meetings
* Deliberative assembly, a gathering of members who use parliamentary procedure for making decisions
* General assembly, an official meeting of the members of an organization or of their representa ...
:
''; For MASM
The Microsoft Macro Assembler (MASM) is an x86 assembler that uses the Intel syntax for MS-DOS and Microsoft Windows. Beginning with MASM 8.0, there are two versions of the assembler: One for 16-bit & 32-bit assembly sources, and another (ML64 ...
/ TASM''
.MODEL SMALL
.STACK
.CODE
main PROC NEAR
MOV AH, 4Ch ''; Service 4Ch - Terminate with Error Code''
MOV AL, 0 ''; Error code''
INT 21h
The DOS API is an API which originated with 86-DOS and is used in MS-DOS/ PC DOS and other DOS-compatible operating systems. Most calls to the DOS API are invoked using software interrupt 21h ( INT 21h). By calling INT 21h with a subfunc ...
''; 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 ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which i ...
32-bit x86 Assembly:
; For NASM
MOV AL, 1 ; Function 1: exit()
MOV EBX, 0 ; Return code
INT 80h ; ''# Passes control to interrupt vector''
''
invokes system call
��in this case system call''
''# number 1 with argument 0''
''# For GAS''
.text
.global _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''
Linux
Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which i ...
64-bit x86 64 Assembly: for FASM
FASM (''flat assembler'') is an assembler for x86 processors. It supports Intel-style assembly language on the IA-32 and x86-64 computer architectures. It claims high speed, size optimizations, operating system (OS) portability, and macro a ...
format ELF64 executable 3
entry start
segment readable executable
start:
; STUFF
; exiting
mov eax, 60 ; sys_exit syscall number: 60
xor edi, edi ; set exit status to 0 (`xor edi, edi` is equal to `mov edi, 0` )
syscall ; call it
OS X
macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and la ...
64-bit x86 64 Assembly: for NASM
global _main
section .text
_main:
mov rax, 0x02000001 ; sys_exit syscall number: 1 (add 0x02000000 for OS X)
xor rdi, rdi ; set exit status to 0 (`xor rdi, rdi` is equal to `mov rdi, 0` )
syscall ; call exit()
Windows
On Windows, a program can terminate itself by calling ExitProcess or RtlExitUserProcess function.
See also
* exit (command)
* Child process
* Execution
* Exit status
* Fork
* Kill command
* Orphan process
An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.
Unix-like
In a Unix-like operating system any orphaned process will be immediately adopted by an implementation-defin ...
* Process
* Parent process
* SIGCHLD
* Task
* Thread
Thread may refer to:
Objects
* Thread (yarn), a kind of thin yarn used for sewing
** Thread (unit of measurement), a cotton yarn measure
* Screw thread, a helical ridge on a cylindrical fastener
Arts and entertainment
* ''Thread'' (film), 2016 ...
* Wait
Wait or WAIT may refer to:
Music
* Wait (musician), British town pipers
Albums and EPs
* ''Wait'' (The Polyphonic Spree EP), by The Polyphonic Spree
* ''Wait'' (Emanuel Nice EP), a 2002 EP released by the band Emanuel Nice
* ''Wait'' (Stee ...
References
External links
* {{man, sh, exit, SUS, terminate a process
C++ reference for std::exit
Articles with example C code
C standard library
Process (computing)
POSIX
Process termination functions