For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > January 2005 > Flush Buffers of another process.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Flush Buffers of another process.
Dexter

2005-01-22, 8:56 am

Hi,

I have a process that is running for a long time. It has all its output
redirected to a file.When i start the program i redirect both stderr
and stdout to the log file.

All the output comes to the files only after the program terminates or
in some cases after 5-6 hrs.

Is there a way that i can force the program to dump its buffers to the
log files when it is running.

i know i can change the code to print all its output to stderr so that
it gets printed immediatly but just wanted to know if theres another
way.

Any pointers or info would be helpful.

-Thanks,
Dexter.

Jens.Toerring@physik.fu-berlin.de

2005-01-22, 3:57 pm

Dexter <vikram_ramakrishnan@ieee.org> wrote:
> I have a process that is running for a long time. It has all its output
> redirected to a file.When i start the program i redirect both stderr
> and stdout to the log file.


> All the output comes to the files only after the program terminates or
> in some cases after 5-6 hrs.


> Is there a way that i can force the program to dump its buffers to the
> log files when it is running.


> i know i can change the code to print all its output to stderr so that
> it gets printed immediatly but just wanted to know if theres another
> way.


Since you seem to have access to the code it's rather simple. Either add
a fflush(3) call after each (f)printf() to stdout or use setvbuf(3) to
switch buffering to either line buffering or unbuffered. I.e. a line
like
setvbuf( stdout, NULL, _IOLBF, 0 );
or
setvbuf( stdout, NULL, _IONBF, 0 );

will do. Of course, that's assuming your using C (or C++). But most
other languages should also have methods to do that, in e.g. Perl
use
$| = 1;

to switch the currently selected output handle to unbuffered mode.

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Dexter

2005-01-24, 8:57 am

yeah i understand i can do it. but is there a way without changing the
code.?

Dexter

2005-01-24, 8:57 am

yeah i understand i can do it. but is there a way without changing the
code.?

Jens.Toerring@physik.fu-berlin.de

2005-01-24, 4:00 pm

Dexter <vikram_ramakrishnan@ieee.org> wrote:
> yeah i understand i can do it. but is there a way without changing the
> code.?


No. What you can do is trick the program into assuming that it's
writing to a terminal (instead of a file), thus making it run in
line buffered instead of fully buffered mode (when a program gets
started it calls isatty(3) to figure out what stdout is connected
to). I guess the best method is to invoke the program from a wrapper
that creates a pseudo terminal and exec's the program with stdin,
stdout and stderr redirected to the pty. Then, in the wrapper, read
the output from the program and write it yourself to the file in
line buffered mode. Perhaps having a look at "expect" and the
library it uses, "libexpect", will be also interesting for you.

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com