return() vs. exit() vs. _exit()

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Sun Jan 26 13:56:59 AEST 1986

> I'm on a binary only 3b2/300 running SV.2.2 so...
> What's the difference between leaving main by return() vs exit() vs _exit()?
> I mean in reality, by proposed standards (X3J11 can you hear me?), and
> functionally (like on my machine).  Who closes file descriptors?  Who reclaims
> memory?  What about shared memory?  What is a gate 4, 8?

There is no difference between reality and the proposed standards.

If main() is left via a return statement, the effect is exactly
the same as termination via invocation of exit() with the return
value used as the parameter to exit().  exit() is required to
flush STDIO output buffers and do any other required cleanup.
_exit() directly terminates the process without performing any
extra actions along the way.  _exit() is not available in all C
implementations, although it is required on UNIX systems.

"File descriptor" is a UNIX-only concept.  Open file descriptors
are closed by the UNIX kernel when a process terminates.  The
kernel is also responsible for memory allocation and sharing.

GATE is similar to a subroutine jump except it switches into the
kernel (acquires new PC & PSW), vectored through tables indexed by
R0 and R1.  This constitutes a "service call" or "system call".
_exit() is normally implemented as a small piece of assembly-
language code that simply invokes the appropriate system call,
perhaps "gate 4,8".  exit() normally calls _cleanup(), perhaps
runs registered onexit handlers, then invokes _exit().

More information about the Comp.lang.c mailing list