Solved

linux, create a tree of processes

Posted on 2011-03-24
7
381 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

808 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