Search code examples
linuxunixcattailfifo

Reading FIFO doesn't show for the first time


In Unix, I've made a FIFO and I tried to read it with tail:

mkfifo fifo.file
tail -f fifo.file

Then I try to write messages into it from another process so I do as below:

cat > fifo.file

Then I type messages such as:

abc
def

Before I type Ctrl-D, nothing is printed at the first process (tail -f fifo.file).
Then I type Ctrl-D, the two lines above are printed.

Now If I do cat > fifo.file again and I type one line such as qwe and type Enter at the end of line, this string will be printed immediately at the first process.

I'm wondering why I get two different behaviors with the same command.
Is it possible to make it the second behavior without the first, meaning that when I cat the first time, I can see messages printed once I type Enter, instead of Ctrl-D?


Solution

  • This is just how tail works. Basically it outputs the specified file contents only when EOF occurs which Ctrl-D effectively sends to the terminal. And the -f switch just makes tail not exit and continue reading when that happens.

    Meaning no matter the switches tail still needs EOF to output anything at all.

    Just to test this you can use simple cat instead of tail:

    term_1$ mkfifo fifo.file
    term_1$ cat < fifo.file
    
    ...
    
    term_2$ cat > fifo.file