[Gllug] Named pipes
Nix
nix at esperi.org.uk
Tue Dec 11 20:01:57 UTC 2007
On 11 Dec 2007, Richard Jones verbalised:
> On Tue, Dec 11, 2007 at 11:32:54AM +0000, Toby Waterford wrote:
>> I've worked out how to use unnamed pipes and I finding them very
>> useful. I may be being dim, but named pipes completely confuse me -
>> are they useful? Does anyone have any advice on getting to grips with
>> them?
>
> They're not really very useful. Very occasionally you might have,
> say, a shell script where you want to use a named pipe to communicate
> between two unrelated processes (unnamed pipes can only be created
> where two processes are related to each other through fork(2)). A
Pedant point: nearly, but not quite right. (You know the right answer but
I'm going to expand anyway.)
Unnamed pipes can only be created by a *single* process, using pipe(2),
which hands you back a pair of file descriptors, one for each end. Most
processes then proceed to hold on to one end in a parent process and
another in a child, but you could farm them out to two children, or even
send them over a Unix-domain socket to a completely unrelated process.
(I wrote a messaging-dispatch process at work which relies on doing
this, although it looks like it might have to start sending Unix-domain
and TCP sockets over those Unix-domain sockets as well. I've been trying
to avoid this because sending Unix-domain sockets over Unix-domain
sockets has a tendency to trip bugs in Unix kernels...)
> rather contrived example might be:
>
> mknod syslog-pipe p
> logger < syslog-pipe &
>
> [and in another shell script ...]
>
> echo "Log message" >> syslog-pipe
>
> Actually that example doesn't work too well because the 'logger'
> process exits after the first log message is sent.
I wrote a tiny `mp3d' script long ago:
,----
| #!/usr/bin/zsh
|
| FIFO=/dev/mp3
|
| [[ ! -p $FIFO ]] && echo "Error: $FIFO does not exist or is not a FIFO." && exit 1
|
| while true; do mpg123 -b 1024 - < $FIFO >/dev/null 2>/dev/null; done
`----
(It's now been obsoleted by MPD, which provides rather more control, but
it was useful for years.)
--
`The rest is a tale of post and counter-post.' --- Ian Rawlings
describes USENET
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list