簡體   English   中英

管道作為stdin / stdout進程通信。

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

我正在學習管道而且我遇到了問題。 我希望我的程序能夠像:
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它編譯並且沒有錯誤地工作,但它沒有輸出(至少不在stdout上,因為我希望它做)。 奇怪的是,當我嘗試在最后一個分叉中打印它時,它在stdin上打印它。 我不是在這個分叉或者parrent過程中改變標准輸出,所以對我來說這似乎很奇怪。 我正在嘗試關閉未使用的管道並刷新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;
}

我不確定為什么使用execlp代替execl幫助。 可能與execl進程無法找到我的文本文件。 Althoug我給了他一條路。 所以我猜execl正在其他目錄中工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM