在C叉搜索 [英] Searching with fork in C

查看:112
本文介绍了在C叉搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该越来越舒适叉了,我看到了一个锻炼的说要用叉子调用搜索索引从0到15我们假设每个进程只能做两件事情的数组。 ..(1)是检查,看看是否阵列是长度为1,和(2)比较的阵列的数目的单个元素被搜索。基本上我传递一个数值,它应该做叉的数量有限,并返回该数字的索引。这里是我的code ..

 的#define MAXINDEX 16INT forkSearch(int类型的[],诠释搜索,诠释开始,诠释完){
  如果(开始==结束){
    如果(*(A + END)==搜索){
      返回结束;
    }
  }
  其他{
    将为pid_t孩子= fork()的;
    如果(孩子== 0)返回forkSearch(一,搜索,开始,结束/ 2);
    否则返回forkSearch(一个,搜索,(START +端)/ 2,端);
  }
}INT主(INT ARGC,CHAR *的argv []){
  INT searchArray [MAXINDEX] = {1,12,11,5,10,6,4,9,13,2,8,14,如图3所示,\\
                               15,7};
  的printf(应该是1。12 =%d个\\ n个指数,forkSearch(searchArray,
                                                       12,0,MAXINDEX));
  返回0;
}

在此迅速爆炸计划的回归似乎一切都可以是1,10,11,或13为什么没有这方面的工作像它应该。


解决方案

 如果(孩子== 0)返回forkSearch(一,搜索,开始,结束/ 2);

这是错误的结束在那里,它应该是(启动+前端)/ 2 ,并开始索引在右半部分搜索应该是(开始+结束)/ 2 + 1 。否则,如果右半边是(开始+结束)/ 2 ..结束,当末开始== + 1 启动的递归调用是老启动价值,你有一个无限循环。

您计划未定义的行为,因为

  INT forkSearch(int类型的[],诠释搜索,诠释开始,诠释完){
  如果(开始==结束){
    如果(*(A + END)==搜索){
      返回结束;
    }
  }

不返回值,如果开始==结束,但 *(A + END)!=搜索。添加退出(0); 如果退出未找到目标进程

  INT searchArray [MAXINDEX] = {...};forkSearch(searchArray,12,0,MAXINDEX)

将导致超出边界访问在 searchArray [MAXINDEX] ,也未定义的行为。

I'm supposed to be getting comfortable with fork, and I saw an exercise that said to use a fork call to search an array indexed from 0 to 15. We're to assume that each process can only do two things...(1) is to check to see if an array is length 1, and (2) compare a single element of an array to the number were searching for. Basically i pass it a number, and its supposed to do a finite number of forks and return the index of that number. Here's my code..

#define MAXINDEX 16

int forkSearch(int a[], int search, int start, int end){
  if(start == end){
    if(*(a + end) == search){
      return end;
    }
  } 
  else{
    pid_t child = fork();
    if(child == 0) return forkSearch(a, search, start, end/2);
    else return forkSearch(a, search, (start + end)/2, end);
  }
}

int main(int argc, char* argv[]){
  int searchArray[MAXINDEX] = {1, 12, 11, 5, 10, 6, 4, 9, 13, 2, 8, 14, 3,\
                               15, 7};
  printf("Should be 1. Index of 12 = %d\n", forkSearch(searchArray,
                                                       12, 0, MAXINDEX));
  return 0;
} 

Everything in the return of this quickly exploding program seems to be either 1, 10, 11, or 13. Why isn't this working like it should.

解决方案

if(child == 0) return forkSearch(a, search, start, end/2);

That's the wrong end there, it should be (start+end)/2, and the start index of the search in the right half should be (start+end)/2 + 1. Otherwise, if the right half is (start+end)/2 .. end, when end == start+1, the start for the recursive call is the old start value and you have an infinite loop.

Your programme has undefined behaviour because

int forkSearch(int a[], int search, int start, int end){
  if(start == end){
    if(*(a + end) == search){
      return end;
    }
  }

doesn't return a value if start == end, but *(a+end) != search. Add an exit(0); after the inner if to exit the processes that didn't find the target.

int searchArray[MAXINDEX] = {...};

forkSearch(searchArray, 12, 0, MAXINDEX)

will lead to an out-of-bounds access at searchArray[MAXINDEX], also undefined behaviour.

这篇关于在C叉搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆