简体   繁体   English

以多进程方式计算斐波那契数?

[英]Calculate the fibonacci numbers in multiprocess way?

I am writing multi process fibonacci number calculator , I have a file that keeps track of the fibonacci numbers , first process open the file and write first fibonacci numbers (0 and 1 ), then do fork and its child process read the last two numbers add them up and write the next into file and close the file and fork again this process continue like that with forking and child adding up numbers and writing calculated number into file, using fork inside the for not a good solution neither recursive call,is there any suggesstion for problem ?? 我正在编写多进程斐波那契数字计算器,我有一个跟踪斐波纳契数字的文件,首先打开文件并编写第一个斐波纳契数字(0和1),然后做fork和它的子进程读取最后两个数字加法他们起来并将下一个写入文件并关闭文件并再次分叉这个过程继续像forking和child将数字加起来并将计算出的数字写入文件,使用fork里面的for not a good solution既不递归调用,有没有对问题的建议?

Here is the link of the problem we are talking about multi-process part of the problem which is part 2 以下是我们所讨论的问题的多个过程部分问题的链接,这是第2部分

http://cse.yeditepe.edu.tr/~sbaydere/fall2010/cse331/files/assignments/F10A1.pdf http://cse.yeditepe.edu.tr/~sbaydere/fall2010/cse331/files/assignments/F10A1.pdf

Assuming you're calculating them in a "simple" way (ie without using a cunning formula), I don't think it's a good candidate for parallel processing at all. 假设你是以“简单”的方式计算它们(即没有使用狡猾的公式),我认为它根本不适合并行处理。

It's easy to come up with an O(n) solution, but each result depends on the previous one, so it's inherently tricky to parallelize. 提出O(n)解决方案很容易,但每个结果都取决于前一个,因此并行化本身就很棘手。 I can't see any benefit in your current parallel approach, as after each process has finished its own job and forked a child to get the next number, it's basically done... so you might as well do the work of the forked child in the existing process. 我目前的并行方法看不出任何好处,因为在每个流程完成自己的工作并分叉一个孩子来获得下一个数字之后,基本上已经完成了......所以你不妨做分叉孩子的工作在现有的过程中。

Fibonacci number calculation is a really strange idea to go multiprocess. 进行多进程的斐波纳契数计算是一个非常奇怪的想法。 Indeed, to calculate a number, you do need to know the previous two. 实际上,要计算一个数字,你需要知道前两个。 Multiple processes cannot calculate other numbers but the next one, and only the next one. 多个进程不能计算其他数字,而是下一个数字,只能计算下一个数字。 Multiple processes will all calculate the next Fibonacci number. 多个进程都将计算下一个斐波纳契数。 Anyway, you'll double check. 无论如何,你会仔细检查。

可能这不是解决你的问题,但这是计算给定范围的斐波纳契数的一种简单方法

int fibo(int n) { return (n<=2)?n:(fibo(n-1))+(fibo(n-2)); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM