exit (system call)

Source: Wikipedia, the free encyclopedia.

On many computer

dead process
after it terminates.

How it works

Under

wait
system call.

Most operating systems allow the terminating process to provide a specific

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

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;

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

process table
in a consistent state.

Examples

The following programs terminate and return a success exit status to the system.

C:
#include <stdlib.h>

int main(void)
{
    exit(EXIT_SUCCESS); /* or return EXIT_SUCCESS */
}
C++:
#include <cstdlib>

int main()
{
    std::exit(EXIT_SUCCESS); // or return EXIT_SUCCESS
}

COBOL:

 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:

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

Pascal:

program wiki;
begin
        ExitCode := 0;
        exit
end.

DR-DOS batch file:[1]

exit 0

Perl:

#!/usr/bin/env perl
exit;

PHP:

<?php
exit(0);

Python:

#!/usr/bin/env python3
import sys
sys.exit(0)

Unix shell:

exit 0

DOS assembly:

; For 
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 32-bit x86 Assembly:

; For NASM
MOV AL, 1 ; Function 1: exit()
MOV EBX, 0 ; Return code
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

x86 64 Assembly: for FASM

 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

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

References

  1. OpenDOS 7.01, including the description of many undocumented features and internals. It is part of the author's yet larger MPDOSTIP.ZIP collection maintained up to 2001 and distributed on many sites at the time. The provided link points to a HTML-converted older version of the file.) [1]

External links