File Descriptors – Default Assignment of File Descriptor 3

file-descriptors

$ ls -og /proc/self /proc/self/fd
lrwxrwxrwx 1 64 Jun 18 11:12 /proc/self -> 32157

/proc/self/fd:
total 0
lrwx------ 1 64 Jun 22  2012 0 -> /dev/tty1
lrwx------ 1 64 Jun 22  2012 1 -> /dev/tty1
lrwx------ 1 64 Jun 22  2012 2 -> /dev/tty1
lr-x------ 1 64 Jun 22  2012 3 -> /proc/32157/fd

What is the file descriptor 3 assigned by default?

Best Answer

Nothing: there are three standard file descriptions, STDIN, STDOUT, and STDERR. They are assigned to 0, 1, and 2 respectively.

What you are seeing there is an artifact of the way ls(1) works: in order to read the content of the /proc/self/fd directory and display it, it needs to open that directory.

That means it gets a file handle, typically the first available ... thus, 3.

If you were to run, say, cat on a separate console and inspect /proc/${pid}/fd for it you would find that only the first three were assigned.