0

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.

user129393192
  • 797
  • 1
  • 8
  • They are three separate file descriptors. `0`, `1` and `2` in normal cases. – Ted Lyngmo Apr 24 '23 at 21:47
  • Well of course ... but that wasn't what I was asking. I'm asking about the underlying open file description, what this means for the "file offset" --> I had thought streams do not have a notion of one, and how it interacts with the terminal -> since it seems that `STDIN_FILENO` has write capability. – user129393192 Apr 24 '23 at 21:56
  • I'm not sure that character devices have an `lseek()` offset. Writing into one won't appear in its input. It's not a loopback. – pmacfarlane Apr 24 '23 at 22:22
  • Does this answer your question? [What is the difference between stdin and STDIN\_FILENO?](https://stackoverflow.com/questions/15102992/what-is-the-difference-between-stdin-and-stdin-fileno) – user1934428 Apr 25 '23 at 09:23
  • POSIX requires that `STDIN_FILENO` equals to 0. This certainly is the case if you run under Linux. Similarily, stdout has on Posix systems the value 1 and stderr has 2. See [here](https://stackoverflow.com/questions/18507057/what-are-the-possible-values-for-file-descriptors) for other FD values. Note also that these filedesciptors (0-2) are not seekable. If this doesn't answer your question, I don't really understand what you are asking. – user1934428 Apr 25 '23 at 09:29
  • What I was asking, which apparently I did not make clear, is the internal representation of the fds 0, 1, 2 and how it seems they share an open file description, and perhaps more insight into what the terminal is actually doing, since it seems you can write to `STDIN_FILENO` with the same results as writing to `STDOUT_FILENO`. – user129393192 Apr 25 '23 at 20:28
  • A file descriptor is just a non-negative integer number. As such, it has no internat representation. In terms of implementation, a file descriptor is typically an index into an array of elements, each of them describing a file. I guess that the internals of such an array depend at least on the operating system, maybe also on the programming lanuage you are using. – user1934428 Apr 26 '23 at 09:34
  • Have a look [here](https://en.wikipedia.org/wiki/File_descriptor) how these structures typically look like. Why do you want to fiddle with these internals? Are you going to write a compiler, or a new operating system? – user1934428 Apr 26 '23 at 09:36
  • BTW, if you are in particular interested in descriptors which work like stdin or stdout, you can find [here](https://linux-kernel-labs.github.io/refs/heads/master/labs/device_drivers.html) the implementation details for Linux. – user1934428 Apr 26 '23 at 09:40

0 Answers0