首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用2个管道执行cat\grep\grep\grep

使用2个管道执行cat\grep\grep\grep
EN

Stack Overflow用户
提问于 2020-04-09 13:39:45
回答 1查看 170关注 0票数 0

标题对于我想要做的事情是不言自明的,下面是我当前的代码:

代码语言:javascript
复制
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main() {

  pid_t pid ;
  int fd[2], fd2[2];
  pipe(fd);
  pipe(fd2);
  pid = fork(); 

  if(pid == 0) {
    close(1);
    dup(fd[1]);
    close(fd[0]);
    close(fd[1]);

    char *exp[] = {"cat", "filename.txt", NULL};
    execvp("cat", exp);
    exit(EXIT_FAILURE);
  } 
  else if(pid > 0) {
    close(1);
    dup(fd2[1]);
    close(fd2[0]);
    close(fd2[1]);

    char *exp[] = {"grep", "-w", "stringname", NULL};
    execvp(exp[0], exp);

    pid_t pid2=fork();

    close(0);
    dup(fd[0]);
    close (fd[1]);
    close(fd[0]);

  char *exp2[] = {"grep", "-c", "stringname", NULL};
  execvp(exp2[0], exp2);

  exit(EXIT_FAILURE);
  }

  else {
    printf("Error in forking");
    exit(EXIT_FAILURE);
  }
  close(fd[0]);
  close(fd[1]);
  close(fd2[0]);
  close(fd2[1]);
  return 0;
}

目前,该程序正在编译,但没有执行(它在执行过程中卡在某个地方,我没有任何输出),对于我做错了什么以及如何修复它有任何帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-09 14:28:59

代码中存在一些问题:

  1. 第一个grep之后的execvp(exp[0], exp);将在第二个fork()之前执行。这是一个逻辑错误。
  2. 我不太明白如何处理管道的文件描述符。您应该用适当的管道末端替换stdin和stdout,并关闭所有其他末端。

我用这些更改重写了您的代码,使用dup2使代码变得更干净:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    int pipes[4];

    // Create two pipes
    pipe(pipes);
    pipe(pipes + 2);

    // Execute cat
    if (fork() == 0) {
        // Replace output with write end of first pipe
        dup2(pipes[1], 1);

        // Close all ends of pipes (we already copied it with `dup2`)
        close(pipes[0]);
        close(pipes[1]);
        close(pipes[2]);
        close(pipes[3]);

        char *exp[] = {"cat", "filename.txt", NULL};
        execvp(exp[0], exp);
        perror("cat failed");
        exit(EXIT_FAILURE);
    } else {
        // Execute first grep
        if (fork() == 0) {
            // Replace input with read end of 1st pipe
            dup2(pipes[0], 0);

            // Replace output with write end of 2nd pipe
            dup2(pipes[3], 1);

            // Close all ends of pipes
            close(pipes[0]);
            close(pipes[1]);
            close(pipes[2]);
            close(pipes[3]);

            char *exp[] = {"grep", "-w", "stringname", NULL};
            execvp(exp[0], exp);
            perror("first grep failed");
            exit(EXIT_FAILURE);
        } else {
            // Execute second grep
            if (fork() == 0) {
                // Replace input with read end of 2nd pipe
                dup2(pipes[2], 0);

                // Close all ends of pipes
                close(pipes[0]);
                close(pipes[1]);
                close(pipes[2]);
                close(pipes[3]);

                char *exp[] = {"grep", "-c", "stringname", NULL};
                execvp(exp[0], exp);
                perror("second grep failed");
                exit(EXIT_FAILURE);
            }
        }
    }

    // Close all ends of pipes
    close(pipes[0]);
    close(pipes[1]);
    close(pipes[2]);
    close(pipes[3]);

    for (int i = 0; i < 3; i++) {
        wait(NULL);
    }

    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61122501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档