以编程方式从父进程的子进程捕获打印,因此它们不会转到标准输出

Programatically capture prints from child process at parent so they don't go to stdout

本文关键字:标准输出 打印 方式 编程 进程 子进程      更新时间:2023-10-16

我确实有一个在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");
}