Program termination: Difference between revisions

From Rosetta Code
Content added Content deleted
(added J)
(add request for explanation of cleanup behavior (as discussed on talk page), mark every example for review)
Line 1: Line 1:
{{task}}Show the syntax for a complete stoppage of a program inside a [[Conditional Structures|conditional]]. This includes all [[threads]]/[[processes]] running under your program.
{{task}}Show the syntax for a complete stoppage of a program inside a [[Conditional Structures|conditional]]. This includes all [[threads]]/[[processes]] which are part of your program.

Explain the cleanup (or lack thereof) caused by the termination (allocated memory, database connections, open files, object finalizers/destructors, run-on-exit hooks, etc.).


=={{header|Ada}}==
=={{header|Ada}}==
{{example-needs-review|Ada}}
Ada programs execute in one or more tasks. All tasks created during the execution of a program depend in a hierarchical manner on the task that create them, except for the environment task which executes the "main" procedure for the program. Each task will abort (terminate abnormally) if the task upon which it depends is aborted. This approach to task termination is not recommended because it does not allow tasks to terminate in a known state.
Ada programs execute in one or more tasks. All tasks created during the execution of a program depend in a hierarchical manner on the task that create them, except for the environment task which executes the "main" procedure for the program. Each task will abort (terminate abnormally) if the task upon which it depends is aborted. This approach to task termination is not recommended because it does not allow tasks to terminate in a known state.


Line 36: Line 39:


=={{header|BASIC}}==
=={{header|BASIC}}==
{{example-needs-review|BASIC}}
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<qbasic>if problem = 1 then
<qbasic>if problem = 1 then
Line 42: Line 46:


=={{header|C}}==
=={{header|C}}==
{{example-needs-review|C}}
<c>#include <stdlib.h>
<c>#include <stdlib.h>


Line 50: Line 55:


=={{header|E}}==
=={{header|E}}==
{{example-needs-review|E}}


Exit indicating successful completion:
Exit indicating successful completion:
Line 64: Line 70:


=={{header|Forth}}==
=={{header|Forth}}==
{{example-needs-review|Forth}}
debug @
debug @
if QUIT \ quit back to the interpreter
if QUIT \ quit back to the interpreter
Line 70: Line 77:


=={{header|Fortran}}==
=={{header|Fortran}}==
{{example-needs-review|Fortran}}
IF (condition) STOP [message]
IF (condition) STOP [message]
! message is optional and is a character string.
! message is optional and is a character string.
Line 75: Line 83:


=={{header|Haskell}}==
=={{header|Haskell}}==
{{example-needs-review|Haskell}}
import Control.Monad
import Control.Monad
import System.Exit
import System.Exit
Line 84: Line 93:


=={{header|J}}==
=={{header|J}}==
{{example-needs-review|J}}
Given <tt>condition</tt>, an integer which is zero if everything's OK (and we should NOT exit), or a non-zero exit code if there's a problem (and we should exit), then:
Given <tt>condition</tt>, an integer which is zero if everything's OK (and we should NOT exit), or a non-zero exit code if there's a problem (and we should exit), then:


Line 93: Line 103:


=={{header|Java}}==
=={{header|Java}}==
{{example-needs-review|Java}}
<java>if(problem){
<java>if(problem){
System.exit(integerErrorCode); //conventionally, error code 0 is the code for "OK", while anything else is an actual problem
System.exit(integerErrorCode); //conventionally, error code 0 is the code for "OK", while anything else is an actual problem
Line 99: Line 110:


=={{header|Logo}}==
=={{header|Logo}}==
{{example-needs-review|Logo}}
{{works with|UCB Logo}}
{{works with|UCB Logo}}
bye ; exits to shell
bye ; exits to shell
Line 108: Line 120:


=={{header|OCaml}}==
=={{header|OCaml}}==
{{example-needs-review|OCaml}}
<ocaml>if problem then
<ocaml>if problem then
exit integerErrorCode (*conventionally, error code 0 is the code for "OK",while anything else is an actual problem*)</ocaml>
exit integerErrorCode (*conventionally, error code 0 is the code for "OK",while anything else is an actual problem*)</ocaml>


=={{header|Perl}}==
=={{header|Perl}}==
{{example-needs-review|Perl}}
<perl>if ($problem) {
<perl>if ($problem) {
exit integerErrorCode; #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case)
exit integerErrorCode; #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case)
Line 118: Line 132:


=={{header|Pop11}}==
=={{header|Pop11}}==
{{example-needs-review|Pop11}}


<pre>
<pre>
Line 126: Line 141:


=={{header|Python}}==
=={{header|Python}}==
{{example-needs-review|Python}}
<python>if problem:
<python>if problem:
sys.exit(integerErrorCode) #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case)
sys.exit(integerErrorCode) #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case)

Revision as of 15:45, 3 August 2008

Task
Program termination
You are encouraged to solve this task according to the task description, using any language you may know.

Show the syntax for a complete stoppage of a program inside a conditional. This includes all threads/processes which are part of your program.

Explain the cleanup (or lack thereof) caused by the termination (allocated memory, database connections, open files, object finalizers/destructors, run-on-exit hooks, etc.).

Ada

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

Ada programs execute in one or more tasks. All tasks created during the execution of a program depend in a hierarchical manner on the task that create them, except for the environment task which executes the "main" procedure for the program. Each task will abort (terminate abnormally) if the task upon which it depends is aborted. This approach to task termination is not recommended because it does not allow tasks to terminate in a known state.

However, this Rosetta Code task requires a simple stoppage of the program including all tasks. The simple way to achieve this is to abort the environment task.

<ada> with Ada.Task_Identification;

procedure Main is

  -- Create as many task objects as your program needs

begin

  -- whatever logic is required in your Main procedure
  if some_condition then
     Ada.Task_Identification.Abort_Task (Ada.Task_Identification.Current_Task);
  end if;

end Main; </ada>

A more correct approach to such termination is to provide an entry in each task created by the environment task which, when called by the task upon which it depends, causes the called task to terminate in a known state.

<ada> procedure Main is

  -- Create as many task objects as your program needs

begin

  -- whatever logic is required in your Main procedure
  if some_condition then
     -- for each task created by the Main procedure
     The_task.Stop;
     -- end the Main procedure
     return;  -- actually, this is not needed
  end if;

end Main; </ada>

BASIC

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.
Works with: QuickBasic version 4.5

<qbasic>if problem = 1 then

  end

end if</qbasic>

C

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

<c>#include <stdlib.h>

if(problem){

 exit(integerErrorCode); /*conventionally, error code 0 is the code for "OK", while anything else is an actual problem*/
 /*optionally: return the integerErrorCode from the main() function*/

}</c>

E

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

Exit indicating successful completion:

if (true) {
    interp.exitAtTop()
}

Exit indicating some problem:

if (true) {
    interp.exitAtTop("because the task said so")
}

Forth

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.
debug @
if   QUIT  \ quit back to the interpreter
else BYE   \ exit forth environment completely (e.g. end of a Forth shell script)
then

Fortran

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.
IF (condition) STOP [message] 
! message is optional and is a character string.
! If present, the message is output to the standard output device.

Haskell

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.
import Control.Monad
import System.Exit

when problem do
    exitWith ExitSuccess                    (* success *)
    exitWith (ExitFailure integerErrorCode) (* some failure with code *)
    exitFailure                             (* generic failure *)

J

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

Given condition, an integer which is zero if everything's OK (and we should NOT exit), or a non-zero exit code if there's a problem (and we should exit), then:

Tacit version:

  2!:55^:] condition

Explicit version:

  3 : 'if. 0~: condition do. 2!:55 condition end.'

Java

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

<java>if(problem){

  System.exit(integerErrorCode); //conventionally, error code 0 is the code for "OK", while anything else is an actual problem
  //optionally: Runtime.getRuntime().exit(integerErrorCode);

}</java>

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.
Works with: UCB Logo
bye   ; exits to shell
throw "toplevel  ; exits to interactive prompt
pause      ; escapes to interactive prompt for debugging
continue   ; resumes after a PAUSE

OCaml

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

<ocaml>if problem then

   exit integerErrorCode (*conventionally, error code 0 is the code for "OK",while anything else is an actual problem*)</ocaml>

Perl

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

<perl>if ($problem) {

   exit integerErrorCode; #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case)
                          #while anything else is an actual problem

}</perl>

Pop11

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.
if condition then
    sysexit();
endif;

Python

This example may be incorrect due to a recent change in the task requirements or a lack of testing. Please verify it and remove this message. If the example does not match the requirements or does not work, replace this message with Template:incorrect or fix the code yourself.

<python>if problem:

   sys.exit(integerErrorCode) #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case)
                              #while anything else is an actual problem</python>