Send messages from child process to parent

帅比萌擦擦* 提交于 2019-12-14 03:11:04

问题


I am executing the parent code. I then do a fork and then execvpe. The new program that i "execvpe" throws a lot of console messages and i want to hide those.

Is it possible for me to redirect all my stdout and stderr messages in the child process onto a file?

I tried a close(1) so that i dont dump messages on console (stdout) and that didnt help


回答1:


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(.......);
}


来源:https://stackoverflow.com/questions/25315133/send-messages-from-child-process-to-parent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!