以编程方式从父进程的子进程捕获打印,因此它们不会转到标准输出
Programatically capture prints from child process at parent so they don't go to stdout
我确实有一个在HPUX和Linux上运行的c++程序。我的程序创建了2个子进程,父进程等待两个子进程完成。当我在运行目录下执行程序时,如下所示:运行> myProgram
我得到了显示的子进程和父进程的打印结果。所以我需要一种方法来停止我的子进程打印到命令提示符窗口。在子进程完成后,我想打开打印,以便父进程可以显示结果。
有人知道如何打开和关闭打印吗?
从这个答案中汲取灵感:
#include <stdio.h>
main()
{
int fd;
fpos_t pos;
printf("printing to stdout enabledn");
fflush(stdout);
fgetpos(stdout, &pos);
fd = dup(fileno(stdout));
// Standard output redirected to the null device
freopen("/dev/null", "w", stdout);
f();
// Standard output restored to its previous fd (the screen)
fflush(stdout);
dup2(fd, fileno(stdout));
close(fd);
clearerr(stdout);
fsetpos(stdout, &pos); /* for C9X */
printf("printing to stdout enabled againn");
}
f()
{
printf("message sucked away by /dev/null");
}
相关文章:
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用提升过程获取 shell 命令的标准输出
- 将标准输出重定向到ostream
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 在程序执行期间从标准输出重定向到自定义流
- 标准输出C++意外行为
- 防止 qDebug() 写入标准输出
- 有没有办法知道C++中将打印到标准输出的大小?
- 在标准输出中执行打印会导致数组中随机分配值
- 反转行的程序未打印为标准输出
- 在C++中打印到标准输出时 /* 注释的行为
- 在 c/c++ 中编辑从 stdin 打印在标准输出上的文本
- 通过C++从标准输出中的一些打印数据中提取特定值
- EXECLP系统调用不打印到标准输出
- (可选)在运行时打印到标准输出
- 如何在Linux (GCC)中打印输出(而不是标准输出)
- Linux打印文档到标准输出或文件
- 是否有一种方法可以查看哪个线程正在使用gdb打印标准输出或标准错误?
- 以编程方式从父进程的子进程捕获打印,因此它们不会转到标准输出
- 将字符串变量的值与一些文本链接,并将其打印到C++中的标准输出