Solved

linux, create a tree of processes

Posted on 2011-03-24
7
384 Views
Last Modified: 2012-05-11
I have the following code, trying to create a tree of processes (every child generates a new child), with a given depth, but the program always creates 2 childs (depth=2)
I'm using cygwin.
#include<stdio.h>
#include<unistd.h>

int main(){
    
    int pid,pid2;
    int depth=3;
    int i;
    pid=fork();
    for(i=1; i<=depth; i++){
             
             if(pid==0){//child
               printf("\nchild %d\n pid: %d ppid: %d\n",i,getpid(),getppid());
               pid2=fork();
               if(pid2==0){printf("\nchild %d\n pid: %d ppid: %d\n",i,getpid(),getppid());exit(0);}
                 wait(0);
                 exit(0);
             }
            
    }//end for 
    if (pid>0){//parent
               printf("\nparent pid: %d\n",getpid());
               wait(0);
             } 
    return 0;
}

Open in new window

0
Comment
Question by:dimi67
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:m_walker
ID: 35205673
on a quick look... from memory fork will execute from the point of the fork statment/call.

the loop

  for(i=1; i<=depth; i++){
             
             if(pid==0){//child
               printf("\nchild %d\n pid: %d ppid: %d\n",i,getpid(),getppid());
               pid2=fork();
               if(pid2==0){printf("\nchild %d\n pid: %d ppid: %d\n",i,getpid(),getppid());exit(0);}
                 wait(0);

// This exit will exit the loop/code, thus not going back for 2 and 3.
                 exit(0);
             }
           
    }//end for
0
 

Author Comment

by:dimi67
ID: 35205740
I changed the code, but I'still taking 2 levels with some children.
Any idea?
0
 
LVL 4

Expert Comment

by:m_walker
ID: 35205770
On linux (gcc) my output

child 1
 pid: 7192 ppid: 1

child 1
 pid: 7193 ppid: 7192

child 2
 pid: 7192 ppid: 1

child 2
 pid: 7194 ppid: 7192

child 3
 pid: 7192 ppid: 1

child 3
 pid: 7195 ppid: 7192


code
Note: a made a few quick modes to speed up the test like dropping the wait(0), but it does hang from time to time.

#include<stdio.h>
#include <stdlib.h>
#include<unistd.h>

int main(){

    int pid,pid2;
    int depth=3;
    int i;
    pid=fork();
    for(i=1; i<=depth; i++){

             if(pid==0){//child
               printf("\nchild %d\n pid: %d ppid: %d\n",i,getpid(),getppid());
               pid2=fork();
               if(pid2==0){printf("\nchild %d\n pid: %d ppid: %d\n",i,getpid(),getppid());exit(0);}
//                 wait(0);
//                 exit(0);
             }

    }//end for
//    if (pid>0){//parent
 //              printf("\nparent pid: %d\n",getpid());
  //             wait(0);
 //            }
    return 0;
}

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:m_walker
ID: 35205801

with the exit in the "if (pid2==0) removed

child 1
 pid: 7242 ppid: 7241

child 1
 pid: 7243 ppid: 7242

child 2
 pid: 7243 ppid: 7242

child 2
 pid: 7244 ppid: 7243

child 3
 pid: 7244 ppid: 7243

child 3
 pid: 7245 ppid: 7244

child 3
 pid: 7243 ppid: 7242

child 3
 pid: 7246 ppid: 7243

child 2
 pid: 7242 ppid: 7241

child 2
 pid: 7247 ppid: 7242

child 3
 pid: 7247 ppid: 7242

child 3
 pid: 7248 ppid: 7247

child 3
 pid: 7242 ppid: 7241

child 3
 pid: 7249 ppid: 7242
0
 
LVL 4

Expert Comment

by:m_walker
ID: 35205884
I dont think I am thinking about what you are trying to do correctly.

"I have the following code, trying to create a tree of processes (every child generates a new child), with a given depth, but the program always creates 2 childs (depth=2)"

Are you tring to get
P of Loop = 1 -> Creates P1.1 P1.2 P1.3
P of Loop = 2 -> Creates P2.1 P2.2 P2.3
P of Loop = 3 -> Creates P3.1 P3.2 P3.3

or do you want
                      P
        p            p           p
    p  p  p    p  p  p   p  p  p
(hope the tree put line keeps it format.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 35206019
   for(i=1; i<=depth; i++){
             pid=fork();
             if(pid==0){//child                                                            
               printf("\nchild %d\n pid: %d ppid: %d\n",i,getpid(),getppid());
             }else if (pid>0){//parent                                                      
               printf("\nparent pid: %d\n",getpid());
               wait(0);
               exit(0);
             }
    }
0
 

Author Closing Comment

by:dimi67
ID: 35213437
ozo is genius!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question