简体   繁体   English

Unix C 编程:多个管道和分支

[英]Unix C Programming: multiple pipes and forks

I am trying to solve assignment problem based on number of available CPU.我正在尝试根据可用 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.例如有 4 个可用的 CPU,我需要创建 3 个孩子(fork() 3 次),并创建 6 个管道。 I searched different sources and they usually create eg// int pipefd[2] and then pipe(pipefd);我搜索了不同的来源,他们通常创建 eg// int pipefd[2] 然后 pipe(pipefd); my first thought was after find out numbers of CPU, I build up matrix as following codes:我的第一个想法是在找出 CPU 数量后,我按照以下代码构建矩阵:

  /* 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.假设我有 4 个 CPU。 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: 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:138:12:注意:每个未声明的标识符只针对它出现的每个函数报告一次

main.c:139:12: error: 'ptcfd' undeclared (first use in this function) close(ptcfd[i][p_read]); main.c:139:12: 错误: 'ptcfd' 未声明(第一次在这个函数中使用) 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?所以我开始想(但无法通过谷歌搜索确认)也许我不需要使用矩阵来创建 6 个管道,我只需要创建常规的 2 路管道:ptpfd[2] 和 ptcfd[2],然后在我 fork() 之后,每个孩子只会继承自己的 2 个管道? If my assumption is wrong, please help me, thanks如果我的假设是错误的,请帮助我,谢谢

C implements block scope for variables. C 实现了变量的块作用域。 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() .您在调用pipe()的第一个for循环中声明ptpfdptcfd数组,然后尝试在调用fork()的第二个for循环中使用它们。 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] .您声明了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.因此,在循环的第一次迭代中,它将是一个 0x2 数组,在第二次迭代中它将是一个 1x2 数组,在第三次迭代中它将是 2x2,依此类推。 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:数组的大小应基于您要分叉的进程数,即cpu_num ,因此应声明:

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.然后, i将成为每个子进程管道的这些数组的索引。

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

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