Solved

linux, create a tree of processes

Posted on 2011-03-24
7
365 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now