Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

linux, create a tree of processes

Posted on 2011-03-24
7
Medium Priority
?
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

704 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