LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What is meaning that a file is a named pipe (FIFO)? (https://www.linuxquestions.org/questions/linux-newbie-8/what-is-meaning-that-a-file-is-a-named-pipe-fifo-891250/)

mei0fei 07-12-2011 04:02 AM

What is meaning that a file is a named pipe (FIFO)?
 
What is a pipe? and why a file is a named pipe? I am just learning linux.

b0uncer 07-12-2011 05:45 AM

See, for example, this Wikipedia page for general information. FIFO stands for "first in, first out", meaning that it's like a pipe: you put in something from one end and you get it out of the other end in the order the contents were put in. An example: open two terminals. From either one, create a fifo (for example in /tmp):
Code:

mkfifo /tmp/testfifo
Now prepare to write data to the fifo in one terminal:
Code:

cat > /tmp/testfifo
Then prepare to read data from the fifo in the other terminal:
Code:

cat /tmp/testfifo
After this, type some lines of text into the first terminal (end lines with Enter), and you'll see them come out from the other terminal reading the fifo. When you're finished, remove the fifo with
Code:

rm /tmp/testfifo
If you write some data to the fifo first (say a few lines) and after that read it (i.e., your reading process is not "on" all the time the writing process is, but is started afterwards), all the (so far) written data is read, so it feels like you were working with ordinary files in that sense. And you're not limited to just playing with two terminals: the point is, you can make programs read and write data to and from the fifo, meaning you can share data between programs using a fifo. Of course you could make a program read the output of another program directly, but in some cases you might want to use a different route instead (fifo or lifo--first in, last out--, for example).

Edit: to be a bit more clear, if process A writes to a fifo (or lifo, depending on which order is required) and process B reads from that fifo (or lifo), and it happens so that it cannot be guaranteed that A is ready to write whenever B is ready to read, or vice versa, then the fifo allows storing information (in order) such that A may write to it when it can, and B may read from it when it can. For example if A produces a lot of data, but B can only handle a bunch at a time, then B can read the data piece by piece from the fifo (or lifo) and in principle not care how much A may have written to it, and on the other hand A can write to the fifo (or lifo) without having to care if B is ready to read the fifo just then.

mei0fei 07-12-2011 06:48 AM

Thanks a lot. I opened two terminals and they worked just like you said.


All times are GMT -5. The time now is 04:42 AM.