Why only the lowest available file descriptor is allocated in UNIX?

c, linux, unix

Solution

There are various reasons, but the ultimate one is "because that's the way it has always been done".

- It is easy to track through the list of file descriptors to find the first unused one.

- It is determinate. This was important before the `dup2()` call was available.

Classically, the file descriptor table for a process was a fixed size, and quite small (20 in 7th Edition Unix, IIRC).

The deterministic mechanism was crucial for I/O redirection in shell. For example:

cat file1 file2 > file3

The shell needed to redirect standard output to `file3`. Therefore, it could use:

close(1);  // Standard output
if (open("file3", O_WRONLY|O_CREAT, 0666) != 1)
   …error creating file…

and it would know that because standard input was already open (file descriptor 0), the `open()` would return file descriptor 1.

These days, you can't deduce anything much from a file descriptor value. I can write:

int fd1 = open(filename, flags, mode);
int fd2 = dup2(fd1, 1024);
close(fd1);

and the fact that `fd2` (should) contain 1024 doesn't tell you anything about the order in which it was opened compared to file descriptor 3 (which might plausibly be returned by the next `open()` call).

Problem

In a group talk I was intrigued by this question - Why UNIX standard demands the guarantee of allocation of only lowest available file descriptor for a process ? And only possible answer that I could thought of was scalability. Since we always choose the least available descriptor, the used portion of the descriptor bitmap is mostly dense and thus the growth of array is slower. I was just wondering if there are any other reasons which I am not aware of. Also, do we have some scenarios where logical conclusions (those we can use in a program) can be made if we know that a given descriptor is bigger/smaller than the other. My understanding though does NOT allows using such technique because it does NOT guarantee the age of the descriptor.

Original source