/dev/fd

Chris Torek chris at mimsy.UUCP
Mon Apr 10 16:15:52 AEST 1989


In article <8019 at boulder.Colorado.EDU> panos at tigger.colorado.edu
(Panos Tsirigotis) writes:
>What exactly is /dev/fd ?

The idea behind /dev/fd, like that behind fork(), is astonishingly simple,
and sometimes seems as hard for students to grasp :-) .

Opening /dev/fd/n, for n in [0..getdtablesize()), causes the program
doing the open to behave as if it had instead done a dup(n).  Hence if
you want the `cat' program to read file `header', then its standard
input, then the file `trailer', instead of writing

	cat header - trailer

one simply writes

	cat header /dev/stdin trailer

(/dev/stdin is an alias for /dev/fd/0).  The special case code in `cat'
for reading stdin if the file named is `-' goes away; instead of the
program doing the sequence

	fd = open("header");		// produces 3 (BSD) or 4 (V8)
	read ...
	if (fd != 0) (void)close(fd);

	fd = 0;				// because of "-" special case
	read ...
	if (fd != 0) (void)close(fd);

	fd = open("trailer");
	read ...
	if (fd != 0) (void)close(fd);

it simply does

	fd = open("header");		// produces 3 or 4 as usual
	read ...
	(void)close(fd);

	fd = open("/dev/stdin");	// produces 3 or 4 as usual
	read ...
	(void)close(fd);

	fd = open("trailer");
	read ...
	(void)close(fd);

While `/dev/stdin' is a bit longer to type than `-', it has the advantage
of working with `naive' programs, programs that do not already have a
special case, or in which you would rather not *write* special cases
(the latter being applicable to everything!).

With a bit of help from the shell, the /dev/fd devices also allow
programs to read output streams from multiple programs.  Given, e.g.,
the `shuffle' program, which, when run as

	shuffle foo bar

produces

	line 1 of foo
	line 1 of bar
	line 2 of foo
	line 2 of bar
	line 3 of foo
	line 3 of bar
		.
		.
		.


(`shuffle' is one of a pair of programs, the other being `deal':
deal `deals' the lines of a single file to multiple files, and
shuffle puts them back), then the ksh syntax

	shuffle <(process1) <(process2)

produces on standard output the combined output of the two processes,
alternating lines as in the example above with `foo' and `bar'.  This
example is somewhat contrived; but the applications to parallel
processing seem obvious.  (ksh is already able to do this sort of
thing, using /dev/fd or, I guess, named pipes---/dev/fd is simply a
nice way of naming pipes after-the-fact.  Now if only we had ksh :-/ ...)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list