简体   繁体   中英

Unix C Programming: multiple pipes and forks

I am trying to solve assignment problem based on number of available CPU. Then I need to create numbers of child process accordingly and two way pipes to communicate between parent to each child. For example there are 4 available CPU, I need to create 3 children (fork() 3 times), and create 6 pipes. I searched different sources and they usually create eg// int pipefd[2] and then pipe(pipefd); my first thought was after find out numbers of CPU, I build up matrix as following codes:

  /* Find # of CPU and minus 1 */
  cpu_num = (sysconf (_SC_NPROCESSORS_ONLN)) - 1;

  /* Pipe creation */
  /* 2 Pipes for 1 child */
  enum {p_read, p_write};
  for (i = 0; i < (cpu_num); i++) {
    int ptcfd[i][2];
    if (pipe(ptcfd[i]) < 0) {
      perror("Parent to Child Pipe Error");
      exit(20);
    }
    int ptpfd[i][2];
    if (pipe(ptpfd[i]) < 0) {
      perror("Child to Parent Pipe Error");
       exit(20);
    }
  }

  char buf[PIPE_BUFF_LEN]; /* create Buffer with size 1024 */

  /* Array to store children PID */
  int childid[cpu_num];

  /* Children preparation task */
  for (i = 0; i < cpu_num; i++) {
    pid = fork();
    /* Failed to fork section */
    /* Creating error message */
    if (pid < 0) {
      printf("Failed to create fork PID #%d. \n\n", getpid());
      exit(44);
    }

    /* Parent */
    /* Record PID into childid array for later use */
    if (pid > 0) {
        close(ptpfd[i][p_write]);
        close(ptcfd[i][p_read]);
        childid[i] = pid;
    }
    /* Child */
    if (pid == 0) {
        close(ptcfd[i][p_write]);
        close(ptpfd[i][p_read]);
    }
  }

Assume I have 4 CPUs. But these doesn't seems right as I try to compile, it says: main.c:138:12: error: 'ptpfd' undeclared (first use in this function) close(ptpfd[i][p_write]);

main.c:138:12: note: each undeclared identifier is reported only once for each function it appears in

main.c:139:12: error: 'ptcfd' undeclared (first use in this function) close(ptcfd[i][p_read]);

So I start to think (but unable to confirm from google search) that maybe I don't need to use matrix to create 6 pipes, I just need to create regular 2 way pipes: ptpfd[2] and ptcfd[2], then after I fork(), each child will just inherit 2 pipes their own? If my assumption is wrong, please help me, thanks

C implements block scope for variables. You're declaring the ptpfd and ptcfd arrays inside the first for loop that calls pipe() , and then trying to use them in the second for loop that calls fork() . You should declare the arrays outside the loops, so they're visible throughout the function.

Also, the size of the arrays is wrong. You declared int ptpfd[i][2] . So on the first iteration of the loop, it will be a 0x2 array, on the second iteration it will be a 1x2 array, on the third iteration it will be 2x2, and so on. The size of the array should be based on the number of processes you're going to be forking, which is cpu_num , so it should be declared:

int ptpfd[cpu_num][2];
int ptcfd[cpu_num][2];

i will then be the index into these arrays for the pipe for each child process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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