Operating System - HP-UX
1819897 Members
2880 Online
109607 Solutions
New Discussion юеВ

Re: how to create many child processes using fork()

 
SOLVED
Go to solution

how to create many child processes using fork()

I need to write a c program that will calculate fibonacci numbers using fork().

Parent process will create a txt like:

0
1

then close the file and fork().

From this point on, the child process will open the file, add the 0 and 1, and append the result to the text, closing it. Then it will fork and create it's child.

So this goes on for each child creating its child, doing it's part for the fib. until the desired fib. number. I hope it is understood.

* Every process has to wait until their subprocess or child termanites.


My problem is that I am not sure how to create
sequantialy childs and arrange their wait for terminating.

For example for calculating fibonacci number 4, I need the main parent process (for 0 and 1), child_1(child of parent, for 0+1=1), child_2(child of child_1, for 1+1=2), child_3(child of child_2, for 1+2=3). Since fibonacci number 4 equals to 3, child_3 calculates my answer. Then child_3 will terminate, following child_2, child_1 and last the main parent.

Do you have any idea, at least an advice about creating these children?
9 REPLIES 9
Steven Schweda
Honored Contributor

Re: how to create many child processes using fork()

> I need to write a c program that will
> calculate fibonacci numbers using fork().

Why? Homework assignment? (Or is it really
work-related?)

> Do you have any idea, at least an advice
> about creating these children?

Attend a computer programming class, and pay
attention while there?
A. Clay Stephenson
Acclaimed Contributor

Re: how to create many child processes using fork()

I could easily write this for you but that wouldn't be doing you any favor --- and I hope nobody else does you harm as well.

Using fork() is rather easy.

pid = fork();
if (pid >= 0)
{
if (pid == 0)
{
/* child process */
}
else
{
printf("I'm the parent of %d\n",pid);
}
}
else
{
fprintf(stderr,"Forked failed errno = %d\n",errno);
}
-----------------------------------------

The other key system call is waitpid(); it will pause a process until a particular process terminates. You will also need to use getpid() to get the current process id.

Man fork,waitpid, and getpid for more details.

Hint: There is no reason to do this in C or C++. You can code this all but identically and equally valid in Perl and use the same fork(), getpid(), and waitpid() functions.
If it ain't broke, I can fix that.

Re: how to create many child processes using fork()

I have a homework.

I managed to do creating multiple children from a single parent but still can't create children as a link.

parent -> child_1 -> child_2 -> child_3...

I don't need the code, just an idea or a piece of algorithm...
A. Clay Stephenson
Acclaimed Contributor

Re: how to create many child processes using fork()

Final Hint: After you fork, the child process will do an exec() and call itself if your limit has not been reached.

You can pass data from parent to child as arguments to main();

If it ain't broke, I can fix that.
Steven Schweda
Honored Contributor

Re: how to create many child processes using fork()

"Recursive" is an idea.
Dennis Handly
Acclaimed Contributor

Re: how to create many child processes using fork()

>Then it will fork and create its child.

(You do realize for this case, it is a bad thing to do in real life? :-)

>* Every process has to wait until their subprocess or child terminates.

Here you could just exec, since this is just tail recursion.

I hope you limit the amount of processes you are creating. And you may need to make sure that fork does work, since you may exceed the kernel limits for large numbers.

>Clay: You will also need to use getpid()

Why do you need to know who you are? You just wait for your child.

>Clay: After you fork, the child process will do an exec()

Why would you need to do an exec? The limit can be picked up from either a global or local.

>Steven: "Recursive" is an idea.

It isn't in this case, iteration is best. :-)

Re: how to create many child processes using fork()

>I hope you limit the amount of processes you are creating. And you may need to make sure that fork does work, since you may exceed the kernel limits for large numbers.

Yes, user enters the limit as a parameter.

I wrote an recursive algorithm and will code it tomorrow. Didn't use the exec though.

I know that after fork, if exec is called, the new process handles the job and terminates. And exec runs a executable program, so what should I do now, write an another program which is recursively forks and handles the task?

like program x:
-----
....
.... //calculating fibonacci, arranging
.... text...

if (!condition)
{
pid=fork();
if(pid==0)
exec(program x....);
else
wait(null);
}
exit(0);
------

I wonder if it works this way... Anyway, I will try tomorrow...
Dennis Handly
Acclaimed Contributor
Solution

Re: how to create many child processes using fork()

>I wrote an recursive algorithm . Didn't use the exec though.

The recursion is only in that you fork and let the child handle it. And you don't need exec.

>I know that after fork, if exec is called, the new process handles the job and terminates.

There is no real need to do an exec since it would most likely invoke yourself.

>write an another program which is recursively forks and handles the task?

Why bother, just fork:
int current, limit;
void control_forks() {
int status;
pid_t pid, pid2;
while (current < limit) {
pid = fork();
if (pid==(pid_t)-1) { handle error case}
if (pid == 0 {
fiddle_with_file();
++current;
} else {
pid2 = wait(&status);
handle error case2
}
}

if (current > 2) exit(0);
}

Note adjustments on the current and limit checks may need to be done. I assume that current starts at 2, the number of initial lines in the file. (I may be off by one.)
Dennis Handly
Acclaimed Contributor

Re: how to create many child processes using fork()

Oops, the parent case needs to break out:
} else {
    pid2 = wait(&status);
    handle error case2
    break;
}