用 C (cgi) 刷新输出缓冲区

发布于 2024-11-14 09:33:35 字数 473 浏览 4 评论 0原文

以下代码:

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

工作正常,如果从命令行运行程序,则每秒刷新标准输出缓冲区。但是,当我尝试在网络浏览器(服务器 - Linux 上的 apache,通过 cgi 处理的编译可执行文件(使用 gcc))中访问该程序时,内容仅在 4 秒后显示,而不是“逐步”。我正在寻找类似 PHP 的 ob_flush() 的东西。 顺便问一下,cgi 是处理已编译的 C 可执行文件的最佳方式吗?

更新: 既不是 fflush(stdout) 也不是 < code>setvbuf(stdout, NULL, _IONBF, 0) 正在工作!!! 禁用 mod_deflate 后效果很好。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾冷 2024-11-21 09:33:35

我不太确定我是否正确理解你的问题,但在 C 中,你可以

  • 在每次打印后刷新 (fflush)
  • 禁用缓冲 (setbuf, setvbuf< /代码>)

    setvbuf(stdout, NULL, _IONBF, 0); /* 这将禁用标准输出的缓冲 */
    

如果这些不起作用,则说明有其他东西正在做缓冲,或者缓冲不是问题。

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.

春夜浅 2024-11-21 09:33:35

您可以尝试 fflush printf 之后的 stdout

You could try to fflush stdout after your printf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文