ArticleslgStudy

computer science

Exit status

Exit status is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Exit status rather than just read about it. In short: In computing, the exit status (also exit code or exit value) of a terminated process is an integer number that is made available to its parent process (or caller). In DOS, this may be referred to as an errorlevel.

Key takeaways

  • Exit status belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Exit status to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Exit status from memory before moving on to harder problems.

Reference excerpt

In computing, the exit status (also exit code or exit value) of a terminated process is an integer number that is made available to its parent process (or caller). In DOS, this may be referred to as an errorlevel. When computer programs are executed, the operating system creates an abstract entity called a process in which the book-keeping for that program is maintained. In multitasking operating systems such as Unix or Linux, new processes can be created by active processes. The process that spawns another is called a parent process, while those created are child processes. Child processes run concurrently with the parent process. The technique of spawning child processes is used to delegate some work to a child process when there is no reason to stop the execution of the parent. When the child finishes executing, it exits by calling the exit system call. This system call facilitates passing the exit status code back to the parent, which can retrieve this value using the wait system call.

Semantics The parent and the child can have an understanding about the meaning of the exit statuses. For example, it is common programming practice for a child process to return (exit with) zero to the parent signifying success. Apart from this return value from the child, other information like how the process exited, either normally or by a signal may also be available to the parent process. The specific set of codes returned is unique to the program that sets it. Typically it indicates success or failure. The value of the code returned by the function or program may indicate a specific cause of failure. On many systems, the higher the value, the more severe the cause of the error. Alternatively, each bit may indicate a different condition, with these being evaluated by the or operator together to give the final value; for example, fsck does this. Sometimes, if the codes are designed with this purpose in mind, they can be used directly as a branch index upon return to the initiating program to avoid additional tests.

AmigaOS In AmigaOS, MorphOS and AROS, four levels are defined:

OK 0 WARN 5 ERROR 10 FAILURE 20

Shell and scripts Shell scripts typically execute commands and capture their exit statuses. For the shell's purposes, a command which exits with a zero exit status has succeeded. A nonzero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command is terminated by a signal whose number is N, a shell sets the variable $? to a value greater than 128. Most shells use 128+N, while ksh93 uses 256+N. If a command is not found, the shell should return a status of 127. If a command is found but is not executable, the return status should be 126. Note that this is not the case for all shells. If a command fails because of an error during expansion or redirection, the exit status is greater than zero.

C and C++ The C and C++ programming languages allow programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESS and EXIT_FAILURE. On Unix-like systems these are equal to 0 and 1 respectively. A C program may also use the exit() function specifying the integer status or exit macro as the first parameter, which halts the program. The return value from main is passed to the exit function, which for values zero, EXIT_SUCCESS or EXIT_FAILURE may translate it to "an implementation defined form" of successful termination or unsuccessful termination. Apart from 0 and the macros EXIT_SUCCESS and EXIT_FAILURE, the C standard does not define the meaning of return codes. Rules for the use of return codes vary on different platforms (see the platform-specific sections).

C# In C#, the Main() method typically does not return a type (its type is void, or if async, it is System.Threading.Tasks.Task). However, Main() may indicate a return type, of type int, which is returned to the operating system. If the Main() method is declared async, then it instead returns System.Threading.Tasks.Task<int>.

DOS In DOS terminology, an errorlevel is an integer exit code returned by an executable program or subroutine. Errorlevels typically range from 0 to 255. In DOS there are only 256 error codes available, but DR DOS 6.0 and higher support 16-bit error codes at least in CONFIG.SYS. With 4DOS and DR-DOS COMMAND.COM, exit codes (in batchjobs) can be set by EXIT n and (in CONFIG.SYS) through ERROR=n. Exit statuses are often captured by batch programs through IF ERRORLEVEL commands. Multiuser DOS supports a reserved environment variable %ERRORLVL% which gets automatically updated on return from applications. COMMAND.COM under DR-DOS 7.02 and higher supports a similar pseudo-environment variable %ERRORLVL% as well as %ERRORLEVEL%. In CONFIG.SYS, DR DOS 6.0 and higher supports ONERROR to test the load status and return code of device drivers and the exit code of programs.

Java In Java, the main() does not indicate an exit code, so if needed, an exit code is manually specified through java.lang.System::exit(), which terminates the program and the JVM. Any method may call System.exit(), unless a security manager does not permit it. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

OpenVMS In OpenVMS, success is indicated by odd values and failure by even values. The value is a 32-bit integer with sub-fields: control bits, facility number, message number and severity. Severity values are divided between success (Success, Informational) and failure (Warning, Error, Fatal).

Plan 9 In Plan 9's C, exit status is indicated by a string passed to the exits() function, and function main() is type void.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Exit status

Start with the simplest possible case. Write down what Exit status claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Exit status before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Exit status ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Exit status

In research
Exit status appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Exit status in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Exit status is common in secondary-school and first-year university syllabi. It links to neighbouring topics Process (computing), so understanding it makes those chapters shorter.
In everyday life
Look for Exit status outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Exit status” →

How to study Exit status in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Exit status means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Exit status out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Exit status in simple terms?

In computing, the exit status (also exit code or exit value) of a terminated process is an integer number that is made available to its parent process (or caller). In DOS, this may be referred to as an errorlevel.

Why does Exit status matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Exit status?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Exit status.

Tags

  • Process (computing)

Keep exploring