Product Code Database
Example Keywords: playbook -playbook $6
   » » Wiki: Exit (system Call)
Tag Wiki 'Exit (system Call)'.
Tag

On many computer operating systems, a terminates its execution by making an exit . More generally, an exit in a multithreading environment means that a thread of execution has stopped running. For resource management, the reclaims resources (, , etc.) that were used by the process. The process is said to be a dead process after it terminates.


How it works
Under and operating systems, a process is started when its executes a fork . The parent process may then wait for the to terminate, or may continue execution (possibly forking off other child processes). When the child process terminates ("dies"), either normally by calling exit, or due to a or signal (e.g., , SIGINT, ), an is returned to the operating system and a 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 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 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., ) 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 allow user 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 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 becomes a child of a special , which then waits for the child process to terminate. Likewise, a similar strategy is used to deal with a , 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 in a consistent state.


Examples
The following programs terminate and return a success to the system.
C: {
  1. include

int main(void) {

   exit(EXIT_SUCCESS); // or return EXIT_SUCCESS
     
}
|C++:

  1. include

int main() {

   std::exit(EXIT_SUCCESS); // or return EXIT_SUCCESS
     
} |} :

IDENTIFICATION DIVISION.
PROGRAM-ID. SUCCESS-PROGRAM.
     

PROCEDURE DIVISION.
MAIN.
    MOVE ZERO TO RETURN-CODE.
END PROGRAM.
     

Java:

public class Success {

   public static void main(String[] args)
   {
       System.exit(0);
   }
     
}

: exit 0

:

  1. !/bin/perl
exit;

:

Python:

  1. !/usr/bin/python
import sys sys.exit(0)

: exit 0

Pascal: program pr1; begin

exit(0);
     
end;

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''
     

32-bit x86 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]]''
.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]]''
                   ''# [http://www.linfo.org/int_0x80.html invokes system call]—in this case system call''
                   ''# number 1 with argument 0''
     

64-bit x86 64 Assembly: for

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
     


Windows
On Windows, a program can terminate itself by calling ExitProcess or RtlExitUserProcess function.


See also


External links

Page 1 of 1
1
Page 1 of 1
1

Account

Social:
Pages:  ..   .. 
Items:  .. 

Navigation

General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 

Statistics

Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs
1s Time