How to print both execl in fork using pipe (two children)?

拜拜、爱过 提交于 2020-05-09 10:27:25

问题


I am trying using pipe() to execute ls | wc. The fork part successfully prints as I expected (first parent --> first child --> second parent --> second child), but it does not print out the second child's part (wc). I put the exactly same codes for both first child and second child, and I have no idea how I can successfully edit the codes. The entire codes are written below:

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

} 

来源:https://stackoverflow.com/questions/61471433/how-to-print-both-execl-in-fork-using-pipe-two-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!