I just started learning how Everything Is A FileTM on Linux, which made me wonder what would happen if I literally read from /dev/stdout:
$ cat /dev/stdout
^C
$ tail /dev/stdout
^C
(The ^C
is me killing the program after it hangs).
When I try with vim
, I get the unthinkable message: "/dev/stdout" is not a file. Gasp!
So what gives, why am I getting hangups or error messages when I try to read these "files"?
Best Answer
You aren't getting "hangups" from
cat(1)
andtail(1)
, they're just blocking on read.cat(1)
waits for input, and prints it as soon as it sees a complete line:Here I typed
foo
Enterbar
EnterCTRL-D.tail(1)
waits for input, and prints it only when it can detectEOF
:Here I typed again
foo
Enterbar
EnterCTRL-D.Vim is the only one that gives you an error. It does that because it runs
stat(2)
against/dev/stdout
, and it finds it doesn't have theS_IFREG
bit set./dev/stdout
is a file, but not a regular file. In fact, there's some dance in the kernel to give it an entry in the filesystem. On Linux:On OpenBSD:
On FreeBSD: