Named Pipes
A named pipe (often called a FIFO) is a mechanism for setting up a conversation between unrelated processes on the same machine. The names in a “named” pipe exist in the filesystem, which is just a funny way to say that you can put a special file in the filesystem namespace that has another process behind it instead of a disk.[161] A FIFO is convenient when you want to connect a process to an unrelated one. When you open a FIFO, your process will block until there’s a process on the other end. So if a reader opens the FIFO first, it blocks until the writer shows up—and vice versa.
To create a named pipe, use the POSIX mkfifo
function—if you’re on a POSIX system, that is. On Microsoft systems,
you’ll instead want to look into the Win32::Pipe module, which, despite its possible appearance to the
contrary, creates named pipes. (Win32 users create anonymous pipes using
pipe just like the rest of
us.)
For example, let’s say you’d like to have your .signature file produce a different answer each time it’s read. Just make it a named pipe with a Perl program on the other end that spits out random quips. Now every time any program (like a mailer, newsreader, finger program, and so on) tries to read from that file, that program will connect to your program and read in a dynamic signature.
In the following example, we use the rarely seen –p file test operator
to determine whether anyone (or anything) has accidentally removed our
FIFO.[162] If they have, there’s no reason to try to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access