用 C (cgi) 刷新输出缓冲区
以下代码:
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 Works great after disabling fflush(stdout)
nor setvbuf(stdout, NULL, _IONBF, 0)
is working!!!mod_deflate
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定我是否正确理解你的问题,但在 C 中,你可以
fflush
)禁用缓冲 (
setbuf
,setvbuf< /代码>)
如果这些不起作用,则说明有其他东西正在做缓冲,或者缓冲不是问题。
I am not quite sure I understand your question correctly, but in C you can
fflush
)Disable buffering (
setbuf
,setvbuf
)If these won't work, then either something else is doing buffering or buffering is not the problem.
您可以尝试
fflush
。printf
之后的 stdoutYou could try to
fflush
stdout
after yourprintf
.