迫使程序刷新其标准输出重定向时 [英] forcing a program to flush its standard output when redirected

查看:117
本文介绍了迫使程序刷新其标准输出重定向时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有打印输出到标准输出一个封闭源代码的程序。我需要解析输出。所以我将输出重定向到一个FIFO使用dup2(从中我可以在叉和高管二进制父进程读取),然后执行的是程序。的问题是,fprintf中成为缓冲,因为它现在写入文件的文件中调用

i have a closed source program that prints output to standard output. i need to parse the output. so i redirect the output to a fifo (from which i can read in the parent process that forks and execs the binary) using dup2 and then exec the program. the problem is that the fprintf calls in the file become buffered because it is now writing to a file.

我打过电话与标准输出_IONBF setvbuf用来调用exec之前。但问题依然存在。

i tried calling setvbuf with _IONBF on stdout before calling exec. but the problem still exists.

为什么不setvbuf用来在我的情况下帮助?

why does setvbuf not help in my case?

我怎么能强制输出就脸红?

how can i force the output to get flushed?

推荐答案

setvbuf用来()都没有区别,因为它改变的C运行时库的一部分的状态,而不是操作系统的一部分。当新的进程开始运行,它的C运行时库将被重新初始化(如果它使用了CRT在所有这就是!)

setvbuf() makes no difference because it changes the state of part of the C runtime library, not part of the OS. When the new process begins running, its C runtime library will be reinitialised (that's if it uses a CRT at all!)

我听说过为解决这个获得的唯一方法就是以某种方式伪造一个终端的过程。这是因为,如果他们相信它们连接到交互终端(在Unix世界最CRT库默认情况下将只执行行缓冲:如果 isatty()为其上的文件返回true描述符),而否则他们将在更大的块(通常缓冲的8Kb左右)。

The only way I have heard of for getting around this is to somehow fake a terminal to the process. That's because most CRT libraries will by default perform only line buffering if they believe they are attached to an interactive terminal (in the Unix world: if isatty() returns true on the file descriptor), whereas otherwise they will buffer in larger blocks (typically 8Kb or so).

本实用看起来像pretty良好的开端。 (从<一个评论借用href=\"http://stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdin-is-interactive-not-a-pipe\">Trick一个应用程序,以为它的标准输入是互动的,不是一个烟斗,它有其他有用的信息。)

This utility looks like a pretty good place to start. (Borrowed from a comment on Trick an application into thinking its stdin is interactive, not a pipe, which has other useful info.)

这篇关于迫使程序刷新其标准输出重定向时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆