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
- one uses
touch
and one usesmkfifo
to create a "file", which without knowing anything else, is just like using another command to do a similar thing, - most importantly,
echo
ing 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.
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? – ilkkachu Mar 31 '24 at 17:55