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?

1
  • 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. Commented Mar 24, 2020 at 12:26

1 Answer 1

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?

6
  • 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. Commented Mar 24, 2020 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. Commented Mar 24, 2020 at 10:51
  • @Stephen will it inherit to the child or the child will also gets a unique one. Commented Mar 24, 2020 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. Commented Mar 24, 2020 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? Commented Mar 24, 2020 at 13:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.