将消息从子进程发送到父进程

时间:2014-08-14 18:29:12

标签: c fork stdout execv

我正在执行父代码。然后我做一个fork然后execvpe。我执行的新计划"抛出很多控制台消息,我想隐藏这些消息。

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

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

1 个答案:

答案 0 :(得分:4)

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(.......);
}
相关问题