Named Pipes (FIFOs)

A named pipe is a file that acts like a pipe. You put something into

the file, and it comes out the other end. Thus it’s called a FIFO,

or First-In-First-Out, because the first thing you put in the pipe

is the first thing to come out the other end.

If you write to a named pipe, the process that is writing to the

pipe doesn’t terminate until the information being written is read

from the pipe. If you read from a named pipe, the reading process

waits until there’s something to read before terminating. The size

of the pipe is always zero: It doesn’t store data, it just links two

processes like the shell |. However, because this pipe has a name,

the two processes don’t have to be on the same command line or even

be run by the same user.

You can try it by doing the following:

cd; mkfifo mypipe

Makes the pipe.

echo "hello" > mypipe &

Puts a process in the background that tries to write “hello” to the

pipe. Notice that the process doesn’t return from the background; it

is waiting for someone to read from the pipe.

cat mypipe

At this point, the echo process should return, because cat read from

the pipe, and the cat process will print hello.

rm mypipe

You can delete pipes just like any other file.

Debian GNU/Linux : Guide to Installation and Usage · 第 155 / 193 题