Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

creating a tree of processes
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


Report this thread to moderator Post Follow-up to this message
Old Post
Jani Yusef
09-24-04 08:59 AM


Re: creating a tree of processes
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Pascal Bourguignon
09-24-04 08:59 AM


Re: creating a tree of processes

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


Report this thread to moderator Post Follow-up to this message
Old Post
Fletcher Glenn
09-24-04 09:02 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.