Re: IDEA: Process-Special files

Marnix Coppens (maco@telindus.be)
Tue, 20 Oct 1998 09:42:59 +0200


At 13:58 19/10/98 -0500, you wrote:
>
>This is a file that, when open()ed for read (O_RDONLY, O_RDWR, etc) would
>be executed as a binary or script, and the output would be substituted for
>the file.
>

There's a nice trick with named pipes that is sometimes used
to dynamically generate .plan files (whose contents are retrieved
when you finger someone).

I believe it goes like this:
1) mkfifo .specialfile
2) while true; do echo "This could be anything" > .specialfile; done
3) (someone else) cat .specialfile
-> This could be anything

Since no one has the pipe open for reading, the echo process will
be suspended until a reader process comes along. At that point, the
echo process will be awakened, thereby putting some contents in the
pipe. When the reader process finishes, the next echo process will be
launched and suspended again.

Of course, the single-liner 2) can be replaced by a more sophisticated
beast (such as a perl-script or whatever). It also depends on atomic
behaviour of pipe reading and writing, but you get the picture.
What happens when the second process writes to the file, or when there
are multiple readers is left as an exercise :-)
Check Stevens' APUE for more.

Cheers,

Marnix Coppens

---
Reality is that which                   | Artificial Intelligence
when you stop believing                 | stands no chance against
in it doesn't go away. (Philip K. Dick) | Natural Stupidity.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/