簡體   English   中英

使用 2 個管道執行 cat|grep|grep

[英]Executing cat|grep|grep using 2 pipes

標題不言自明,我想這樣做這是我當前的代碼:

#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;
}

目前該程序正在編譯但未執行(它在執行時卡在某處,我沒有得到任何輸出),對我做錯了什么有任何幫助,我該如何解決?

您的代碼中存在一些問題:

  1. execvp(exp[0], exp); 在第一個 grep 之后將在第二個fork()之前執行。 這是一個邏輯錯誤。
  2. 我不太明白您如何使用管道的文件描述符。 您應該用適當的管道末端替換標准輸入和標准輸出並關閉所有其他末端。

我用這些更改重寫了您的代碼,使用dup2使其更清晰:

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM