Pipes as stdin/stdout in process communication.

2019-07-03 01:14发布

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 It compiles and works with no errors, but it gives no output(at least not on stdout as i want it to do). What is strange, when i try to printf sth in last fork it's printing it on stdin. Im not changing stdout in this fork or in the parrent process so it seems weird to me. 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.

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

1条回答
Fickle 薄情
2楼-- · 2019-07-03 01:47

I'm not sure why bu using execlp instead of execl helped. Probably with execl process couldnt find my text file. Althoug i gave him path to it. So I guess execl is working in other directory.

查看更多
登录 后发表回答