在C中一起实现管道和重定向 [英] implementing pipe and redirection together in C

查看:88
本文介绍了在C中一起实现管道和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C制作最小的外壳.我了解管道和重定向的工作方式.我有两个程序,一个处理重定向的程序,例如ls -l > outputfile.我有另一个处理一个管道的程序,例如ls - l | wc.我遇到的主要麻烦是将它们放在一起.基本上,我希望能够在一个程序中同时实现管道重定向.以下是管道代码的一部分.

I am working on making a minimal shell in C. I understand how pipe and redirection work. I have two programs, one program that handles redirection, for example ls -l > outputfile. I have another program that handles one pipe, for example, ls - l | wc. The main trouble I am having is to put them together. Basically, I want to be able to implement both pipe and redirection in one program. Below is part of the code for pipe.

   pipe(p);
      if (fork() == 0) {
      dup2(p[1], 1);
      close(p[0]);
      execvp(upstream[0], upstream );
      exit(1);
    }
    if (fork() == 0) {
    dup2(p[0], 0);

    close(p[1]);
    execvp(downstream[0], downstream);
    exit(1);
    }
    else { /* Parent */
        /*  so Close both ends of pipe */
    close(p[0]);
    close(p[1]);

如果能对此提出一些建议,我将不胜感激.我不确定应该在代码的哪一部分中实现重定向. 在此先感谢

I would appreciate highly if I can get some pointer on this one. I am not sure in which part of my code, I am supposed to implement redirection. Thanks in Advance

推荐答案

您要执行

ls -l   |   wc   >   outputfile
 |           |         |
p-1        p-2      redirect the o/p of ls | wc to outputfile

要实现上述目的,您需要使用fork()系统调用&然后使用dup()dup2()系统调用将o/p重定向到预期的位置.

To achieve above you need to create the process(p-1 and p-2) using fork() system call & then use dup() or dup2() system call to redirect the o/p where it is intended.

这是示例代码

int main(int argc,char *argv[]) {
        int fd[2];
        pid_t cpid;
        close(1); /* so that output file should get file descriptor 1 */
        int file_dsptr = open(argv[3],O_CREAT|O_TRUNC |O_WRONLY , 0664); /* file should be exist in current directory, otherwise use O_CREAT | O_TRUNC */
        printf("[%d]\n",file_dsptr);

        if(argc == 4){
                if(pipe(fd) == -1){
                        perror("error in pipe creation");
                        exit(EXIT_FAILURE);
                }
                cpid = fork();
                if( cpid == 0 ){ /*child process */
                        close( fd[0] ); /* close read end of pipe */
                        if( -1 == dup2(fd[1] , file_dsptr)){ /* duplicate fd[1] to fd where data.txt points */
                                exit( EXIT_FAILURE );
                        }
                        if(-1 == execlp(argv[1] , argv[1] , NULL )){ /* executes the commands */
                                exit( EXIT_FAILURE );
                        }
                        close( fd[1] );
                        _exit( EXIT_SUCCESS );
                }
                else if(cpid > 0){
                        wait( NULL ); /* wait for child to completes */
                        close( fd[1] ); /* close write end of pipe */
                        if( -1 == dup2(fd[0] , STDIN_FILENO)) {
                                exit( EXIT_FAILURE );
                        }
                        if(-1 == execlp(argv[2] , argv[2], NULL )) {
                                exit( EXIT_FAILURE );
                        }
                        close( fd[0] );
                        _exit( EXIT_SUCCESS );
                }
                else{
                        fprintf(stderr, "fork failed\n");
                        exit(EXIT_FAILURE);
                }
        }
        else{
                fprintf(stderr, "Usage Msg :. ./a.out ls wc data.txt \n");
                exit(EXIT_FAILURE);
        }
        return 0;
}

正如我在评论中提到的,上面的代码运行类似

As I mentioned in comments run above code like

gcc -Wall test.c
./a.out ls  wc  outputfile

现在检查outputfile的内容,应该是ls | wc的结果.

And check the content of outputfile now, it should be result of ls | wc.

这篇关于在C中一起实现管道和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆