[Openmcl-devel] timely output from an external-process-output-stream

Gary Byers gb at clozure.com
Wed Mar 23 12:49:27 PDT 2011


See also the C library functions 'setvbuf' and 'setbuf', either or both
of which may be available (and some OSes may offer other variants.)

Doing something like:

   setvbuf(stdout, NULL, _IONBF, 0);

before writing to stdout has the effect of making stdout "unbuffered"
(so all data written to it is immediately written to the underlying device.)



On Wed, 23 Mar 2011, Sol Swords wrote:

> Thanks for the help, Paul and Dietrich.  I also found this:
> http://lists.freebsd.org/pipermail/freebsd-hackers/2008-May/024662.html
> which basically says that the "script" utility can be used to get a program 
> to act like it's talking to a terminal instead of a pipe, so:
>
> (time (let* ((extprog (ccl::run-program "script"
>                                        '("/dev/null" "-q" "-c" "./foox")
>                                        :wait nil
>                                        :input nil
>                                        :output :stream :error :stream))
>             (out (ccl::external-process-output-stream extprog))
>             line buf (keep-going t))
>        (loop while keep-going do
>              (write-line "Waiting for line") (force-output)
>              (setq line (read-line out nil)) ;; no eof-error-p
>              (if line
>                  (progn (push line buf)
>                         (write-line line)
>                         (force-output))
>                (setq keep-going nil)))))
>
> Pretty strange, but it appears to basically work.
>
> - Sol
>
>
> On 03/23/11 12:03, Diedrich Wolter wrote:
>> Hi,
>> 
>> the 'delay' occurs most likely since the output stream is not flushed.
>> Just add the line fflush(stdout) to your C-program and your done. To the
>> best of my knowledge, flushing behavior solely depends on the output
>> device you write to and there's no programmatic way to change this
>> (terminal devices tend to flush after \n, pipes generally don't).
>> 
>>> #include <stdio.h>
>>> int main () {
>>> int i;
>>> for (i=0; i<5; i++) {
>>> printf("line %i\n", i);
>> ---> fflush(stdout); <---
>> 
>>> sleep(1);
>>> }
>>> return 0;
>>> }
>> 
>> 
>> Best,
>> Diedrich
>> 
>> --
>> Diedrich Wolter
>> dwolter at informatik.uni-bremen.de
>> SFB/TR 8 Spatial Cognition, project R3-[Q-Shape]
>> 
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel
>
>



More information about the Openmcl-devel mailing list