ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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
...
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
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...
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.
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
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??
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.