creating a tree of processes
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > creating a tree of processes




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    creating a tree of processes  
Jani Yusef


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-24-04 07:50 AM

I origially posted this in another group but this group is more
appropriate so I am reposting it here.
I am trying to create a tree of processes. The code below works,
however, my initial call to the procTree function is passed depth-2. I
arrived at this via trial and error and am not sure why this works?
Can anyone spot what I am doing wrong? Or is that right for some reason
I don't understand?


int i=-1;
int pid;
int numberOfProcesses=-1;

void procTree(int depth)
{
for(i=0;i<numberOfProcesses;i=i+1){
int socko=socket(2,1,0);
struct sockaddr_in addr;
addr.sin_family=2;
addr.sin_addr.s_addr=htonl((127<<24)+1);
addr.sin_port=0;
bind(socko,(struct sockaddr *)&addr,16);
if((pid=fork())< 0){
printf("Fork failed");
exit(1);
}
if(pid==0){
//then in child
if(depth>0){
procTree(depth-1);
}

// do other child stuff
return;
}
if(pid){
//then in parent
}
}
}

int main(int argc, char *argv[]){
char input[MAXBUFFER];//create a character array to hold input
from the user
char output[MAXBUFFER];//create a character array to hold output
int depth=atoi(argv[2]);
numberOfProcesses=atoi(argv[1]);

procTree(depth-2);

read(0,input,MAXBUFFER);
sprintf(output,"%s",input);
write(1,output,strlen(output));
}

What I really really want to know is why I need to set depth to depth-2
in my fist call to procTree? I just iteratated the value a few times to
find one that works but I have no idea why!!
I kept looking at ps output until the tree looked correct to me.
i.e.
$ ps -ef | grep "2 2"
jani     2244  1979  0 15:34 ttyp1    00:00:00 ./a.out 2 2
jani     2245  2244  0 15:34 ttyp1    00:00:00 ./a.out 2 2
jani     2246  2244  0 15:34 ttyp1    00:00:00 ./a.out 2






[ Post a follow-up to this message ]



    Re: creating a tree of processes  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-24-04 07:50 AM

Jani Yusef <jani@persian.com> writes:

> I origially posted this in another group but this group is more
> appropriate so I am reposting it here.
> I am trying to create a tree of processes. The code below works,
> however, my initial call to the procTree function is passed depth-2. I
> arrived at this via trial and error and am not sure why this works?
> Can anyone spot what I am doing wrong? Or is that right for some
> reason I don't understand?
> [...]
> void procTree(int depth)
>         {
>             for(i=0;i<numberOfProcesses;i=i+1){
> [...]
>                 if((pid=fork())< 0){
>                     printf("Fork failed");
>                     exit(1);
>                 }
>                 if(pid==0){
>                     //then in child
>                     if(depth>0){
>                       procTree(depth-1);
>                     }
>
>                     // do other child stuff
>                     return;
>                 }
> [...]
> What I really really want to know is why I need to set depth to depth-2
> in my fist call to procTree? I just iteratated the value a few times to
> find one that works but I have no idea why!!

That's just a problem of recursion and counting.

First, you have already one root process, so your minimum depth is 1,
not 0, and when you set depth=1, you don't want to fork any
child. That means, you should test the depth before trying to fork!


void procTree(int depth){
if(1<depth){
for(i=0;i<number_of_children;i++){
int pid=fork();
switch(pid){
case 0: /* in child */
depth--;
procTree(depth);
return;
case -1:
perror("fork");
return;
default: /* in parent */
break; }}}}


int main(int argc,char** argv){
/* ... */
int depth=argv[2]?atoi(argv[2]):1;
procTree(depth);
/* ... */ }


(If you use procps ps, you can use ps -f to get a display of your
process tree (or use pstree)).

--
__Pascal Bourguignon__                     http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.





[ Post a follow-up to this message ]



    Re: creating a tree of processes  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-24-04 10:51 PM



Jani Yusef wrote:
> I origially posted this in another group but this group is more
> appropriate so I am reposting it here.
> I am trying to create a tree of processes. The code below works,
> however, my initial call to the procTree function is passed depth-2. I
> arrived at this via trial and error and am not sure why this works?
> Can anyone spot what I am doing wrong? Or is that right for some reason
> I don't understand?
>
>
> int i=-1;
> int pid;
> int numberOfProcesses=-1;
>
> void procTree(int depth)
>        {
>            for(i=0;i<numberOfProcesses;i=i+1){
>                int socko=socket(2,1,0);
>                struct sockaddr_in addr;
>                addr.sin_family=2;
>                addr.sin_addr.s_addr=htonl((127<<24)+1);
>                addr.sin_port=0;
>                bind(socko,(struct sockaddr *)&addr,16);
>                if((pid=fork())< 0){
>                    printf("Fork failed");
>                    exit(1);
>                }
>                if(pid==0){
>                    //then in child
>                    if(depth>0){
>                      procTree(depth-1);
>                    }
>
>                    // do other child stuff
>                    return;
>                }
>                if(pid){
>                    //then in parent
>                }
>            }
>        }
>
> int main(int argc, char *argv[]){
>          char input[MAXBUFFER];//create a character array to hold inpu
t
> from the user
>          char output[MAXBUFFER];//create a character array to hold out
put
>          int depth=atoi(argv[2]);
>          numberOfProcesses=atoi(argv[1]);
>
>          procTree(depth-2);
>
>          read(0,input,MAXBUFFER);
>          sprintf(output,"%s",input);
>          write(1,output,strlen(output));
> }
>
> What I really really want to know is why I need to set depth to depth-2
> in my fist call to procTree? I just iteratated the value a few times to
> find one that works but I have no idea why!!
> I kept looking at ps output until the tree looked correct to me.
> i.e.
> $ ps -ef | grep "2 2"
> jani     2244  1979  0 15:34 ttyp1    00:00:00 ./a.out 2 2
> jani     2245  2244  0 15:34 ttyp1    00:00:00 ./a.out 2 2
> jani     2246  2244  0 15:34 ttyp1    00:00:00 ./a.out 2
>

In your code, you do not modify your depth value.  Instead of saying
proctree(depth-1) say proctree(--depth).

--

Fletcher Glenn






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:53 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register