简体   繁体   English

c管道,stdin / stdout和排序

[英]c pipes, stdin/stdout and sort

Im trying to write a program that forks and sends sort (linux) some words to sort over stdin since sort without args will use stdin. 我试图写一个程序,分叉和发送sort(linux)一些单词来排序stdin,因为没有args的排序将使用stdin。 Then collect the stdout from sort in the parent to output on parent's stdout. 然后从父级的sort中收集stdout以输出父级的stdout。

Currently my program hangs. 目前我的程序挂起了。 Could anyone lend a hand in explaining to me what is going wrong? 谁能帮我解释一下出了什么问题?

My code: 我的代码:

#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);
}

I don't think sort expects stdin to be nul-terminated: it wouldn't be if redirected from a file. 我不认为sort期望stdin被nul终止:如果从文件重定向则不会。

It does however need to know when the input is complete (and won't emit anything until it is, so your read will block). 但是它确实需要知道输入何时完成(并且在它输出之前不会发出任何内容,因此您的读取将被阻止)。 Try closing fo[1] after writing to it. 写完之后尝试关闭fo[1]

Oh, and you can run this under strace -f to see what's happening, it will show you what both parent and child processes are doing. 哦,你可以在strace -f下运行它来看看发生了什么,它会告诉你父进程和子进程正在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM