1

The following code:

int z = 0;
while(z < 4)
{
printf("iteration %d\n",z);
sleep(1);
z++;
}

Works fine and stdout buffer is flushed every second if running the program from command line. However, when I try to access the program in a web browser (server - apache on linux, compiled executable (with gcc) handled through cgi), the content is displayed only after 4 seconds and not "step by step". I am looking for something like PHP's ob_flush(). And, by the way, is cgi the best way of processing compiled C executables?

Update: neither fflush(stdout) nor setvbuf(stdout, NULL, _IONBF, 0) is working!!! Works great after disabling mod_deflate.

2 Answers2

2

I am not quite sure I understand your question correctly, but in C you can

  • Flush after each print (fflush)
  • Disable buffering (setbuf, setvbuf)

    setvbuf(stdout, NULL, _IONBF, 0); /* this will disable buffering for stdout */
    

If these won't work, then either something else is doing buffering or buffering is not the problem.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

You could try to fflush stdout after your printf.

Mat
  • 202,337
  • 40
  • 393
  • 406