1

I can understand each process fd 0,1,2 is pointing to /dev/stdin, /dev/stdout, /dev/stderr. If I write some data to /dev/stdin to I was able to receive data from fd 1 of the respective process only.

If every process stdout (fd 1) is pointing to /dev/stdout then if I write some data to /dev/stdin should broadcast to all process right?, am I missing anything?

Kusalananda
  • 333,661
  • If you stop thinking of standard I/O in terms of the pseudo-files in /dev, things will become a lot clearer. The pseudo-files have quite a number of behaviours that muddy the waters. Think of just the file descriptors. – JdeBP Mar 24 '20 at 12:26

1 Answers1

2

On Linux, /dev/std{in,out,err} don’t connect to devices, they give access to each process’ corresponding file descriptors. See the output of ls -l /dev/std*:

lrwxrwxrwx. 1 root root 15 Feb 24 09:34 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx. 1 root root 15 Feb 24 09:34 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx. 1 root root 15 Feb 24 09:34 /dev/stdout -> /proc/self/fd/1

So this is all handled by /proc/self; see Which process is `/proc/self/` for?

Stephen Kitt
  • 434,908
  • In other words, they're not "real" devices in the sense that something like /dev/sda is, but "magic" implemented by the kernel. Other OS's would have something similar, even though they might not have /proc. – ilkkachu Mar 24 '20 at 10:43
  • @ilkkachu Yes. They are virtual in- and outputs. That way more than one program can be open and depending on which one is active, your keyboard input is directed to one or the other. – Ned64 Mar 24 '20 at 10:51
  • @Stephen will it inherit to the child or the child will also gets a unique one. – Karthik Nedunchezhiyan Mar 24 '20 at 12:48
  • @Karthik that depends on how the child is set up. If it’s given the same file descriptors as its parent, then it will share them; if it’s given new ones, it will have its own. For example, if you’re running a shell in a terminal, commands you start from that shell will use the same standard streams as the shell, unless you redirect them. – Stephen Kitt Mar 24 '20 at 12:58
  • @StephenKitt if I let the child inherit parent's fd then child's fd will point to the parent? i mean if i write on the child stdin will it receives on the parent stdout? – Karthik Nedunchezhiyan Mar 24 '20 at 13:05
  • @Karthik if the child inherits the parent’s fds, its standard in will read from the same source as the parent, and its standard out will write to the same target as the parent. In the shell in a terminal, both are the terminal; see ls -l /proc/self/fd. – Stephen Kitt Mar 24 '20 at 13:09