c - 为什么我无法从 pipefd[0] 获得输出并且无法打印?
问题描述
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
int main(){
int pipefd[2];
int status = pipe(pipefd);
int child = fork();
printf("%d",child);
if(!child){
wait(NULL);
dup2(pipefd[0],0);
int temp = execl("/bin/sort","sort",NULL);
close(pipefd[0]);
}else{
if(!status){
dup2(pipefd[1],1);
close(pipefd[1]);
int temp = execl("/bin/ls","ls",NULL);
}else{
printf("Pipe error\n");
}
}
return 1;
}
在上面的代码中,我的目标是从子进程和从 pipefd[0] 读取的父进程读取,但我不知道为什么它没有被打印...
解决方案
推荐阅读
- unity3d - 在 Unity with AR Foundation 中检测时如何获取跟踪图片的位置?
- elixir - 以表格格式显示具有关联的表格中的数据
- c# - 与Epplus一起使用时如何对类的属性进行序列化时对列进行排序
- c# - .NET 自定义依赖项(无 nuget)
- video - 如何检测其他用户在 sinch 视频通话中断开连接
- flask - Flask-Babel 转换 Flask-WTF SelectField
- c++ - 如果不使用 vector::erase(),如何编写自定义 Vector 方法来删除元素?
- python - 如何处理字典理解中的异常
- reactjs - Reactjs 没有重新渲染从 WebSockets 收到的更新
- wolfram-mathematica - 如何计算 n 阶的行列式,其中 n 是带有数学或枫树或最大值的变量?