【问题标题】:c pipes, stdin/stdout and sortc 管道、标准输入/标准输出和排序
【发布时间】:2012-02-23 22:45:01
【问题描述】:

我正在尝试编写一个程序来分叉并发送排序(linux)一些单词以对标准输入进行排序,因为没有参数的排序将使用标准输入。然后从父级的排序中收集标准输出,输出到父级的标准输出。

目前我的程序挂起。 谁能帮我解释一下出了什么问题?

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>


int main(int argc, char *argv[]){
    int fi[2];
    int fo[2];
    int oldstdin;
    int oldstdout;

    if (pipe(fi) == -1){
        exit(EXIT_FAILURE);
    }
    if (pipe(fo) == -1){
        exit(EXIT_FAILURE);
    }
    oldstdin = dup(STDIN_FILENO); /* Save current sdtin */
    oldstdout = dup(STDOUT_FILENO); /* Save current stdout */

    close(STDIN_FILENO);
    close(STDOUT_FILENO);


    if(dup2 (fo[0],STDIN_FILENO) == -1) /* Make the read end of out to be stdin */
        exit(EXIT_FAILURE);
    if(dup2 (fi[1],STDOUT_FILENO) == -1) /* Make the write end of in to be stdout */
        exit(EXIT_FAILURE);


    switch(fork()){
    case -1:
        exit(EXIT_FAILURE);
    case 0: /* CHILD */  
        close(fo[0]);
        close(fo[1]);
        close(fi[0]);
        close(fi[1]);
        execlp("sort","sort", (char *)NULL);
    default:
        break; /* fall through to parent */
    }

    char input[100];
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    dup2(oldstdin,STDIN_FILENO);
    dup2(oldstdout,STDOUT_FILENO);

    close(fo[0]); /* these are used by CHILD */
    close(fi[1]); /* "" */

    write(fo[1],"dino\nbat\nfish\nzilla\nlizard\0",27);
    input[read(fi[0], input,100)] = 0;
    printf("%s", input);
}

【问题讨论】:

    标签: c linux stdout pipe stdin


    【解决方案1】:

    我不认为 sort 期望 stdin 被 nul 终止:如果从文件重定向就不会。

    但是,它确实需要知道输入何时完成(并且在完成之前不会发出任何内容,因此您的读取将阻塞)。写信后尝试关闭fo[1]

    哦,你可以在strace -f 下运行它来看看发生了什么,它会告诉你父进程和子进程都在做什么。

    【讨论】:

    • Closing fo[1] (我知道你写了 fi[1] 但我知道你的意思是 fo[1] ;])成功了,谢谢!如此愚蠢的问题 - 我很感激!