使用pipe():如何允许多个子进程同时执行

我正在使用pipe()按索引拆分文件,将该索引发送到子进程,让子进程计算其指定文件块中数字的总和,并将其总和返回给父进程。

我的孩子似乎按顺序执行,我希望他们同时执行,以使这个过程更有效率。

这是我正在使用的代码:

 #include  #include  #include  #include  #include  #include  #include  int main(int argc, char *argv[]) { int numchild; struct timeval stop, start; int i, j, len, ret, fpos=0, val, count=0, total=0, alltotal=0; pid_t pid; int nums = 1000; FILE * file; printf("How many children to use: "); scanf("%d", &numchild); printf("\nWill use %d child process(es).\n", numchild); gettimeofday(&start, NULL); int fd[numchild][2]; //parent to child. one for each int results[2]; //all children to parent pipe(results); fd_set result_fd; FD_ZERO(&result_fd); FD_SET(results[0], &result_fd); struct timeval tm = {.tv_sec=0, .tv_usec=1}; // create all pipes for (i=0; i<numchild; i++) { pipe(fd[i]); } for (i=0; i 0) { file = fopen("file1.dat", "r"); fseek (file, fpos, SEEK_SET); count = 0; total = 0; printf("Child(%d): Recieved position: %d\n", pid, fpos); // read from file starting at fpos // add values read to a total value while (count < (nums/numchild)) { fscanf(file, "%i", &val); total += val; count++; } //write to parent write(results[1], &total, sizeof(total)); printf("Child(%d): Sent %d to parent.\n", pid, total); } else { printf("Child(%d): Error with len\n", pid); } _exit(0); } // parent process pid = getpid(); fpos = ((i*nums*5)/numchild); // 5 is the offset of the file values // write to child process printf("Parent(%d): Sending file position to child\n", pid); write(fd[i][1], &fpos, sizeof(fpos)); // wait for child responce ret = select(FD_SETSIZE+1, &result_fd, NULL, NULL, NULL); //&tm if (FD_ISSET(results[0], &result_fd)) { ret = read(results[0], &total, sizeof(total)); // output total printf("Parent(%d): Recieved %d from child.\n", pid, total); alltotal += total; //printf("\tParent(%d): Total: %d\n", pid, alltotal); } } wait(0); gettimeofday(&stop, NULL); printf("\tTime elapsed: %lu microseconds\n", stop.tv_usec - start.tv_usec); printf("\tParent(%d): Total: %d\n", pid, alltotal); } 

请让我知道我需要改变什么才能使子进程同时运行(不要等到同一时间运行,而是在父进程给他们索引时运行,而不是等待前一个子进程完成)。

从上面的评论中我得出结论:1。这是一个类型2的赋值。它需要使用fork和pipe如果我真的做了这样的事情(并且不清楚它是否值得做),我会可能是使用线程队列和信号量。

鉴于有限制,我会尽力回答你的问题。

问题是你在for循环中有父代码。 所以发生的事情是,每次循环,父进程产生一个子进程,然后发送偏移信息,然后等待结果。 因此,在父进行下一次循环迭代之前强制子进程完成。

答案是有多个循环。 在第一个循环中,生成所有子项。 在第二个循环中,向孩子发送他们的偏移量。 在第三个循环中,收集结果。 在第四个循环中等待孩子终止。 如果你在第一个循环中向孩子发送他们的偏移量,那可能会更快。

另一种方法是在执行每个fork之前在变量中设置偏移量。 这样可以避免使用管道进行输入。 我相信你也可以让每个孩子一起退出。 那么孩子的返回退出状态可以是总和。 父母可以只计算总和,并避免使用返回管道。 这将是一个更好的方法 – 虽然它不会遵循你明显的规则。