- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to create many child processes using fork(...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 11:05 AM
тАО03-06-2007 11:05 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 12:21 PM
тАО03-06-2007 12:21 PM
Re: how to create many child processes using fork()
> 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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 12:29 PM
тАО03-06-2007 12:29 PM
Re: how to create many child processes using fork()
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 12:32 PM
тАО03-06-2007 12:32 PM
Re: how to create many child processes using fork()
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 12:37 PM
тАО03-06-2007 12:37 PM
Re: how to create many child processes using fork()
You can pass data from parent to child as arguments to main();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 12:40 PM
тАО03-06-2007 12:40 PM
Re: how to create many child processes using fork()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 02:06 PM
тАО03-06-2007 02:06 PM
Re: how to create many child processes using fork()
(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. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 02:42 PM
тАО03-06-2007 02:42 PM
Re: how to create many child processes using fork()
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 03:57 PM
тАО03-06-2007 03:57 PM
SolutionThe 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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-06-2007 04:04 PM - edited тАО10-01-2011 08:12 PM
тАО03-06-2007 04:04 PM - edited тАО10-01-2011 08:12 PM
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;
}