[comp.unix.questions] How to flush output to disk?

rfinch@caldwr.UUCP (Ralph Finch) (12/05/89)

I'm running very long jobs (1-3 days) in a batch queue, on a Sun
4/330, SunOS 4.0.3.  The screen output is saved in a file called
batch.log.  Often that file is not updated for several hours, which is
a little frustrating; it's difficult to know how far along the model
run is.  Is there any way to force the output to be flushed more
often, say every 15-30 minutes?

Many thanks,
-- 
Ralph Finch		The opinions expressed herein are mine...
...ucbvax!ucdavis!caldwr!rfinch		916-445-0088

matthew@sunpix.UUCP ( Sun Visualization Products) (12/06/89)

In article <606@caldwr.UUCP> rfinch@caldwr.UUCP (Ralph Finch) writes:
>
>Is there any way to force the output to be flushed more often, say every 15-30 minutes?
>
>-- 
>Ralph Finch

The following, untested, script should do it. (I know using 'sync' is a little heavy 
handed, but it gets the job done.)

#! /bin/csh

while (1)
	sleep 900
	sync
end




-- 
Matthew Lee Stier                            |
Sun Microsystems ---  RTP, NC  27709-3447    |     "Wisconsin   Escapee"
uucp:  sun!mstier or mcnc!rti!sunpix!matthew |
phone: (919) 469-8300 fax: (919) 460-8355    |

brnstnd@stealth.acf.nyu.edu (Dan Bernstein) (12/07/89)

In article <606@caldwr.UUCP> rfinch@caldwr.UUCP (Ralph Finch) writes:
> Is there any way to force the output to be flushed more often, say every
> 15-30 minutes?

It sounds like you want stdio output flushed, as in the fflush() call.
If changing the source to use fflush() is impossible, stick the program
under a pty (using, for example, my pty program) so that stdio writes
each line as soon as it sees the newline.

In article <1025@friar-taac.UUCP> matthew@friar-taac.UUCP (Matthew Stier - Sun Visualization Products) writes:
> The following, untested, script should do it. 
> (I know using 'sync' is a little heavy 
> handed, but it gets the job done.)

sync makes sure that the data in the buffers is (at least scheduled to
be) written to disk; this doesn't affect any normal operations other
than straight reads from the raw disk device. In particular, it
certainly doesn't affect stdio.

---Dan

dce@smsc.sony.com (David Elliott) (12/07/89)

In article <1025@friar-taac.UUCP> matthew@friar-taac.UUCP (Matthew Stier - Sun Visualization Products) writes:
>In article <606@caldwr.UUCP> rfinch@caldwr.UUCP (Ralph Finch) writes:
>>
>>Is there any way to force the output to be flushed more often, say every 15-30 minutes?
>
>The following, untested, script should do it. (I know using 'sync' is a little heavy 
>handed, but it gets the job done.)

Are we talking about the same thing?

Maybe SunOS changed this, but on most BSD systems, the disk buffers are
written every 30 seconds via the /etc/update command.  Even if they
weren't, when you open a file that someone has written to, you get
pages from the buffer cache first, so you should get the data.

The problem here is (probably) that Ralph's program is using stdio to
log its output, and since the data is going to a file, it gets buffered
inside the program.  In other words, the data hasn't even gotten
written yet.

The easiest way to fix this is to modify the program to either call
fflush() at certain intervals to force the buffers to be written, or
call setlinebuf() (or the equivalent) to force line-buffered output.
-- 
David Elliott
dce@smsc.sony.com | ...!{uunet,mips}!sonyusa!dce
(408)944-4073
"It's bigger than a breadbox, and smaller than the planet Jupiter."

dan@charyb.COM (Dan Mick) (12/08/89)

fflush()?  You've gotta do that more than once.  Why not add a 
setbuf(stdout, NULL) to the top of the file?  Quick, easy, simple.

-- 
.sig files are idiotic and wasteful.

cpcahil@virtech.uucp (Conor P. Cahill) (12/08/89)

In article <333@charyb.COM>, dan@charyb.COM (Dan Mick) writes:
> fflush()?  You've gotta do that more than once.  Why not add a 
> setbuf(stdout, NULL) to the top of the file?  Quick, easy, simple.

except that it is much more ineffecient unless you plan to call fflush() after
*every* putc()/fprintf()/fputs().
-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

dan@charyb.COM (Dan Mick) (12/10/89)

In article <1989Dec8.131507.1685@virtech.uucp> cpcahil@virtech.uucp (Conor P. Cahill) writes:
|In article <333@charyb.COM>, dan@charyb.COM (Dan Mick) writes:
|> fflush()?  You've gotta do that more than once.  Why not add a 
|> setbuf(stdout, NULL) to the top of the file?  Quick, easy, simple.
|
|except that it is much more ineffecient unless you plan to call fflush() after
|*every* putc()/fprintf()/fputs().

Hey, you're debugging, right?  Efficiency has to be near the bottom of the
priority list anyway.  My point is it's one mod to main(), and it makes sure
you don't miss something in the call tree that you're not (at the moment)
tracing down.  Or, okay, if efficiency is *that* annoying, how about
setlinebuf(), as someone mentioned?


-- 
.sig files are idiotic and wasteful.