[comp.unix.wizards] /dev/fd

panos@tigger.colorado.edu (Panos Tsirigotis) (04/10/89)

In article <16799@mimsy.UUCP> Chris Torek writes:
> The trend (as shown by /dev/fd/*, ......

What exactly is /dev/fd ?

Panos Tsirigotis


----------------------------------------------------
| Email address: panos@boulder.colorado.edu        |
----------------------------------------------------

chris@mimsy.UUCP (Chris Torek) (04/10/89)

In article <8019@boulder.Colorado.EDU> panos@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@mimsy.umd.edu	Path:	uunet!mimsy!chris

tar@ksuvax1.cis.ksu.edu (Tim Ramsey) (04/10/89)

In article <16817@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>In article <8019@boulder.Colorado.EDU> panos@tigger.colorado.edu writes:
>(Panos Tsirigotis) writes:
>>What exactly is /dev/fd ?

[ Chris explains ]

Where can I find source for this device driver?

(followups to comp.source.wanted)

--
Timothy Ramsey                               Kansas State University
BITNET:   tar@KSUVAX1              Dept. of Computing and Information Sciences
Internet: tar@ksuvax1.cis.ksu.edu          Office of Unplanned Reboots
UUCP:  ...!{rutgers,texbell}!ksuvax1!tar     Division of Lost Inodes

john@jwt.UUCP (John Temples) (04/13/89)

In article <16817@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>shuffle puts them back), then the ksh syntax
>
>	shuffle <(process1) <(process2)
>
>produces on standard output the combined output of the two processes,

I tried this (with diff, not shuffle) on two different kshs, and both barfed
on the syntax.  Is this only supported under certain versions of ksh or Unix?
-- 
John Temples - UUCP: {uiucuxc,hoptoad,petsd}!peora!rtmvax!bilver!jwt!john

allbery@ncoast.ORG (Brandon S. Allbery) (04/21/89)

As quoted from <257@jwt.UUCP> by john@jwt.UUCP (John Temples):
+---------------
| In article <16817@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
| >shuffle puts them back), then the ksh syntax
| >
| >	shuffle <(process1) <(process2)
| >
| >produces on standard output the combined output of the two processes,
| 
| I tried this (with diff, not shuffle) on two different kshs, and both barfed
| on the syntax.  Is this only supported under certain versions of ksh or Unix?
+---------------

The only ksh'es I've used require that Unix have the /dev/fd/n driver
installed to do this.  While it is possible to get this behavior by the use
of named pipes, ksh doesn't use them when /dev/fd/n doesn't exist.  (Perhaps
the most recent version, released a month or so ago if I remember correctly,
does -- but *I* don't have it and chances are that you don't either.)

++Brandon
-- 
Brandon S. Allbery, moderator of comp.sources.misc	     allbery@ncoast.org
uunet!hal.cwru.edu!ncoast!allbery		    ncoast!allbery@hal.cwru.edu
      Send comp.sources.misc submissions to comp-sources-misc@<backbone>
NCoast Public Access UN*X - (216) 781-6201, 300/1200/2400 baud, login: makeuser