Exit (system call)
   HOME

TheInfoList



OR:

On many computer
operating systems An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs. Time-sharing operating systems schedule tasks for efficient use of the system and may also in ...
, a computer process terminates its
execution Capital punishment, also known as the death penalty, is the state-sanctioned practice of deliberately killing a person as a punishment for an actual or supposed crime, usually following an authorized, rule-governed process to conclude that ...
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 of execution has stopped running. For
resource management In organizational studies, resource management is the efficient and effective development of an organization's resources when they are needed. Such resources may include the financial resources, inventory, human skills, production resources, or ...
, 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 Resource refers to all the materials available in our environment which are technologically accessible, economically feasible and culturally sustainable and help us to satisfy our needs and wants. Resources can broadly be classified upon their av ...
(
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 remember ...
, files, 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, ...
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 An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs. Time-sharing operating systems schedule tasks for efficient use of the system and may also in ...
, a process is started when its parent process executes a ''
fork In cutlery or kitchenware, a fork (from la, furca 'pitchfork') is a utensil, now usually made of metal, whose long handle terminates in a head that branches into several narrow and often slightly curved tine (structural), tines with which one ...
''
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 A child process in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask. There are two major procedure ...
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 In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The '' IEEE Transactions on Signal Processing' ...
(e.g., SIGTERM,
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 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 ...
), an
exit status The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred ...
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 The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred ...
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 Plan 9 from Bell Labs is a distributed operating system which originated from the Computing Science Research Center (CSRC) at Bell Labs in the mid-1980s and built on UNIX concepts first developed there in the late 1960s. Since 2000, Plan 9 has be ...
) allow a
character string In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation). ...
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 z/OS is a 64-bit operating system for IBM z/Architecture mainframes, introduced by IBM in October 2000. It derives from and is the successor to OS/390, which in turn was preceded by a string of MVS versions.Starting with the earliest: * ...
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 programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
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 Exec or EXEC may refer to: * Executive officer, a person responsible for running an organization * Executive producer, provides finance and guidance for the making of a commercial entertainment product * A family of kit helicopters produced by Rot ...
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-defined s ...
'' 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 The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred ...
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 u ...
: 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 Pascal, Pascal's or PASCAL may refer to: People and fictional characters * Pascal (given name), including a list of people with the name * Pascal (surname), including a list of people and fictional characters with the name ** Blaise Pascal, Frenc ...
: program wiki; begin ExitCode := 0; exit end.
DR-DOS DR-DOS (written as DR DOS, without a hyphen, in versions up to and including 6.0) is a disk operating system for IBM PC compatibles. Upon its introduction in 1988, it was the first DOS attempting to be compatible with IBM PC DOS and MS- ...
batch file Batch may refer to: Food and drink * Batch (alcohol), an alcoholic fruit beverage * Batch loaf, a type of bread popular in Ireland * A dialect term for a bread roll used in North Warwickshire, Nuneaton and Coventry, as well as on the Wirra ...
: exit 0
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
: #!/usr/bin/env perl exit; PHP: 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 syste ...
: exit 0
DOS DOS is shorthand for the MS-DOS and IBM PC DOS family of operating systems. DOS may also refer to: Computing * Data over signalling (DoS), multiplexing data onto a signalling channel * Denial-of-service attack (DoS), an attack on a communicat ...
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 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 subfunctio ...
''; 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, whi ...
32-bit x86 Assembly: ; For NASM MOV AL, 1 ; Function 1: exit() MOV EBX, 0 ; Return code INT 80h ; ''# Passes control to
interrupt vector An interrupt vector table (IVT) is a data structure that associates a list of interrupt handlers with a list of interrupt requests in a table of interrupt vectors. Each entry of the interrupt vector table, called an interrupt vector, is the addre ...
'' ''
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 The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred ...
0'' int $0x80 ''# Passes control to
interrupt vector An interrupt vector table (IVT) is a data structure that associates a list of interrupt handlers with a list of interrupt requests in a table of interrupt vectors. Each entry of the interrupt vector table, called an interrupt vector, is the addre ...
'' ''
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, whi ...
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 ab ...
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) In computing, 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 ...
*
Child process A child process in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask. There are two major procedure ...
*
Execution Capital punishment, also known as the death penalty, is the state-sanctioned practice of deliberately killing a person as a punishment for an actual or supposed crime, usually following an authorized, rule-governed process to conclude that ...
*
Exit status The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred ...
*
Fork In cutlery or kitchenware, a fork (from la, furca 'pitchfork') is a utensil, now usually made of metal, whose long handle terminates in a head that branches into several narrow and often slightly curved tine (structural), tines with which one ...
*
Kill command ''Kill Command'' (also known as ''Identify'') is a 2016 British science fiction action horror film written and directed by Steven Gomez, and starring Thure Lindhardt and Vanessa Kirby. The story focuses on a group of U.S. Marines attempting to ...
*
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-defined s ...
* Process * Parent process * SIGCHLD * Task * Thread *
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