[net.unix] return

rich@rexago1.UUCP (K. Richard Magill) (01/24/86)

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?

K. Richard Magill

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (01/26/86)

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

brooks@lll-crg.ARpA (Eugene D. Brooks III) (01/26/86)

In article <178@rexago1.UUCP> rich@rexago1.UUCP (K. Richard Magill) writes:
>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()?
return(i) from main is the same as exit(i), exit(i) flushes all the open file
descriptors and then calls _exit(i).  _exit(i) crashes and burns.

jsdy@hadron.UUCP (Joseph S. D. Yao) (01/26/86)

In article <178@rexago1.UUCP> rich@rexago1.UUCP (K. Richard Magill) writes:
>What's the difference between leaving main by return() vs exit() vs _exit()?

Functionally, return and exit should be identical.  Your 'lint'
(s5r2.0v2) prob'ly complains if you use exit() tho.  The code goes
roughly like:
[C header, crt0.s:]
	push	envp
	push	argv
	push	argc
	call	$3, _main
	push	r0
	call	$1, $_exit
	sys	$exit_system_call
	halt
Note that exit(main(argc, argv, envp)) is the effect.

The exit() code resembles:
exit(val)
  int val;
{
	register FILE *fp;

	for (fp = _iob; fp < &_iob[NIOBUFS]; fp++)
		if fp has been opened
			fclose(fp);
	_exit(val);
}
..........
__exit:
	move	arg to r0
	sys	$exit_system_call

Shared and local memory are reclaimed by the kernel after an exit()
system call.

>                               ...  What is a gate 4, 8?

Got my curiosity piqued.
-- 

	Joe Yao		hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}