For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2004 > creating a tree of processes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author creating a tree of processes
Jani Yusef

2004-09-24, 3:59 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

Pascal Bourguignon

2004-09-24, 3:59 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.
Fletcher Glenn

2004-09-24, 4:02 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 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
>


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

--

Fletcher Glenn

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com