LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2010, 07:34 PM   #1
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Rep: Reputation: 0
Using fork() to create a binary tree


Hi everyone,

Im new to this forum and new to programming. I am working on a project where I need to use the C language to generate a tree of processes. I understand how fork() works but I cant seem to get fork() to create two children from one parent and then have the two children create two more children.

Right now what i am seeing is a chain...where the parent creates one child...and that child creates another ONE child..etc.

Any suggestions...

Here is what I have so far:

for (i=0; i<n;i++){
if (childpid = fork()) break;

}
if (childpid == -1){
perror ("\n The fork failed\n");
exit(1);
}
printf("\n %d: process ID:%6ld parent ID:%6ld child ID:%6ld \n\n",i, (long)getpid(), (long)getppid(), (long)childpid);
 
Old 09-27-2010, 12:39 AM   #2
linux_hy
Member
 
Registered: Oct 2006
Posts: 66

Rep: Reputation: -2
try to the following code maybe help for you
but you need to modify this code for your requirement
and this code need to debug,it just a sample thinking of create process with recursion
layer is the count of child process
Code:
void process_tree(int layer)
{
   if(layer<=0) return;
   switch(fork()){
     case 0:
         process_tree(layer-1);
         break;
     case -1:
         fprintf(stderr,"fork failed\n");
         break;
     default:
         break;
   }
}

Last edited by linux_hy; 09-27-2010 at 12:40 AM.
 
Old 09-27-2010, 07:17 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
I think you want nested loops:

Outer loop iterates through the desired depth of the tree.
..Inner loop iterates through the desired number of children per parent.
....fork
....break out of inner loop if this process is the child
..break out of outer loop if the inner loop completed and this process is still the parent.

Notice also, that you will want an array of child pids since you want more than one child per parent.
 
Old 09-28-2010, 02:27 AM   #4
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Original Poster
Rep: Reputation: 0
Hi again,

I got it to run. I used a switch statement nested inside a while loop. It works great! Thanks for the tips.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
C Binary Search Tree bnixon10 Programming 4 04-05-2008 10:46 PM
Binary search tree in C Zeno McDohl Programming 3 01-27-2008 06:07 PM
Binary tree in C spank Programming 20 04-25-2006 11:45 AM
representation of binary tree using array sajith Programming 3 10-06-2005 11:59 PM
Printing a binary tree in c? JMC Programming 5 09-26-2003 12:02 PM


All times are GMT -5. The time now is 05:33 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration