我有一个程序,forks()子进程被另一个进程替换,比如 A,它是通过调用来运行的execv(A)。
如何将 processA的输出重定向到/dev/null??
到目前为止我已经尝试过:(处理错误部分,并且没有发生错误)
pid = fork();
//check for errors
if (pid<0){
//handle error
}
//the child process runs here
if (pid==0){
fd = open("/dev/null", O_WRONLY);
if(fd < 0){
//hadnle error
}
if ( dup2( fd, 1 ) != 1 ) {
//handle error
}
if (execv(lgulppath.c_str(),args)<0){
//handle error
}
}
但是,可以理解的是,这不起作用,因为它将子进程的输出重定向到/dev/null而不是进程的输出,然后A替换子进程的输出。
有任何想法吗?(我没有A进程的代码)
谢谢