How the heck does tail work?
Joe
josephj at main.nc.us
Sat May 15 14:55:12 EDT 2004
Hi. I had too much time on my hands the other night and I started
thinking about how to write the program "tail".
It all seemed pretty simple. Just read through the file saving the last
n lines in a circular buffer and then print them out when done.
But, that has problems. If the file has *really* long lines, it could
run out of memory or be really nasty for the system.
So, just read down the file with a circular buffer of pointers to lines
and then print from the right pointer when done. Well, that got rid of
the memory problem and would work fine for a static file.
What I finally got stumped on was standard input. As far as I know,
once it's read, it's gone - no going back to a pointer and reading again.
I found a version of tail.c on my system and, although it's a bit more
elegant than what I would have written, it's basically the circular
buffer of pointers to lines approach. (I don't know if this is "the"
tail.c. It was just in an obscure directory.)
I understand how this works for a static file, but how does it work for
a pipe?
This is really an operating system question rather than an application
programming issue. It doesn't matter what language the "tail" program
is coded in.
Joe
--
We travel together, passengers on a little spaceship, dependent on its
vulnerable reserves of air and soil; all committed for our safety to its
security and peace; preserved from annihilation only by the care, the
work and the love we give our fragile craft. We cannot maintain it half
fortunate; half miserable; half confident, half despairing; half slave
of the ancient enemies of man, half free in a liberation of resources
undreamed of until this day. No craft, no crew can safely travel with
such vast contradictions. ~ Adlai Stevenson
More information about the nflug
mailing list