c - 为什么我在关闭管道后执行 execlp?

标签 c linux pipe

嗨,有人能解释一下为什么我应该在关闭管道后使用 execlp 吗?

这是一个例子:

if( cid == 0)
{//Only child cid can run this code

    char msg[256];

    //Redirect output into the pipe instead of the terminal
    dup2(pipe1Fds[1],STDOUT_FILENO);

    //Close pipes or the pipe reader will not get an EOF
    close(pipe1Fds[0]);
    close(pipe1Fds[1]);
    close(pipe2Fds[0]);
    close(pipe2Fds[1]);

    //Execute cmd1
    execlp(cmd1,cmd1,(char *)0);

    exit(0);
}

最佳答案

execlp() 将加载不同的代码到这个进程中,然后运行新程序,因此您需要在加载目标代码之前关闭管道,因为目标代码不会访问管道。
您可以阅读本文以获取更多信息 link .由于您的代码将被 execlp() 加载的程序替换,因此您必须在调用 execlp() 之前关闭管道。

关于c - 为什么我在关闭管道后执行 execlp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22074593/

相关文章:

cosf(M_PI_2) 不返回零

linux - 检查服务是否停止的常用方法

linux - 在 shell 脚本中创建结构(数据结构)

linux - 通过 $@ 杀死

unix - 在 shell/bash 中的两个命令之间连接输入 _and_output

python - 子进程中的多个管道

c++ - 为什么 C 和 C++ 支持在结构中按成员分配数组,但通常不支持?

c - 在 C 中,为什么有些人在释放指针之前先强制转换它?

c - Control+问号的十六进制代码?

linux - 在 zsh 中混合进程替换和管道