Consider this simple program:
#include <unistd.h>
int main () {
write(STDIN_FILENO, "hello\n", 6);
return 0;
}
and that the output will be viewed on the terminal even if I replace STDIN_FILENO
with STDERR_FILENO
xor STDOUT_FILENO
.
I understand that bash
holds fd 255
, (zsh
, 10
), for special purpose to maintain access to the terminal when one file descriptor is closed. This seems to imply that there is no real difference in the underlying representation for the associated streams.
My question is as follows:
- what distinguishes the "stream devices" STDIN, STDOUT, STDERR?
- Do they share the same open file description, so that a write to one advances the
lseek()
offset of another? How would a program such as:
$ cat
hello
hello
work otherwise without them overwriting eachother?
On my Linux VM (using zsh
), it can be seen:
$ ls -l /proc/$$/fd
total 0
lrwx------ ... ... ... 0 -> /dev/tty1
lrwx------ ... ... ... 1 -> /dev/tty1
lrwx------ ... ... ... 10 -> /dev/tty1
lrwx------ ... ... ... 2 -> /dev/tty1
so it seems that they do share the same device, and I was hoping for more insight on this.