Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What is best way to implement "tail"?

Reply
Thread Tools

What is best way to implement "tail"?

 
 
Owen Zhang
Guest
Posts: n/a
 
      09-19-2007
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.

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-19-2007
Owen Zhang <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-19-2007
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
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      09-19-2007
On Sep 19, 2:11 pm, Erik Wikström <(E-Mail Removed)> 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



 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      09-19-2007
On Sep 19, 11:11 am, Erik Wikström <(E-Mail Removed)> 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.


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      09-19-2007
On Sep 19, 3:06 pm, user923005 <(E-Mail Removed)> wrote:
> On Sep 19, 11:11 am, Erik Wikström <(E-Mail Removed)> 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.

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-19-2007
On Wed, 19 Sep 2007 11:52:02 -0700, Lew Pitcher wrote:

> On Sep 19, 2:11 pm, Erik Wikström <(E-Mail Removed)> 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

 
Reply With Quote
 
Martien verbruggen
Guest
Posts: n/a
 
      09-19-2007
On Wed, 19 Sep 2007 11:52:02 -0700,
Lew Pitcher <(E-Mail Removed)> wrote:
> On Sep 19, 2:11 pm, Erik Wikström <(E-Mail Removed)> 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
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-19-2007
On Wed, 19 Sep 2007 11:52:02 -0700, Lew Pitcher wrote:

> On Sep 19, 2:11 pm, Erik Wikström <(E-Mail Removed)> 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-19-2007
Army1987 <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to implement extremely heavy calculation? roygon ASP .Net 10 04-07-2006 03:22 PM
Best way to implement custom control: Repeater vs. Anything Else =?Utf-8?B?V2ViTWF0cml4?= ASP .Net 3 12-09-2004 02:26 AM
what is the best way to implement a navigation David ASP .Net 1 06-03-2004 05:20 AM
Re: BEST WAY TO implement shopping basket Kevin Spencer ASP .Net 0 08-05-2003 04:23 PM
Re: Best way to implement ASP Session variables Steve C. Orr, MCSD ASP .Net 0 07-16-2003 09:09 PM



Advertisments