Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Linux, why is there a global /dev/stdin file for all processes?

Shouldn't the standard input for different process unique? If so, shouldn't the path of the stdin file be like /dev/pid/stdin instead of a global /dev/stdin?

Does anyone have ideas about this?

like image 801
Hanfei Sun Avatar asked Apr 14 '16 06:04

Hanfei Sun


People also ask

Does each process have its own stdin?

Every process is initialized with three open file descriptors called stdin , stdout , and stderr .

What is Dev stdin Linux?

/dev/stdin is a symlink to /proc/self/fd/0 , convenient here because of $(< .. ) syntax which expects a file. this could cause a problem because the command may be blocked until stdin closed. it is safe in the meaning that multiline input will be preserved because au double-quotes.

Does each process have its own stdout?

There aren't multiple stdout, stdin and stderr channels; there's only one of each, assigned by the operating system itself. It's possible to redirect stdin, stdout and stderr to different targets on a process by process basis, but it's all handled by the operating system.

What are stdin and stdout of the new process?

stdin / stdout are logical names for open files that are forwarded (or initialized) by the process that has started a given process. Actually, with the standard fork-and-exec pattern the setup of those may occur already in the new process (after fork) before exec is being called.


1 Answers

/dev/stdin is unique because

  • it is a symbolic link to /proc/self/fd/0
  • /proc/self is a symbolic link only seen by your running process to its process-id

The /proc filesystem is a virtual (not real) filesystem which has the ability to show a different view to each process.

Further reading:

  • Linux Filesystem Hierarchy: 1.14. /proc
  • Red Hat Enterprise Linux 3: Reference Guide: Chapter 5. The proc File System
like image 93
Thomas Dickey Avatar answered Nov 05 '22 09:11

Thomas Dickey