

Linux: Understanding File Descriptors and IO Redirection - symkat
http://symkat.com/59/understanding-file-descriptors-and-io-redirection/

======
klodolph
"Although it is typically not important to know, it’s worth stating anyway
that while you can access these file descriptors though the above paths they
actually exist in the memory allocated to the program by libc."

Incorrect. The file descriptors exist in kernel memory, and user code simply
refers to them by number. (Sure, you have to allocate memory for the number,
but that's not necessarily done by libc.)

------
andrerobot
Unix scripting always amazes me. Every time I read a script or a blog post
about it I learn something new.

After all these years using Linux, I don't remember ever noticing input
redirection.

~~~
guns
Randal Schwartz used to give out the "Useless use of `cat' award" to annoyed
posters in comp.unix.shell because of the relative obscurity of the `<'
operator.

[http://groups.google.com/groups?q=Useless%20Use%20of%20Cat%2...](http://groups.google.com/groups?q=Useless%20Use%20of%20Cat%20Award)

(actually his main point was that most unix commands take a file as an
arguments, but for the ones that don't you can just slurp it into stdin with
`<')

Nowadays, we could say the same thing about useless uses of `echo', given the
modern `<<<' operator.

~~~
klodolph
In those same modern shells, "echo" and "<<<" are both built-in, but "cat" is
not.

------
MarkSweep
This article is a little bit imprecise at times. It refers to pipes as FIFOs
and seems to mix up FILE* and file descriptors.

Also it has a lot more IO redirection and not really much file descriptors
except for the very beginning. The description of how redirection works is
rather vague. There should be something about how the shell dup()'s over
standard file descriptors to create redirection.

~~~
Altreus
I disagree. The post seems to me like it is aimed at learning new things, not
an in-depth investigation on how the things you're already doing work in the
background.

The things in the article are explained on their own level, and then a level
below it, i.e. the file descriptors. How the file descriptors work, where
they're stored, things like that, are well out of the scope of the article.

IMO.

------
dododo
instead of /proc/PID/fd/XX you can simply use /proc/self/fd/XX if PID is
simply the pid of the current process.

standard input/output/error are not necessarily character devices (as stated
in the article). they can be anything (sockets, files, etc).

------
thwarted
_Let’s use a line of Perl to see what the diff program saw._

That's a lot of typing to implement echo in perl.

~~~
Altreus
Would you rather she golfed it? ;)

