0

Take this:

  • in one terminal,
    touch myfile echo some text >> myfile # returns straight away 
  • in another terminal,
    cat myfile # prints "some text" 
  • now I can rm myfile

Now this:

  • in one terminal
    mkfifo mypipe echo some text > mypipe # does not return yet 
  • in another terminal
    cat mypipe # prints "some text" 
  • the echo in the first terminal returns
  • now I can rm myfifo

The only differences I see between the two scenarii are

  1. one uses touch and one uses mkfifo to create a "file", which without knowing anything else, is just like using another command to do a similar thing,
  2. most importantly, echoing to the pipe doens't return until the read operation returns.

I guess 2 is what makes named pipes special as compared to regular files.

But how are named pipes special?

I read they are for inter process communication, but I would argue that processes could also just use regular files. But if that was the case, pipes wouldn't exist, so I must be wrong.


Here's one very simple example that totally confuses me:

If in one terminal I do

mkfifo mypipe while true; do date > mypipe ; done 

it will keep writing (overwriting, as I understand) to the pipe, but a single read from the pipe produces more output than I'd expect, e.g. this

cat myfifo 

prints the same date more times, generally 4, but at times 5 or 6.

4
  • 2
    Sure, you could use regular files, but then the processes will need to coordinate clearing out processed data, or you'd be unnecessarily using up disk space. Commented Mar 31, 2024 at 17:29
  • Er, well, for one, consider what happens if you run cat myfile vs. cat mypipe again, with or without writing more into the file. Plus you already mentioned the thing about blocking on open. Asking how pipes differ from regular files seems to imply the presumption that they're mostly similar, which I don't think is necessarily a good way of thinking about it. How would you consider e.g. the difference between a regular file and a character device that represented a serial (UART) connection? Commented Mar 31, 2024 at 17:55
  • See also e.g. manpages.ubuntu.com/manpages/trusty/en/man7/pipe.7.html if you haven't already, and maybe the links in the "pipe" tag wiki at unix.stackexchange.com/tags/pipe/info Commented Mar 31, 2024 at 17:55
  • Not all files are files, as you thing of a file. In Unix a file is a thing with a file-name. Commented Apr 1, 2024 at 13:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.