Pipes as stdin/stdout in process communication.

别说谁变了你拦得住时间么 提交于 2019-12-10 18:35:40

问题


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:


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.



来源:https://stackoverflow.com/questions/16390323/pipes-as-stdin-stdout-in-process-communication

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!