管道作为标准输入

管道作为标准输入

本文介绍了管道作为标准输入/输出过程的沟通。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习的管道,我已经发生的问题。我想我的程序作为工作:结果
的grep [字的找] [文件搜索] | grep按[无字] |厕所-l
它编译并没有错误的作品,但它没有给输出(至少不是在标准输出上因为我想要它做的)。什么是奇怪的,当我尝试给printf去年叉它印刷在标准输入某事。 IM在这个叉子或在parrent过程中不改变标准输出如此看来怪我。我试图关闭未使用的管道和冲洗标准输出(它仍然做某事这里?),但很可能还是折腾更多的工作要做。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;无效的帮助(){
        的printf(程序的用法:\\ n
                \\ t./alagrep [fileToSearch] [wordToFind] [wordToExpel]的\\ n);
}
INT主(INT ARGC,CHAR *的argv []){        如果(argc个!= 4){
                帮帮我();
                出口(EXIT_FAILURE);
        }        INT FD [2];
        如果(管道(FD)!= 0){
                的printf(错误在打开一个管道\\ n);
                出口(EXIT_FAILURE);
        }        将为pid_t PID;
        如果((PID =叉())== - 1){
                的printf(错误而分叉\\ n);
                出口(EXIT_FAILURE);
        }如果别人(PID == 0){
                关闭(FD [0]);
                如果(dup2(FD [1],STDOUT_FILENO)小于0){
                        的printf(无法复制标准输出\\ n);
                        _exit(EXIT_FAILURE);
                }
                关闭(FD [1]);
                EXECL(/斌/ grep的,grep的的argv [2],ARGV [1],NULL);
                fflush(标准输出);
        }        关闭(FD [1]);
        INT FD1 [2];
        如果(管道(FD1)!= 0){
                的printf(错误在打开一个管道\\ n);
                出口(EXIT_FAILURE);
        }        如果((PID =叉())== - 1){
                的printf(错误而分叉\\ n);
                出口(EXIT_FAILURE);
        }如果别人(PID == 0){
                关闭(FD1 [0]);
                如果(dup2(FD [0],STDIN_FILENO)小于0){
                        的printf(无法复制标准输入\\ n);
                        _exit(EXIT_FAILURE);
                }
                如果(dup2(FD1 [1],STDOUT_FILENO)小于0){
                        的printf(无法复制标准输出\\ n);
                        _exit(EXIT_FAILURE);
                }
                关闭(FD [0]);
                关闭(FD1 [1]);
                EXECL(/斌/ grep的,grep的, - 我的argv [3],NULL);
                fflush(标准输出);
        }        关闭(FD [0]);
        关闭(FD1 [1]);        如果((PID =叉())== - 1){
                的printf(错误而分叉\\ n);
                出口(EXIT_FAILURE);
        }如果别人(PID == 0){
                关闭(FD1 [1]);
                如果(dup2(FD1 [0],STDIN_FILENO)小于0){
                        的printf(无法复制标准输入\\ n);
                        _exit(EXIT_FAILURE);
                }
                关闭(FD1 [0]);
                EXECL(/斌/ WC,WC, - 1,NULL);
                fflush(标准输出);
        }        关闭(FD1 [0]);
        返回0;
}


为什么使用BU

解决方案

我不知道 execlp 而不是 EXECL 帮助。可能与 EXECL 程序无法找到我的文本文件。 Althoug我给他路径。所以我猜 EXECL 在其他目录下工作。

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 -lIt 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;
}
解决方案

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.

这篇关于管道作为标准输入/输出过程的沟通。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 12:34