Automatically flushing STDOUT buffer?

Automatically flushing STDOUT buffer?

Post by Ryan Sommer » Sat, 28 Oct 2000 09:39:21



I'm writing a program that exec's another program and using two
unidirectional pipes and redirected stdin and stdout to provide
communication between the two programs. The problem I am having is data
will sit in the stdout buffer and not travel over the pipe. The reason I
think this is if I do a fflush(stdout) after the printf statement the
parent gets the data. Is there anyway I can automatically have it flush
stdout when it gets a newline? Or not have it buffer at all, just go
straight to the pipe.

Here is the code so far:

/* BEGIN READ.C */
#include <stdio.h>
/* #include <sys/uio.h> */
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>

int readl( int, char *, size_t );

main( int argc, char **argv )
{
        int pipe1[2], pipe2[2];
        FILE *hlout, *hlin;
        pid_t child;
        char inbuf[256];
        char obuf[256];

        pipe( pipe1 );
        pipe( pipe2 );

        if ((child = fork() ) == 0 )
        {
                dup2( pipe1[1], fileno(stdout) );
                dup2( pipe2[0], fileno(stdin) );

                if ( execlp("/usr/home/leadzero/echo", NULL ) == -1 )
                {
                        perror( "execle" );
                        exit(0);
                }
        } else {
                readl( pipe1[0], obuf, 256 );
                puts( obuf );
        }
        return 1;

Quote:}

int readl( int fd, char *buf, size_t nbytes )
{
        static int b, rb;

        for ( b = 0; b < nbytes; b++ )
        {
                rb = read( fd, (buf + b), 1);
                if ( rb == 0 || buf[b] == '\n' )
                        return b;
                else if ( rb == -1 )
                        return rb;
        }

        return b;

Quote:}

/* END READ.C */

/* BEGIN ECHO.C */
#include <stdio.h>
#include <string.h>

main()
{
        char ibuf[256], obuf[256];
        unsigned int p;

        printf("Starting echo...\n");
/* Without this line read (the parent) will stay blocked in read() */
        fflush(stdout);
        fgets( ibuf, 256, stdin  );
        for ( p = 0; p < 256; p++ )
        {
                obuf[p] = toupper( ibuf[p] );
        }

        puts( obuf );
        return 1;

Quote:}

/* END ECHO.C */

Any help is appreciated.

--
Ryan "leadZERO" Sommers

ICQ: 1019590

 
 
 

Automatically flushing STDOUT buffer?

Post by Nate Eldredg » Sat, 28 Oct 2000 09:55:07



> I'm writing a program that exec's another program and using two
> unidirectional pipes and redirected stdin and stdout to provide
> communication between the two programs. The problem I am having is data
> will sit in the stdout buffer and not travel over the pipe. The reason I
> think this is if I do a fflush(stdout) after the printf statement the
> parent gets the data. Is there anyway I can automatically have it flush
> stdout when it gets a newline? Or not have it buffer at all, just go
> straight to the pipe.

man setvbuf

--

Nate Eldredge


 
 
 

1. Forcing a flush of stdout buffer

Hello,

I am writing an application that will access an existing
command-line tool.  It does so using Xt Input callbacks,
with stdin and stdout from the forked command-line tool process.

Unfortunately, the command-line tool won't seem to dump
its buffer when I pipe it to the parent (my app).  I have tried my
program on other command-line programs (e.g. bc) and it works fine.

How can I force it to dump its buffer?  I am running HPUX8.0

(Since I can't modify the command-line tool, I can't do
 an fflush from that process)

Also, why would this tool not empty its buffer when forked
and run with execlp, when it necessarily does so as a foreground
interactive application?  The only way I can seem to get a flush
is by forcing the buffer to get really full.

Many thanks,

2. CA-UNICENTER

3. can I force the stdout buffer of a child process to be flushed?

4. dtpad question

5. flushing the stdout buffer.

6. Apache on RedHat 6.2

7. doing buffered RPC via clnt_call: how to do a buffer flush?

8. linksys pcmcia ethernet cars

9. Flushing stdout from a shell???

10. Flushing stdout periodically

11. flush the stdout.