简体   繁体   English

管道作为stdin / stdout进程通信。

[英]Pipes as stdin/stdout in process communication.

I'm learning pipes and I have occured problem. 我正在学习管道而且我遇到了问题。 I want my program to work as: 我希望我的程序能够像:
grep [word to find] [file to search] | grep -i [without word] | wc -l grep [word to find] [file to search] | grep -i [without word] | wc -l It compiles and works with no errors, but it gives no output(at least not on stdout as i want it to do). grep [word to find] [file to search] | grep -i [without word] | wc -l它编译并且没有错误地工作,但它没有输出(至少不在stdout上,因为我希望它做)。 What is strange, when i try to printf sth in last fork it's printing it on stdin. 奇怪的是,当我尝试在最后一个分叉中打印它时,它在stdin上打印它。 Im not changing stdout in this fork or in the parrent process so it seems weird to me. 我不是在这个分叉或者parrent过程中改变标准输出,所以对我来说这似乎很奇怪。 I'm trying to close unused pipes and flush stdout(is it still doing sth here?), but there is probably still sth more to do. 我正在尝试关闭未使用的管道并刷新stdout(它还在做什么吗?),但可能还有更多要做的事情。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

void help() {
        printf( "Usage of the program:\n"
                "\t./alagrep [fileToSearch] [wordToFind] [wordToExpel]\n");
}


int main(int argc, char *argv[]) {

        if(argc != 4) {
                help();
                exit(EXIT_FAILURE);
        }

        int fd[2];
        if(pipe(fd) != 0) {
                printf("Error while opening a pipe.\n");
                exit(EXIT_FAILURE);
        }

        pid_t pid;
        if((pid = fork()) == -1) {
                printf("Error while forking.\n");
                exit(EXIT_FAILURE);
        } else if(pid == 0) {
                close(fd[0]);
                if(dup2(fd[1],STDOUT_FILENO) < 0) {
                        printf("Cannot duplicate stdout.\n");
                        _exit(EXIT_FAILURE);
                }
                close(fd[1]);
                execl("/bin/grep","grep",argv[2],argv[1],NULL);
                fflush(stdout);
        }

        close(fd[1]);
        int fd1[2];
        if(pipe(fd1) != 0) {
                printf("Error while opening a pipe.\n");
                exit(EXIT_FAILURE);
        }

        if((pid = fork()) == -1) {
                printf("Error while forking.\n");
                exit(EXIT_FAILURE);
        } else if(pid == 0) {
                close(fd1[0]);
                if(dup2(fd[0],STDIN_FILENO) < 0) {
                        printf("Cannot duplicate stdin.\n");
                        _exit(EXIT_FAILURE);
                }
                if(dup2(fd1[1],STDOUT_FILENO) < 0) {
                        printf("Cannot duplicate stdout.\n");
                        _exit(EXIT_FAILURE);
                }
                close(fd[0]);
                close(fd1[1]);
                execl("/bin/grep","grep","-i",argv[3],NULL);
                fflush(stdout);
        }

        close(fd[0]);
        close(fd1[1]);

        if((pid = fork()) == -1) {
                printf("Error while forking.\n");
                exit(EXIT_FAILURE);
        } else if(pid == 0) {
                close(fd1[1]);
                if(dup2(fd1[0],STDIN_FILENO) < 0) {
                        printf("Cannot duplicate stdin.\n");
                        _exit(EXIT_FAILURE);
                }
                close(fd1[0]);
                execl("/bin/wc","wc","-l",NULL);
                fflush(stdout);
        }

        close(fd1[0]);
        return 0;
}

I'm not sure why bu using execlp instead of execl helped. 我不确定为什么使用execlp代替execl帮助。 Probably with execl process couldnt find my text file. 可能与execl进程无法找到我的文本文件。 Althoug i gave him path to it. Althoug我给了他一条路。 So I guess execl is working in other directory. 所以我猜execl正在其他目录中工作。

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

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