我必须在包含 10 个子进程的 1000 个数字的数组中找到最大值(这样每个子进程只检查一百个值),而父进程只需要收集数据。我已经完成了整个事情,但我坚持阅读这些价值观。
这是代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int array[1000];
int i, j;
int pids[10];
int searchminindex;
int searchmaxindex;
int maxindex;
srand(time(NULL));
//fill up array with random numbers
for(i = 0; i < 1000; i++)
{
tomb[i] = random() % 5000;
}
//create 10 child processes
for (i = 0; i < 10; i++) {
if ((pids[i] = fork()) < 0) {
perror("fork");
abort();
}
else if (pids[i] == 0) {
searchminindex = i * 100;
searchmaxindex = (i+1) * 100;
//finding the biggest value
maxindex = searchminindex;
for(j = searchminindex+1; j < maxindex; j++) {
if( array[maxindex] < array[j])
maxindex = j;
}
}
}
for(i = 0; i < 10; i++){
//here's where I'd read the return values of the subarrays
}
return 0;
}
我尝试过使用管道并使用 WEXITSTATUS,但我真的很困惑,不知道在哪里关闭管道的一端以及类似的东西,而使用 WEXITSTATUS 我完全迷路了。
有什么办法可以帮忙吗?