/dev/null

Joseph S. D. Yao jsdy at hadron.UUCP
Thu Nov 28 13:48:00 AEST 1985


In article <808 at dcl-cs.UUCP> stephen at comp.lancs.ac.uk (Stephen J. Muir) writes:
>write (filedes, buffer, nbytes)
>	char	*buffer
>	{ if (major_minor (filedes) == that_of ("/dev/null"))
>		return (nbytes);
>	  else
>		go_do_the_REAL_write (filedes, buffer, nbytes);
>	}

I don't know whether to hope that this is a bad joke, or to hope
that no-one would on purpose perpetrate this kind of misinformation.

The write system call  i s  a system call: the arguments get passed
down to the kernel.
The kernel checks various parameters, and especially what type of
file is being written to.  (At this point, we have common code for
read and write, with a flag saying what is being done.)  Since
/dev/{kmem,null,mem,Ukmem} are char devices, the kernel looks in
the character device driver table, and indexes off the major
device number to find which driver to use.
The driver write routine for this particular major device number
does the following things:
	If minor == KMEM, then
		while (u.u_count > 0)
			c = getc();
			put c at *u.u_offset++ in such a way
			that if there is a seg violation, the
			error gets returned to the user instead
			of causing a system crash.
	else if minor == MEM, then
		map *u.u_offset into space accessible via suword()
		and subyte().
		while (u.u_count > 0)
			c = getc();
			if (subyte(c, u.u_offset++) < 0)
				return error;
	else if minor == NULL, then
		just set u.u_count to 0.  that's it.  nothing else.
		that pretends that everything got writ.
	else ...

Some versions of UNIX may do this more elegantly/efficiently, e.g.
with copyout() rather than subyte(); but this is the original,
common idea.  It is   NOT   handled at user level.
-- 

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



More information about the Comp.unix.wizards mailing list