Exit (operating system)

From Wikipedia, the free encyclopedia

An exit, when applied to computing, is when a process terminates execution. 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.

Under Unix and Unix-like operating systems, every process is starting by a parent process, when the parent 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 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 operation system that the process has terminated and allows it to reclaim the resources used by the process.

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.

  • This does not apply to Microsoft Windows, for which a child process does not depend on its parent process.

[edit] Examples

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

C:

#include <stdlib.h>

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

or:

#include <stdlib.h>

int main(void)
{
    return EXIT_SUCCESS;
}

C++:

#include <cstdlib>

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

Microsoft DOS:

set ERRORLEVEL=0
exit

Perl:

#!/bin/perl
exit;

Python:

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

Unix shell:

#!/bin/sh
exit 0

[edit] See also