将消息从子进程发送到父进程 [英] Send messages from child process to parent

查看:274
本文介绍了将消息从子进程发送到父进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行父代码.然后,我先执行fork,然后执行execvpe.我"execvpe"的新程序会抛出很多控制台消息,我想隐藏这些消息.

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.

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

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

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

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

推荐答案

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

这篇关于将消息从子进程发送到父进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆