Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increasing standard output buffer size

Tags:

c

linux

buffer

I'm running a program which starts a process and then that process writes to stdout which my program uses. Problem is, the output I need is about 42000 bytes. It seems the stdout buffer size is 8192 and I don't want it to flush until it gets to the 42000. Is there a way to set this?

I tried this:

setvbuf ( stdout , NULL , _IOFBF , 50000 ); // ie set it to 50000 bytes

on the code for the subprocess, but it doesn't seem to work at all. Does anyone have any ideas?

like image 760
user1276273 Avatar asked Jul 09 '26 07:07

user1276273


2 Answers

You need to supply a buffer to setvbuf() to work.

static char buf[50000]; /* buf must survive until stdout is closed */
setvbuf ( stdout , buf , _IOFBF , sizeof(buf) );

From the man page:

int setvbuf(FILE *stream, char *buf, int mode , size_t size);
...
Except for unbuffered files, the buf argument should point to a buffer at least size bytes long; this buffer will be used instead of the current buffer. If the argument buf is NULL, only the mode is affected; a new buffer will be allocated on the next read or write operation.

Here is a sample program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[]) {
    char msg[42000];
    char buf[50000];

    setvbuf(stdout, buf, _IOFBF, sizeof(buf));
    memset(msg, 'a', sizeof(msg));
    msg[sizeof(msg)-1] = '\0';
    puts(msg);
    exit(0);
}

On my system, the strace output shows a single write of 42000 bytes.

like image 90
jxh Avatar answered Jul 11 '26 22:07

jxh


As additional information to jxh answer:

char buf[50000];
setvbuf ( stdout , buf , _IOFBF , sizeof(buf) );
/*close stdout before the programs finishes or even before buf goes out of scope*/
fclose(stdout);

you'll have to close stdout aswell because man setvbuff also mentions:

You must make sure that the space that buf points to still exists by the time stream is closed, which also happens at program termination.

like image 31
hetepeperfan Avatar answered Jul 11 '26 22:07

hetepeperfan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!