LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2007, 07:20 PM   #1
BrokenFighter
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Rep: Reputation: 0
Fibonacci with fork


Hi all...

I have an assignment about calculating fibonacci with using fork(). We will create a file that records the calculated fibonacci numbers. Firstly, the first two number(0 and 1) of fibonacci series will be written into that file manually...And then we will create a child process with using fork and our program will read the first two lines of file. We will calculate the 3rd fibonacci number. Then we will write the calculated fibonacci number as a new line at the end of file. After that we will create a new child and that will take the last two lines of file and we will calculate the fibonacci number with using these two lines. After that calculation we will write the calculated fibonacci number as a new line at the end of file. The program will continue in this way until the desired fibonacci number is calculated.

Is there any algorithm that can you advise me?? Any advices are welcome...

Thank you
 
Old 03-04-2007, 08:12 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Sounds like you've already described the algorithm pretty well.
 
Old 03-04-2007, 08:16 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
whether you are just reiterating the assignment sheet, or if you have already put some thought into it, it seems you already have a basic 'algorithm' to solve it.

get some paper, and use pseudocode (short-form english sentences, similar to what you already have) to write your algorithm. structure it the same way you would if you were writing a source-code file on your computer. once you have your pseudocode its much easier to convert that to code.. and the bonus is that it can be used for any programming language.

example:
Code:
create and open file "myfile.txt"
writeline "0" to "myfile.txt"
writeline "1" to "myfile.txt"
create child
...
hope it helps a little.
 
Old 03-04-2007, 09:00 PM   #4
BrokenFighter
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Original Poster
Rep: Reputation: 0
First of all thanks for your replies...

For me, the main problem is fork...I know the way of calculating a fibonacci number but until this time, we've done it recursively without using fork...I don't know how can I use the created child to help me about solving this problem...
Code:
Get 2 lines from myfile.txt
Use child   (But how?)
Calculate fibonacci
Write the result to myfile.txt
 
Old 03-04-2007, 09:14 PM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by BrokenFighter
Hi all...

I have an assignment about calculating fibonacci with using fork(). We will create a file that records the calculated fibonacci numbers. Firstly, the first two number(0 and 1) of fibonacci series will be written into that file manually...And then we will create a child process with using fork and our program will read the first two lines of file. We will calculate the 3rd fibonacci number. Then we will write the calculated fibonacci number as a new line at the end of file. After that we will create a new child and that will take the last two lines of file and we will calculate the fibonacci number with using these two lines. After that calculation we will write the calculated fibonacci number as a new line at the end of file. The program will continue in this way until the desired fibonacci number is calculated.

Is there any algorithm that can you advise me?? Any advices are welcome...

Thank you
Code:
#! /bin/bash

fork() ## Fibonacci Object Reader and Kalculator
{
    set -- $( < "$fibfile" )
    shift "$(( $# - 2 ))"
    fibnum=$(( $1 + $2 ))
    printf "%d\n" "$fibnum" >> "$fibfile"
}

usage()
{
    printf "USAGE: %s FILE NUM\n" "${0##*/}"
}

[ $# -lt 2 ] && { usage >&2; exit 1; }

fibfile=$1
desired=$2
fibnum=1
printf "%s\n" 0 1 > "$fibfile"

while [ $fibnum -lt $desired ]
do
  fork
done
 
Old 03-04-2007, 09:23 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
here is an example of fork that one of my professors gave us: (i added the comments)
Code:
#include <unistd.h>
int main(int argc, char *argv[]){
   int pid;

   printf("Only one process\n");
   pid = fork();

   // right now, there (probably) is 2 processes executing this same code.
   if(pid == -1){
      perror("impossible to fork");
      exit(1);
   }

   // we KNOW now there are 2 processes executing the following if statement.

   if(pid > 0) // anything inside this block will ONLY be executed by the PARENT process.
      printf("I am the parent, pid=%d\n", getpid());
   else
      if(pid == 0) // anything inside this block will ONLY be executed by the CHILD process.
         printf("I am the child, pid=%d\n", getpid());

   // BOTH processes will execute everything else
   exit(0);
}
fork() takes no parameters and returns an integer (two integers, if successful). if the value returned is -1, then fork failed to create a child process. if the value returned is greater than 0, then the process checking this value is the parent process. if the returned value is 0, then you are currently inside the child process. im sure it sounds confusing but it really isnt too difficult once you do it afew times.
i hope that helped.. if not then search around or check the man page of fork.

hopefully this helps a little.

edit: thanks for the catch graemef

Last edited by nadroj; 03-04-2007 at 09:53 PM.
 
Old 03-04-2007, 09:42 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Just to clarify the fork process. When a process calls fork it creates a clone of itself, so there is now an extra process running, when the new process is created they are both in the same state. The original process is referred to as the parent and the new process is referred to as the child process.

In the parent process the return value from fork will be the process ID of the child, which is going to be greater than one. In the child process the return value from fork will be zero. Because of this it is always possible to know if you are a child or a parent process.

nadroj - you may want to edit your description to: if the value returned is greater than 0, then the process checking this value is the parent process
 
Old 03-04-2007, 10:32 PM   #8
BrokenFighter
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Original Poster
Rep: Reputation: 0
I got the point about fork...Now I can start coding...

Thank you so so much, nadroj, cfaj and graemef...
 
Old 03-10-2007, 08:53 AM   #9
BrokenFighter
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Original Poster
Rep: Reputation: 0
I wrote the code...If somebody need this, I can send...

Thank you again that I learned fork with help of you...

My new assignment is about fibonacci again...We will write a c code and the format of the program will be the same...But this time, we will use POSIX threads instead of fork...

Can you give some information about POSIX threads and their usage??

Thank you all...
 
Old 03-10-2007, 08:32 PM   #10
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Read this paper:

http://www.sun.com/software/whitepap...ultithread.pdf

It tells you more about threading you want to know, but it is an excellent paper.

jlinkels
 
Old 03-10-2007, 09:41 PM   #11
BrokenFighter
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Original Poster
Rep: Reputation: 0
It's a nice document but I think it's so detailed...Could you suggest any other practical documents??

Regards...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Big O notation - fibonacci sequence debiant Programming 13 09-09-2006 11:10 PM
about fork() kpachopoulos Programming 2 02-14-2005 12:24 AM
more on fork() feetyouwell Programming 6 09-17-2004 11:18 AM
need perl help calculating fibonacci numbers WorldBuilder Programming 5 12-17-2003 01:41 AM
Fork again Avatar33 Programming 13 08-22-2003 01:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration