3 ms·
> I always took this to mean the values should pack downward and should not be e.g. allocated randomly, though it never seemed clear to me why, The reason for
by CyberRabbi 5y ago
> I always took this to mean the values should pack downward and should not be e.g. allocated randomly, though it never seemed clear to me why,
The reason for this requirement is that early versions of Unix did not have dup2(), only dup(). It has nothing to do with multi threading as this predates pthreads by more than two decades. The shell (sh) makes use of the lowest numbered property to redirect standard in/out/error when setting up pipelines:
int pipes[2];
/* ignore errors */
(void) pipe(&pipes);
if (fork()) {
close(0);
/* guaranteed to return 0 */
(void) dup(pipes[0]);
close(pipes[0]);
close(pipes[1]);
exec_child();
} else {
close(1);
/* guaranteed to return 1, we know 0 is taken */
(void) dup(pipes[1]);
close(pipes[0]);
close(pipes[1]);
exec_parent();
}
Code like this exists in literally every POSIX shell. Anyone saying code like this isn’t common has no idea what they’re talking about.