Как напечатать оба execl в fork, используя pipe (двое детей)? - PullRequest
0 голосов
/ 28 апреля 2020

Я пытаюсь использовать pipe() для выполнения ls | wc. Часть fork успешно печатается, как я и ожидал (первый родитель -> первый дочерний элемент -> второй родитель -> второй дочерний элемент), но не выводит часть второго дочернего элемента (wc). Я поставил одинаковые коды как для первого, так и для второго ребенка, и я понятия не имею, как я могу успешно редактировать коды. Полные коды написаны ниже:

#include<stdio.h> 
#include<stdlib.h> 
#include<unistd.h> 
#include<sys/types.h> 
#include<string.h> 
#include<sys/wait.h> 

/* NOTE: place a new child in parents. ALWAYS! */


int main() 
{ 
    // We use two pipes 
    // First pipe to send input string from parent 
    // Second pipe to send concatenated string from child 

    int fd1[2];  // Used to store two ends of first pipe 
    int fd2[2];  // Used to store two ends of second pipe 

    char fixed_str[] = "forgeeks.org"; 
    char input_str[100]; 
    pid_t child_a, child_b, child_c; //three children to run three commands

    //first pipe
    if (pipe(fd1) == -1) 
    { 
        fprintf(stderr, "Failed to pipe" ); 
        return 1; 
    } 

    //second pipe
    if (pipe(fd2) == -1) 
    { 
        fprintf(stderr, "Failed to pipe" ); 
        return 1; 
    } 

    //scanf("%s", input_str); 

    child_a = fork(); //first fork

    //first child
    if (child_a == 0)
    { 
         printf("first child\n");
        close(fd1[1]);  // Close writing end of first pipe 

        // Read a string using first pipe 
        char concat_str[100]; 
        read(fd1[0], concat_str, 100); 

        execlp("ls", "ls", NULL); //ls | wc222 | wc //FIXME: Changed with concat_str somehow

        // We only get here if exec() fails
        perror("exec ls");
        exit(1);

        //concat_str[k] = '\0';   // string ends with '\0' 

        // Close both reading ends 
        close(fd1[0]); 
        close(fd2[0]); 

        // Write concatenated string and close writing end 
        write(fd2[1], concat_str, strlen(concat_str)+1); 
        close(fd2[1]); 

        exit(0); 
    } 
    //first parent 
    else if (child_a > 0) 
    { 
      printf("first parent\n");
      child_b = fork(); //second fork

      //second child
      if (child_b == 0)
      {
        printf("second child\n");
        close(fd1[1]);  // Close writing end of first pipe 

        // Read a string using first pipe 
        char concat_str[100]; 
        read(fd1[0], concat_str, 100); 

        execlp("wc", "wc", NULL); //ls | wc222 | wc //FIXME: Changed with concat_str somehow

        // We only get here if exec() fails
        perror("exec wc");
        exit(1);

        //concat_str[k] = '\0';   // string ends with '\0' 

        // Close both reading ends 
        close(fd1[0]); 
        close(fd2[0]); 

        // Write concatenated string and close writing end 
        write(fd2[1], concat_str, strlen(concat_str)+1); 
        close(fd2[1]); 

        exit(0); 
      }
      //second parent
      else if (child_b > 0)
      {
              printf("second parent\n");
        char concat_str[100]; 

        close(fd1[0]);  // Close reading end of first pipe 

        // Write input string and close writing end of first 
        // pipe. 
        write(fd1[1], input_str, strlen(input_str)+1); 
        close(fd1[1]); 

        // Wait for child to send a string 
        wait(NULL); 

        close(fd2[1]); // Close writing end of second pipe 

        // Read string from child, print it and close 
        // reading end. 
        read(fd2[0], concat_str, 100); 
        printf("Concatenated string %s\n", concat_str); 
        close(fd2[0]);
      }
      //second error
      else
      {
        fprintf(stderr, "Failed to fork" ); 
        return 1; 
      }

      char concat_str[100]; 

      close(fd1[0]);  // Close reading end of first pipe 

      // Write input string and close writing end of first 
      // pipe. 
      write(fd1[1], input_str, strlen(input_str)+1); 
      close(fd1[1]); 

      // Wait for child to send a string 
      wait(NULL); 

      close(fd2[1]); // Close writing end of second pipe 

      // Read string from child, print it and close 
      // reading end. 
      read(fd2[0], concat_str, 100); 
      printf("Concatenated string %s\n", concat_str); 
      close(fd2[0]); 
    }   

  //first error
   else 
    { 
        fprintf(stderr, "Failed to fork" ); 
        return 1; 
    } 

} 
...