使用 fork 和 pipe 在数组中搜索最大值
Max Value search in array using fork and pipe
我必须编写一个 c 程序 (linux) 来搜索数组中的最大值,同时使用 10 child。数组大小为 1000。每个 child 从 100 个数字中搜索最大值。 parent 应该在管道上得到结果。我的代码不能完美运行。主要问题是管道。 parent 只得到第一个最大值。第二个问题是 childs arent 运行 在同一时间(不是一个大问题,但可能有人可以告诉我哪里出了问题)
我为我的代码做了一些笔记,但我的英语太糟糕了。
我希望我以正确的形式复制源代码。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int array[1000]; //main array
int i;
srand (time(NULL)); //for not the same numbers
for(i=0;i<1000;i++){ //array upload
array[i]= ( rand() % (10000+1) ); //from 1 to 10000 random numbers
}
int searchminindex; //search index, 0-100,100-200, etc
int searchmaxindex;
int threads = 10; //number of threads
int fd[2];
pipe(fd);
int arraymax[10]; //the 10 max numbers
for(i=0;i<threads;i++){ //for 10 threads
if(fork() == 0){
close(fd[0]); //close reading
searchminindex=i*100; //number of thread * arraysize/threadsnumber
searchmaxindex=(i+1)*100;
int maxindex=searchminindex; //it will store the max number index, start from the search min index
int j;
for(j=searchminindex;j<searchmaxindex;j++){ //max search
if(array[maxindex]<array[j]){
maxindex=j;
}
}
write(fd[1], &array[maxindex], sizeof(array[maxindex])); //write the max number into the pipe
printf("max: %d\n", array[maxindex]); //that for check the result, will be deleted from the final code
close(fd[1]); //close write
wait(NULL);
exit(0);
}
else{
wait(NULL);
close(fd[1]); //close write
read(fd[0], &arraymax[i], sizeof(arraymax[i])); //read the max and store in arraymax[]
close(fd[0]); //close read
printf("the read max from the pipe: %d\n", arraymax[i]); //that for check the result, will be deleted from the final code
}
}
int arraymaxi=0; //it is search the max in the main array for check the result, will be deleted
int k;
for(k=0;k<1000;k++){
if(array[arraymaxi]<array[k]){
arraymaxi=k;
}
}
printf("maxi: %d\n", array[arraymaxi]); //end of checking the correct result, will be deleted
int l; //check the max numbers from the forks, will be deleted
for(l=0;l<10;l++){
printf("from the pipe max: %d\n", arraymax[l]);
}
int h; //search the true max from the 10 numbers
int truemaxindex=0;
for(h=0;h<10;h++){
if(arraymax[truemaxindex]<arraymax[h]){
truemaxindex=h;
}
}
printf("the final max: %d\n", arraymax[truemaxindex]);
return 0;
每次调用 fork
后,您都在等待刚刚创建的进程完成。您应该在等待任何进程之前创建所有进程。
您还有其他一些错误。您在循环的每次传递中关闭 fd[1]
,但随后尝试在循环的下一次传递中读取它。如果你愿意,你可以对每个 child 使用不同的管道,但是如果你要对所有 children 使用相同的管道,你需要保持管道打开直到你读完所有回复。
另外,不要在children中调用exit
!当 parent 的 atexit
处理程序不止一次 运行 时,这可能会导致非常令人惊讶的行为。您可以使用 _exit
.
在父级的 for 循环中,您在第一次迭代时关闭了管道的读取端,因此第二次迭代中的读取失败。将闭包移出循环。 (并检查错误!!)
我必须编写一个 c 程序 (linux) 来搜索数组中的最大值,同时使用 10 child。数组大小为 1000。每个 child 从 100 个数字中搜索最大值。 parent 应该在管道上得到结果。我的代码不能完美运行。主要问题是管道。 parent 只得到第一个最大值。第二个问题是 childs arent 运行 在同一时间(不是一个大问题,但可能有人可以告诉我哪里出了问题) 我为我的代码做了一些笔记,但我的英语太糟糕了。 我希望我以正确的形式复制源代码。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int array[1000]; //main array
int i;
srand (time(NULL)); //for not the same numbers
for(i=0;i<1000;i++){ //array upload
array[i]= ( rand() % (10000+1) ); //from 1 to 10000 random numbers
}
int searchminindex; //search index, 0-100,100-200, etc
int searchmaxindex;
int threads = 10; //number of threads
int fd[2];
pipe(fd);
int arraymax[10]; //the 10 max numbers
for(i=0;i<threads;i++){ //for 10 threads
if(fork() == 0){
close(fd[0]); //close reading
searchminindex=i*100; //number of thread * arraysize/threadsnumber
searchmaxindex=(i+1)*100;
int maxindex=searchminindex; //it will store the max number index, start from the search min index
int j;
for(j=searchminindex;j<searchmaxindex;j++){ //max search
if(array[maxindex]<array[j]){
maxindex=j;
}
}
write(fd[1], &array[maxindex], sizeof(array[maxindex])); //write the max number into the pipe
printf("max: %d\n", array[maxindex]); //that for check the result, will be deleted from the final code
close(fd[1]); //close write
wait(NULL);
exit(0);
}
else{
wait(NULL);
close(fd[1]); //close write
read(fd[0], &arraymax[i], sizeof(arraymax[i])); //read the max and store in arraymax[]
close(fd[0]); //close read
printf("the read max from the pipe: %d\n", arraymax[i]); //that for check the result, will be deleted from the final code
}
}
int arraymaxi=0; //it is search the max in the main array for check the result, will be deleted
int k;
for(k=0;k<1000;k++){
if(array[arraymaxi]<array[k]){
arraymaxi=k;
}
}
printf("maxi: %d\n", array[arraymaxi]); //end of checking the correct result, will be deleted
int l; //check the max numbers from the forks, will be deleted
for(l=0;l<10;l++){
printf("from the pipe max: %d\n", arraymax[l]);
}
int h; //search the true max from the 10 numbers
int truemaxindex=0;
for(h=0;h<10;h++){
if(arraymax[truemaxindex]<arraymax[h]){
truemaxindex=h;
}
}
printf("the final max: %d\n", arraymax[truemaxindex]);
return 0;
每次调用 fork
后,您都在等待刚刚创建的进程完成。您应该在等待任何进程之前创建所有进程。
您还有其他一些错误。您在循环的每次传递中关闭 fd[1]
,但随后尝试在循环的下一次传递中读取它。如果你愿意,你可以对每个 child 使用不同的管道,但是如果你要对所有 children 使用相同的管道,你需要保持管道打开直到你读完所有回复。
另外,不要在children中调用exit
!当 parent 的 atexit
处理程序不止一次 运行 时,这可能会导致非常令人惊讶的行为。您可以使用 _exit
.
在父级的 for 循环中,您在第一次迭代时关闭了管道的读取端,因此第二次迭代中的读取失败。将闭包移出循环。 (并检查错误!!)