4

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 ??

Here is the link of the problem we are talking about multi-process part of the problem which is part 2

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

Burak Dede
  • 3,725
  • 5
  • 40
  • 53

4 Answers4

2

Assuming you're calculating them in a "simple" way (i.e. 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. 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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • There are about 100 of them representable in 64bits, so there's no reason to use parallel calculations. – ruslik Oct 18 '10 at 11:30
  • I know it is not a good solution for this problem , but it is important to show that multi thread part is better than this solution, problem also have multi-threaded written part.It is just comparison – Burak Dede Oct 18 '10 at 11:31
  • 2
    @Burak an algorithm has parts that can be parallelized and parts that cannot. This one cannot be. – ruslik Oct 18 '10 at 11:35
  • @Ruslik , I know it is not a good candidate for multiprocessing but it is a homework so i have no choice other than implementing it but i agree with you – Burak Dede Oct 18 '10 at 11:37
  • @Burak: Picking an example which is doesn't parallelize nicely in the first place is a really bad idea if you're trying to compare multiple threads vs multiple processes. It's like asking whether apples or oranges are better for building houses with. – Jon Skeet Oct 18 '10 at 11:38
  • @Burak then it's OK if you have performance issues. You've proved that for this particular algorithm the serial version is the best one. – ruslik Oct 18 '10 at 11:43
  • yes serial version is way better than multiprocess one , i just wonder if any other solution different than using fork inside for to do it. – Burak Dede Oct 18 '10 at 11:48
  • 2
    @Jon Skeet, apples, hands down! :) – Prof. Falken Oct 18 '10 at 11:49
2

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.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
1

You might want to look at this article:

http://cgi.cse.unsw.edu.au/~dons/blog/2007/11/29

There are more ideas here:

http://www.haskell.org/haskellwiki/The_Fibonacci_sequence

stusmith
  • 14,003
  • 7
  • 56
  • 89
0

probably this isn't solving your problem, but here is a trivial way to calculate the fibonacci numbers of a given range

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

  • i am not using recursive algorithm in this manner and algorithm is not the issue here performance is ,i know the fibonacci algorithm – Burak Dede Oct 18 '10 at 12:13