c - Usage of clock_gettime doesn't allow output redirection to file -
the following program increments variable i
100ms
, prints value. problem doesn't print onto file when redirecting output terminal.
#include <stdio.h> #include <time.h> struct timespec start={0,0}, end={0,0}; int main(void) { double diff; while(1) { unsigned long = 0; clock_gettime(clock_monotonic, &start); { ++i; clock_gettime(clock_monotonic, &end); diff = (((double)end.tv_sec + 1.0e-9*end.tv_nsec) - ((double)start.tv_sec + 1.0e-9*start.tv_nsec)); } while(diff < 0.1); printf("i = %lu\n", i); } return 0; }
i compile gcc counter.c -o counter
, when run ./counter
, values of i
gets printed onto screen, when ./counter > file.out
, there nothing in file. i've tried redirecting stderr 2>
, ./counter > file.out 2>&1
. if remove clock_gettime
function, seems work fine. idea why happens , how circumvent it?
update: question more how printing screen (stdout
) immediately, when redirected not immediate? using clock_gettime
seems trigger buffer. if not redirection works fine.
Comments
Post a Comment