代码之家  ›  专栏  ›  技术社区  ›  Toms

从子进程向父进程发送消息

  •  0
  • Toms  · 技术社区  · 10 年前

    我正在执行父代码。然后我做叉,然后执行。我“执行”的新程序抛出了很多控制台消息,我想隐藏这些消息。

    我是否可以将子进程中的所有stdout和stderr消息重定向到文件中?

    我尝试了关闭(1),这样我就不会在控制台(stdout)上转储消息,这并没有帮助

    1 回复  |  直到 10 年前
        1
  •  4
  •   SzG A B    10 年前
    pid_t pid = fork();
    /* Child process */
    if (pid == 0) {
        /* Open log file */
        int log = creat("logfile", 0644);
        /* Redirect stdout to log file */
        close(1);
        dup(log);
        /* Redirect stderr to log file */
        close(2);
        dup(log);
        /* Execute other program: its stdout & stderr (1 & 2) will both point to logfile */
        execvpe(.......);
    }