porcupine icon indicating copy to clipboard operation
porcupine copied to clipboard

c stdio buffering

Open Akuli opened this issue 3 years ago • 1 comments

#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <unistd.h>

int main(void)
{
	while(1) {
		printf("a\n");
		fflush(stdout);
		usleep(500000);
	}
}

Try compiling and running this in porcupine. Doesn't print anything if you remove fflush(stdout)

Akuli avatar Oct 22 '21 22:10 Akuli

stdbuf might be good for this

akuli@akuli-desktop:~$ cat foo.c
#include <stdio.h>
#include <sys/select.h>

int main(void)
{
	printf("a\n");

	struct timeval t = { .tv_sec = 1 };
	select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);

	printf("b\n");
}
akuli@akuli-desktop:~$ gcc foo.c
akuli@akuli-desktop:~$ timeout 1 ./a.out | cat
Terminated
akuli@akuli-desktop:~$ timeout 1 stdbuf -o0 ./a.out | cat
a
Terminated

Akuli avatar Nov 16 '21 19:11 Akuli