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"?

 
 
Flash Gordon
Guest
Posts: n/a
 
      09-19-2007
Lew Pitcher wrote, On 19/09/07 19:52:
> 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;
> }


This is not the same as the Unix "tail -f" since you cannot apply it to
a file being written by another process.
--
Flash Gordon
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      09-20-2007
Flash Gordon wrote:
> Lew Pitcher wrote, On 19/09/07 19:52:
>> 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;
>> }

>
> This is not the same as the Unix "tail -f" since you cannot apply it to
> a file being written by another process.


Hmmm..... No.

lpitcher@merlin:~/code/mytail$ head -10 mytail.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void WaitFiveSecs(void)
{
clock_t now, later;

now = clock();
do
lpitcher@merlin:~/code/mytail$ cc -o mytail mytail.c
lpitcher@merlin:~/code/mytail$ mytail </var/log/messages
Sep 19 04:40:02 merlin syslogd 1.4.1: restart.
Sep 19 05:08:52 merlin -- MARK --
Sep 19 05:28:52 merlin -- MARK --
Sep 19 05:48:52 merlin -- MARK --
Sep 19 06:08:52 merlin -- MARK --
.
.
.
Sep 19 20:23:24 merlin popa3d[28471]: 1 (835) deleted, 0 (0) left
Sep 19 20:25:18 merlin sshd[28533]: Accepted publickey for lpitcher from
192.168.11.2 port 32882 ssh2

Seems to work fine for me


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-20-2007
Erik Wikström wrote:
> 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.


Implementing the -f requires abilities outside of standard C. For
the rest of it, a simple method would be to read the file with
ggets and save the last N pointers returned (not forgetting to free
one when overwriting an earlier one). Set ggets.zip on:

<http://cbfalconer.home.att.net/download/>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-20-2007
Lew Pitcher wrote:
> Erik Wikström <(E-Mail Removed)> wrote:
>> 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.


Yes, but your method eats up the whole machine, thus preventing any
other process from updating the input file. Not really practical.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
C. Benson Manica
Guest
Posts: n/a
 
      09-20-2007
On Sep 19, 4:15 pm, Army1987 <(E-Mail Removed)> wrote:

> 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.


Well, the "higher performance" requirement didn't mention anything
about the performance of those other irrelevant tasks, and especially
not the silly tasks that other users might be running. It runs just
fine on the virtual machine!

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-20-2007
Lew Pitcher wrote, On 20/09/07 01:27:
> Flash Gordon wrote:
>> Lew Pitcher wrote, On 19/09/07 19:52:
>>> 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;
>>> }

>> This is not the same as the Unix "tail -f" since you cannot apply it to
>> a file being written by another process.

>
> Hmmm..... No.
>
> lpitcher@merlin:~/code/mytail$ head -10 mytail.c
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> void WaitFiveSecs(void)
> {
> clock_t now, later;
>
> now = clock();
> do
> lpitcher@merlin:~/code/mytail$ cc -o mytail mytail.c
> lpitcher@merlin:~/code/mytail$ mytail </var/log/messages
> Sep 19 04:40:02 merlin syslogd 1.4.1: restart.
> Sep 19 05:08:52 merlin -- MARK --
> Sep 19 05:28:52 merlin -- MARK --
> Sep 19 05:48:52 merlin -- MARK --
> Sep 19 06:08:52 merlin -- MARK --
> .
> .
> .
> Sep 19 20:23:24 merlin popa3d[28471]: 1 (835) deleted, 0 (0) left
> Sep 19 20:25:18 merlin sshd[28533]: Accepted publickey for lpitcher from
> 192.168.11.2 port 32882 ssh2
>
> Seems to work fine for me


Now do it on a file that is not updated for a long time, where "long
time" is defined as long enough for the end of the file to be reached
before the file is extended.
--
Flash Gordon
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      09-20-2007
>> On 2007-09-19 19:41, Owen Zhang wrote:
>>>What is the best way to implement "tail -f" [with emphasis on]
>>>higher performance ...


>On Sep 19, 2:11 pm, Erik Wikström <(E-Mail Removed)> wrote:
>>You would need to use platform specific functions ...


In article <(E-Mail Removed) .com>
Lew Pitcher <(E-Mail Removed)> wrote:
>Nonsense. It can be done (disregarding questions of efficiency) in ISO
>standard C.


Indeed; but as I noted with a little editing above, "questions of
efficiency" seem to be central to the original poster.

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


This fails on any system using my stdio, because you neglect to
call clearerr(stdin). As soon as the first EOF occurs, a "sticky"
EOF flag is set on the underlying stream, and further attempts to
read from it signal EOF again, without asking the underlying OS
for more bytes. Using clearerr() on the stream resets this "sticky"
flag, so that subsequent attempts to read from the stream will ask
the OS for more bytes.

(Implementations are allowed, but not required, to have this sort
of "sticky EOF" behavior. Various stdio implementations vary. I
made my behavor depend upon a single line in the stdio source code,
so that implementors could change it if desired.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-20-2007
In article <(E-Mail Removed)>,
Chris Torek <(E-Mail Removed)> wrote:

>(Implementations are allowed, but not required, to have this sort
>of "sticky EOF" behavior.


C99 says, under fgetc():

If the end-of-file indicator [...] is set, or if the stream is at
end-of-file, the end-of-file indicator [...] is set and the fgetc
function returns EOF.

C90 is less clear: it says:

If the stream is at end-of-file, the end-of-file indicator [...] is
set and the fgetc function returns EOF.

So it seems to have been changed to clarify that sticky behaviour is
required.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      09-20-2007
On Sep 19, 11:46 pm, CBFalconer <(E-Mail Removed)> wrote:
> Lew Pitcher wrote:
> > Erik Wikström <(E-Mail Removed)> wrote:
> >> 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.

>
> Yes, but your method eats up the whole machine, thus preventing any
> other process from updating the input file. Not really practical.


Demonstrably not true.

But even if it were, then it would be a possible QOI issue on the
implementation of the standard library.

On a unitasking system, there would not and could not be another
process for this code to contend against. Thus the code would have
explicit permission to use as much CPU as necessary, but would be
useless because there would be no other code executing (because of the
design of the system, and not because of the example code) to write
the file that this code reads.

On a co-operative multitasking system, the program would effect it's
voluntary release of processing time either through the explicit
invocation a system-specific API (off topic here), or through an
implicit release imbedded in the standard library. I would expect
that, sans system-specific API, the implementation of standard
functions (especially like the time functions) would include such a
voluntary release. Once that release is met, then competing processes
(including the one that writes to the file) can run and extend the
file. Surely, this is a QOI issue here.

Finally, on a preemptive multitasking system, the program is forced
(through an off-topic external mechanism) to release it's processing.
Again, once released, competing processes (including the one that
writes to the file) can run and extend the file.

I stand by the assertions that this will and does work, does not "hog
CPU" and is legal (at least from a C90 standpoint). I don't know how
code written to the C99 standard (sans POSIX) would do this, though.

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-20-2007
In article <(E-Mail Removed). com>,
Lew Pitcher <(E-Mail Removed)> wrote:

>Finally, on a preemptive multitasking system, the program is forced
>(through an off-topic external mechanism) to release it's processing.
>Again, once released, competing processes (including the one that
>writes to the file) can run and extend the file.


And it will compete with them for cpu time. If you have one other
process running, they may well get half the cpu time each, which
certainly amounts to hogging the cpu given that the program doesn't
need to be using it at all.

Anyway, what is the point of the WaitFiveSecs function? If you're
going to busy-wait, why not just keep calling getchar()?

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
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