Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   What is best way to implement "tail"? (http://www.velocityreviews.com/forums/t538535-what-is-best-way-to-implement-tail.html)

Owen Zhang 09-19-2007 05:41 PM

What is best way to implement "tail"?
 
What is the best way to implement "tail -f" in C or C++ and higher
performance compared to either unix shell command "tail -f" or perl
File::Tail ? Any suggestion appreciated. Thanks.


Ben Pfaff 09-19-2007 06:00 PM

Re: What is best way to implement "tail"?
 
Owen Zhang <owenzhang.chicago@gmail.com> writes:

> What is the best way to implement "tail -f" in C or C++ and higher
> performance compared to either unix shell command "tail -f" or perl
> File::Tail ? Any suggestion appreciated. Thanks.


What performance problems have you observed with these
implementations?
--
Ben Pfaff
http://benpfaff.org

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= 09-19-2007 06:11 PM

Re: What is best way to implement "tail"?
 
On 2007-09-19 19:41, Owen Zhang wrote:
> What is the best way to implement "tail -f" in C or C++ and higher
> performance compared to either unix shell command "tail -f" or perl
> File::Tail ? Any suggestion appreciated. Thanks.


You would need to use platform specific functions to do that, ask in a
group discussing programming on your platform.

--
Erik Wikström

Lew Pitcher 09-19-2007 06:52 PM

Re: What is best way to implement "tail"?
 
On Sep 19, 2:11 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2007-09-19 19:41, Owen Zhang wrote:
>
> > What is the best way to implement "tail -f" in C or C++ and higher
> > performance compared to either unix shell command "tail -f" or perl
> > File::Tail ? Any suggestion appreciated. Thanks.

>
> You would need to use platform specific functions to do that, ask in a
> group discussing programming on your platform.


Nonsense. It can be done (disregarding questions of efficiency) in ISO
standard C.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void WaitFiveSecs(void)
{
clock_t now, later;

now = clock();
do
later = clock();
while (((later-now)/CLOCKS_PER_SEC) < 5);
}

int main(void)
{
int datum;

for(;;)
{
if ((datum = getchar()) != EOF)
putchar(datum);
else WaitFiveSecs();
}

return EXIT_SUCCESS;
}


> --
> Erik Wikström




user923005 09-19-2007 07:06 PM

Re: What is best way to implement "tail"?
 
On Sep 19, 11:11 am, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2007-09-19 19:41, Owen Zhang wrote:
>
> > What is the best way to implement "tail -f" in C or C++ and higher
> > performance compared to either unix shell command "tail -f" or perl
> > File::Tail ? Any suggestion appreciated. Thanks.

>
> You would need to use platform specific functions to do that, ask in a
> group discussing programming on your platform.


I think it could be implemented purely in ANSI C without much
difficulty.

A super-simple (stinky) implementation would just read into a ring
buffer of size specified by the user, overwriting as it goes.
When the file has been read, spew out the lines still in the buffer.

Now, suppose we want something a bit smarter.

We could create a static array of characters with 1000 characters/row
and 1000 rows.
Next, we seek to the end of the file, and back up one megabyte.
Next we fgets from the current position into our ring buffer,
overwriting as we go until we hit the end.
If any rows exceeds 1000 characters, signal an error.
When we hit the end of the file, just cough up whatever is in the ring
buffer.

I'm sure that there are better ways to do it in standard C, but off
the top of my head I think it would work.

On the other hand, I guess that the UNIX shell command tail is
implemented in C and a simple hack job is not going to do better.
For that matter, the Perl version probably just calls the system tail
function or implements it in C underneath the covers anyway.

So I guess that it will be an exercise in futility.



Lew Pitcher 09-19-2007 07:18 PM

Re: What is best way to implement "tail"?
 
On Sep 19, 3:06 pm, user923005 <dcor...@connx.com> wrote:
> On Sep 19, 11:11 am, Erik Wikström <Erik-wikst...@telia.com> wrote:
>
> > On 2007-09-19 19:41, Owen Zhang wrote:

>
> > > What is the best way to implement "tail -f" in C or C++ and higher
> > > performance compared to either unix shell command "tail -f" or perl
> > > File::Tail ? Any suggestion appreciated. Thanks.

>
> > You would need to use platform specific functions to do that, ask in a
> > group discussing programming on your platform.

>
> I think it could be implemented purely in ANSI C without much
> difficulty.
>
> A super-simple (stinky) implementation would just read into a ring
> buffer of size specified by the user, overwriting as it goes.
> When the file has been read, spew out the lines still in the buffer.


The minimum implementation of tail(1) on Unix depends on knowledge of
an admitedly platform-specific behaviour of the standard C I/O
library. On Unix, EOF is not necessarily a permanent and final
condition, and the implementation of the standard library acknowledges
this. I believe that this falls within a gray area of the C standard,
being an implementation-defined behaviour.

Knowing that EOF is possibly a temporary condition, programs can
choose to ignore the EOF indication on a file, and attempt to continue
reading it. tail(1) (when invoked with the -f "follow" flag)
disregards the EOF indicator, waits a bit (for the process that is
writing the file to write some more, and thus extend the file past
it's current end-of-file condition), and then attempts to read more
data from the file. No ring buffer necessary.

However, some sort of "out of band" condition or signal must be used
to tell the program to stop reading the file. Typically, this would be
done with an implicit or explicit signal handler that would terminate
the program when a suitable signal is presented to it. SIGINT does
nicely here.


Army1987 09-19-2007 08:15 PM

Re: What is best way to implement "tail"?
 
On Wed, 19 Sep 2007 11:52:02 -0700, Lew Pitcher wrote:

> On Sep 19, 2:11 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
>> On 2007-09-19 19:41, Owen Zhang wrote:
>>
>> > What is the best way to implement "tail -f" in C or C++ and higher
>> > performance compared to either unix shell command "tail -f" or perl
>> > File::Tail ? Any suggestion appreciated. Thanks.

>>
>> You would need to use platform specific functions to do that, ask in a
>> group discussing programming on your platform.

>
> Nonsense. It can be done (disregarding questions of efficiency) in ISO
> standard C.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> void WaitFiveSecs(void)
> {
> clock_t now, later;
>
> now = clock();
> do
> later = clock();
> while (((later-now)/CLOCKS_PER_SEC) < 5);

If this uses 100% of processor time, it will block all other
tasks, including any one writing on that file. Anyway it's not a
good idea on a multitasking system, and a particularly bad idea on
multi-user ones. If it uses less than 100% of processor time, it
will actually wait longer than five seconds. The latter problem
can be solved using time() instead of clock(). The resolution will
be just of one seconds, though. (BTW, I'd not call 'now' the one
which stays constant... I'd call 'before' the former and 'now' the
latter.)
> }
>
> int main(void)
> {
> int datum;
>
> for(;;)
> {
> if ((datum = getchar()) != EOF)
> putchar(datum);
> else WaitFiveSecs();
> }
>
> return EXIT_SUCCESS;

When?
> }
>
>
>> --
>> Erik Wikström


--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen


Martien verbruggen 09-19-2007 09:49 PM

Re: What is best way to implement "tail"?
 
On Wed, 19 Sep 2007 11:52:02 -0700,
Lew Pitcher <lpitcher@teksavvy.com> wrote:
> On Sep 19, 2:11 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
>> On 2007-09-19 19:41, Owen Zhang wrote:
>>
>> > What is the best way to implement "tail -f" in C or C++ and higher
>> > performance compared to either unix shell command "tail -f" or perl
>> > File::Tail ? Any suggestion appreciated. Thanks.

>>
>> You would need to use platform specific functions to do that, ask in a
>> group discussing programming on your platform.

>
> Nonsense. It can be done (disregarding questions of efficiency) in ISO
> standard C.


Not if you also take into account that one of the requirements is that
you need to have 'higher performace' than given other tools. To get that
sort of performance, you generally need to resort to platform-specific
tricks, or use functions or assumptions that fall outside of ISO
standard.

Martien
--
|
Martien Verbruggen | The problem with sharks is that they are too
| large to get to the shallow end of the gene
| pool. -- Scott R. Godin

Army1987 09-19-2007 10:52 PM

Re: What is best way to implement "tail"?
 
On Wed, 19 Sep 2007 11:52:02 -0700, Lew Pitcher wrote:

> On Sep 19, 2:11 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
>> On 2007-09-19 19:41, Owen Zhang wrote:
>>
>> > What is the best way to implement "tail -f" in C or C++ and higher
>> > performance compared to either unix shell command "tail -f" or perl
>> > File::Tail ? Any suggestion appreciated. Thanks.

>>
>> You would need to use platform specific functions to do that, ask in a
>> group discussing programming on your platform.

>
> Nonsense. It can be done (disregarding questions of efficiency) in ISO
> standard C.


[snip]
> for(;;)
> {
> if ((datum = getchar()) != EOF)
> putchar(datum);
> else WaitFiveSecs();

And in which way is this better than calling clearerr(stdin)?
> }

--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen


Keith Thompson 09-19-2007 10:55 PM

Re: What is best way to implement "tail"?
 
Army1987 <army1987@NOSPAM.it> writes:
[...]
> If this uses 100% of processor time, it will block all other
> tasks, including any one writing on that file. Anyway it's not a
> good idea on a multitasking system, and a particularly bad idea on
> multi-user ones. If it uses less than 100% of processor time, it
> will actually wait longer than five seconds. The latter problem
> can be solved using time() instead of clock(). The resolution will
> be just of one seconds, though. (BTW, I'd not call 'now' the one
> which stays constant... I'd call 'before' the former and 'now' the
> latter.)


The resolution of time() is typically one second, but that's not
guaranteed.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


All times are GMT. The time now is 09:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.