Solved

linux, create a tree of processes

Posted on 2011-03-24
7
376 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reinstall 1 78
UNiX Script filesystem space usage 19 74
building a service to accessed via telnet client, want colors ? 4 88
unix example issues 18 90
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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 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…

773 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