Exit (operating system)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

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.

Contents

Under Unix and Unix-like operating systems, every process is starting by a parent process when it 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 kernel and a SIGCHLD signal is sent to the parent process. The exit status can then be retrieved by the parent process.

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.

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 offers the _Exit() function, deriving from SVr4 _exit(), which terminates the current process "immediately". 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.

Some operating systems handle a child process whose parent process has terminated in a special manner. Such an orphan process 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.

The following programs do nothing but terminate and return a success status to the system.

C:

#include 

int main(void)
{
    exit(EXIT_SUCCESS);
}

or:

#include 

int main(void)
{
    return EXIT_SUCCESS;
}

C++:

#include 

int main(void)
{
    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.

Java:

import java.lang.System;

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

DOS Batch file:

set ERRORLEVEL=0
exit

Perl:

#!/bin/perl
exit;

Python:

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

Unix shell:

exit 0

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!

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.