.TH EXITS 2
.SH NAME
exits, _exits, atexit, atexitdont, terminate \- terminate process, process cleanup
.SH SYNOPSIS
.B #include <u.h>
.br
.B #include <libc.h>
.PP
.nf
.B
void	_exits(char *msg)
.B
void	exits(char *msg)
.PP
.B
int	atexit(void(*)(void))
.PP
.B
void	atexitdont(void(*)(void))
.PP
.B
/* Alef only */
.PP
.B
void	_exits(byte *msg)
.PP
.B
void	exits(byte *msg)
.PP
.B
void	terminate(byte *msg)
.fi
.SH DESCRIPTION
.I Exits
is the conventional way to terminate a process.
.I _Exits
is the underlying system call.
They
can never return.
.PP
.I Msg
conventionally includes a brief (maximum length
.BR ERRLEN )
explanation of the reason for
exiting, or a null pointer or empty string to indicate normal termination.
The string is passed to the parent process, prefixed by the name and process
id of the exiting process, when the parent does a
.IR wait (2).
.PP
Before calling
.I _exits
with
.I msg
as an argument,
.I exits
calls in reverse order all the functions
recorded by
.IR atexit .
.PP
.I Atexit
records
.I fn
as a function to be called by
.IR exits .
It returns zero if it failed,
nonzero otherwise.
A typical use is to register a cleanup routine for an I/O package.
To simplify programs that fork or share memory,
.I exits
only calls those
.IR atexit -registered
functions that were registered by the same
process as that calling
.IR exits .
.PP
Calling
.I atexit
twice (or more) with the same function argument causes
.I exits
to invoke the function twice (or more).
.PP
There is a limit to the number of exit functions
that will be recorded;
.I atexit
returns 0 if that limit has been reached.
.PP
.I Atexitdont
cancels a previous registration of an exit function.
.SH Alef
In Alef, the system call
.I _exits
is the same, but its use is discouraged because the run-time system
needs to maintain consistency;
.I terminate
and
.I exits
are the recommended routines.
.I Terminate
is called automatically when a
.B task
or
.B proc
returns from its main function; it may also be called explicitly.
In either case, it frees resources
private to the task (which may be the implicit
main task within the
.BR proc )
and terminates that task.  If that
.B task
is the last one in the
.BR proc ,
resources private to the
.B proc
are then freed.  If that
.B proc
is the last one in the program, it calls
.IR exits .
.I Exits
should only be called in the last
.B proc
of a program; it calls any
.I atexit
functions (registered by any
.BR proc )
and then calls
.IR _exits .
.PP
In Alef,
.I atexit
and
.I atexitdont
behave the same as in C.
.SH SOURCE
.B /sys/src/libc/port/atexit.c
.SH "SEE ALSO"
.IR fork (2),
.IR wait (2)
