Reputation: 1993
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?
Upvotes: 3
Views: 6784
Reputation: 1428
Check the man page for ulimit
or is that, like, way too naïve?? Because I'm pretty sure that the OS limits user fifos to 8k.
Hopefully you won't need a new kernel!!
Upvotes: 0
Reputation: 70372
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, thebuf
argument should point to a buffer at leastsize
bytes long; this buffer will be used instead of the current buffer. If the argumentbuf
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.
Upvotes: 6
Reputation: 4411
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.
Upvotes: 2