How are the standard file descriptors implemented?

Sebastian Fricke sebastian.fricke.linux at gmail.com
Wed Oct 7 02:11:17 EDT 2020


Hello,

after my current research on that topic, I fear that my question is
probably way too broad and unspecific, as there seem to be multiple
viable ways, so I try to narrow it down.
First of what is my motivation?
I try to implement these standard file descriptors within a practice
operating system, in order to get file-system specific system calls like
open, read, write and close to work.

What I currently (believe to) know:
- The standard file descriptors are an abstraction layer in between an
  output device like a monitor and an input device like a keyboard. In
  the old days, these were configured directly to the terminals.
- In glibc (https://github.com/bminor/glibc/blob/master/libio/stdfiles.c),
  they are defined there as FILE struct instances that associate to the
  specific file descriptors.
- I can locate the file descriptors of a specific process within Linux
  under: /proc/{process_id}/fd/{0,1,2}
    root at basti:/proc/self# ls -l fd
    total 0
    lrwx------ 1 root root 64 Oct  7 07:54 0 -> /dev/pts/4
    lrwx------ 1 root root 64 Oct  7 07:54 1 -> /dev/pts/4
    lrwx------ 1 root root 64 Oct  7 07:54 2 -> /dev/pts/4
    lrwx------ 1 root root 64 Oct  7 07:54 255 -> /dev/pts/4

  they all point to /dev/pts/4, if I look in the /dev/pts directory:

    root at basti:/dev/pts# ls -l
    total 0
    crw--w---- 1 basti        tty  136, 0 Oct  7 07:53 0
    crw--w---- 1 basti        tty  136, 1 Oct  6 20:59 1
    crw--w---- 1 libvirt-qemu tty  136, 2 Oct  4 16:27 2
    crw--w---- 1 basti        tty  136, 3 Oct  7 07:56 3
    crw------- 1 basti        tty  136, 4 Oct  7 07:56 4
    c--------- 1 root         root   5, 2 Oct  4 12:10 ptmx

  Here I can see that they all point to a character device, with the
  group tty. And I know that /dev/pts/4 is a pseudeo teletype, which is
  a slave of the ptmx multiplexor.

The gaps I currently have:
- Who creates that device /dev/pts/4 ? I was currently not able to
  locate this information.
- How is that device connected to my keyboard/monitor?
- Could I maybe create a simplified version, which just utilizes a file
  for STDIN, STDOUT, STDERR. That are created by the init process and
  inherited to all child processes? Maybe I could then redirect the
  keyboard output to the STDIN file, the STDOUT file to the monitor
  device etc.

Thanks in advance for any help!

Greetings
Sebastian




More information about the Kernelnewbies mailing list