Exit (operating system)

Exit (operating system)

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.

How it works

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.

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

Orphans and zombies

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.

Examples

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:

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

DOS Batch file:set ERRORLEVEL=0exit

Perl:
#!/bin/perlexit;

Python:
#!/usr/bin/pythonraise SystemExit

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!

ee also

*Child process
*Execution
*Exit command
*Exit status
*Fork
*Kill command
*Orphan process
*Process
*Parent process
*SIGCHLD
*Task
*Thread
*Wait

External links

*


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Wait (operating system) — In modern computer operating systems, a process (or task) may wait on another process to complete its execution. In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait… …   Wikipedia

  • Fork (operating system) — In computing, when a process forks, it creates a copy of itself, which is called a child process. The original process is then called the parent process . More generally, a fork in a multithreading environment means that a thread of execution is… …   Wikipedia

  • Exec (operating system) — The exec functions of Unix like operating systems are a collection of functions that causes the running process to be completely replaced by the program passed as argument to the function. As a new process is not created, the process ID (PID)… …   Wikipedia

  • Real-time operating system — A real time operating system (RTOS; generally pronounced as are toss ) is a multitasking operating system intended for real time applications. Such applications include embedded systems (programmable thermostats, household appliance controllers,… …   Wikipedia

  • Exit (command) — exit is a command used in many operating system command line shells. Entering the command logs the user out of their current session, or closes the user s current console or terminal connection.References*man|cu|exit|SUS|cause the shell to exit… …   Wikipedia

  • Exit — or exit (lat. exit he/she leaves , pl. exeunt ) may refer to:*Exit, denoting a way out of a building, city, or place * Emergency exit, or fire exit, used in case of an emergency * Exit ramp, or slip road, used to leave an expressway or motorway * …   Wikipedia

  • Exit status — The exit status of a process in computer programming is an small number passed from a child process to a parent process when it is done executing a specific task delegated. On DOS, this may be referred to as an errorlevel.When software programs… …   Wikipedia

  • exit — /ˈɛgzət / (say egzuht), /ˈɛksət / (say eksuht) noun 1. a way or passage out. 2. a going out or away; a departure: to make one s exit. 3. the departure of a player from the stage as part of the action of a play. –verb (exited, exiting) –verb (i) 4 …  

  • System Call — Ein Systemaufruf, auch Systemcall (von engl. system call) oder kurz Syscall, ist in der Computertechnik eine von Anwendungsprogrammen benutzte Methode, um vom Betriebssystem bereitgestellte Funktionalitäten auszuführen. Inhaltsverzeichnis 1… …   Deutsch Wikipedia

  • System call — In computing, a system call is the mechanism used by an application program to request service from the kernel. Background A system call is a request made by any arbitrary program to the kernel for performing tasks picked from a predefined set… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”