首页 > 解决方案 > 为什么我无法从 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] 读取的父进程读取,但我不知道为什么它没有被打印...

标签: csortingpipeforkexecl

解决方案


推荐阅读