简体   繁体   English

管道之间没有正确的重定向

[英]No correct redirection between pipes

I'm writing a program that should work like the following unix command: seq 2 number | awk argument | grep argument我正在编写一个程序,它应该像以下 unix 命令一样工作: seq 2 number | awk argument | grep argument seq 2 number | awk argument | grep argument

#define READ 0
#define WRITE 1

void die(char* msg){
  perror(msg);
  exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {
  int seq_to_awk[2];

  if(pipe(seq_to_awk) == -1)
    die("pipe");

  pid_t child = fork();
  if(child == -1)
    die("fork");

  if(child == 0){
    if(close(seq_to_awk[READ]) == -1)
      die("close");

    if(dup2(seq_to_awk[WRITE], STDOUT_FILENO) < 0)
      die("dup2");

    if(close(seq_to_awk[WRITE]) == -1)
      die("close");

    execlp("seq", "seq", "2", argv[1], NULL);
  }

  child = fork();
  if(child == -1)
    die("fork");

  int awk_to_grep[2];
  if(pipe(awk_to_grep) == -1)
    die("pipe");

  if(child == 0){

    if(close(seq_to_awk[WRITE]) == -1)
      die("close");
    
    if(dup2(seq_to_awk[READ], STDIN_FILENO) < 0)
      die("dup2");

    if(close(seq_to_awk[READ]) == -1)
      die("close");
/**/
    if(close(awk_to_grep[READ]) < 0)
      die("close");

    if(dup2(awk_to_grep[WRITE], STDOUT_FILENO) < 0)
      die("dup2");

    if(close(awk_to_grep[WRITE]) == -1)
      die("close");
/**/    
    execlp("awk", "awk", argv[2], NULL);
    die("execlp");

  }

  if(close(seq_to_awk[READ]) == -1)
    die("close");
  if(close(seq_to_awk[WRITE]) == -1)
    die("close");

/**/
  child = fork();
  if(child == -1)
    die("fork");

  if(child == 0){

    if(close(awk_to_grep[WRITE]) == -1)
      die("close");

    if(dup2(awk_to_grep[READ], STDIN_FILENO) < 0)
      die("dup2");

    if(close(awk_to_grep[READ]) == -1)
      die("close");

    execlp("grep", "grep", argv[3], NULL);
    die("execlp");

  }
/**/

  if(close(awk_to_grep[READ]) == -1)
    die("close");
  if(close(awk_to_grep[WRITE]) == -1)
    die("close");

  wait(NULL);
  wait(NULL);
  wait(NULL);

  exit(EXIT_SUCCESS);
}

When I run the following code with the command: ./program 26 {print} 5 I get nothing as output, when the correct output should be:当我使用以下命令运行以下代码时: ./program 26 {print} 5我什么也得不到 output,而正确的 output 应该是:

5
15
25

I don't understand what exactly I'm doing wrong here.我不明白我到底做错了什么。 When I leave out one of the programs(either awk or grep) the program runs as expected.当我遗漏其中一个程序(awk 或 grep)时,程序会按预期运行。

The problem is when setting up the awk_to_grep pipe. You fork before creating the pipe, so you in fact create two separate pipes.问题是在设置awk_to_grep pipe 时。您在创建 pipe之前fork ,因此您实际上创建了两个单独的管道。 The pipe in your child process is not available to the parent and other children created after that.您的子进程中的 pipe 对父进程和之后创建的其他子进程不可用。

Swap the order to this:将订单交换为:

  int awk_to_grep[2];
  if(pipe(awk_to_grep) == -1)
    die("pipe");

 child = fork();
  if(child == -1)
    die("fork");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM