用子进程和父进程写入管道

cma*_*in0 2 c redirect fork pipe

我正在尝试创建一个调用sort的子项.父级通过管道将数据发送给子级.我的代码编译并运行,但没有输出.我究竟做错了什么?我没有正确关闭管道,写管道还是正确输出数据?

[eddit]在我的系统上,我需要调用/ bin/sort NOT/usr/bin/sort!

#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>

int main(void){
int pipes[2];
pid_t pid;
FILE *stream;

if(pipe(pipes) == -1)
    printf("could not create pipe\n");

switch(fork()){
    case -1:
        fprintf(stderr, "error forking\n");
        break;
    case 0:
        dup2(pipes[0], STDIN_FILENO);

        pid = getpid();
        printf("in child, pid=%d\n");

        if(close(pipes[1]) == -1)
            fprintf(stderr,"err closing write end pid=%d\n", pid);

        execl("/usr/bin/sort", "sort",  (char*) NULL);
        break;
    default:
        stream = fdopen(pipes[1], "w");
        pid = getpid();
        printf("in parent, pid=%d\n", pid);

        if (stream == NULL)
            fprintf(stderr, "could not create file streami\n");

        if(close(pipes[0]) == -1)
            printf("err closing read end pid=%d\n");

                   fputs("bob\n",stream);
        fputs("cat\n",stream);
        fputs("ace\n",stream);
        fputs("dog\n",stream);

        if(fclose(stream) == EOF)
            fprintf(stderr, "error while closing stream\n");
        break;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

[编辑]这是我的工作代码.谢谢大家

#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void){
int pipes[2];
pid_t pid;
FILE *stream;

int stat;
if(pipe(pipes) == -1)
    printf("could not create pipe\n");

switch(fork()){
    case -1:
        fprintf(stderr, "error forking\n");
        break;
    case 0:
        dup2(pipes[0], STDIN_FILENO);

        pid = getpid();
        printf("in child, pid=%d\n", pid);

        if(close(pipes[1]) == -1)
            fprintf(stderr,"err closing write end pid=%d\n", pid);

        if(close(pipes[0]) == -1)
            fprintf(stderr,"err closing write end pid=%d\n", pid);

        execl("/bin/sort", "sort",  (char*) NULL);
        exit(EXIT_FAILURE);
        break;
    default:
        stream = fdopen(pipes[1], "w");
        pid = getpid();
        printf("in parent, pid=%d\n", pid);

        if (stream == NULL)
            fprintf(stderr, "could not create file streami\n");

        if(close(pipes[0]) == -1)
            printf("err closing read end pid=%d\n");

        fputs("bob\n",stream);
        fputs("cat\n",stream);
        fputs("ace\n",stream);
        fputs("dog\n",stream);

        if(fclose(stream) == EOF)
            fprintf(stderr, "error while closing stream\n");
        break;
}

wait(&stat);
return 0;
}   
Run Code Online (Sandbox Code Playgroud)

Jon*_*ler 7

你肯定close()在代码中没有足够的调用,这会锁定进程.

伪代码:

Create pipe
Fork
In parent:
    Close read end of pipe
    Write data to be sorted down write end of pipe
    Close write end of pipe
    Wait for child to die
In child
    Close write end of pipe
    Duplicate read end of pipe to stdin
    Close read end of pipe
    Exec the sort program
    Exit with an error if the exec returns
Run Code Online (Sandbox Code Playgroud)

请注意,伪代码最终会关闭管道的所有四个端点 - 父级中的两个端口和子级中的两个端口.如果你不这样做,你将陷入僵局.