10個の子プロセスを持つ1000個の数値の配列の中で最大の値を見つけなければなりません(したがって、すべてが100個の値しかチェックしないようにする必要があります)。 私はすでにすべてのことをやっていますが、私は値を読むことに固執しています。フォークされた10個の子プロセス、親プロセスはどのように戻り値を収集できますか?
は、ここでは、コードです:
#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と私は完全に失われています。
お手伝いができますか?
複数の見出しが良いオプションになります –
これに対してスレッドを使用していませんか?物事は少し楽になります。 –
グローバル配列pidsresults [10]を追加して、各pidsにその結果を独自のインデックスに配置できますか? – zipzit