need help creating a tree of processes(implemented in C)

J

Jani Yusef

I want to create a tree of processes. The depth of the tree is given
by the variable depth and the number of processes started by each node
is given by the variable numberOfProcesses.
Is the following code correct? I don't think it is, especially since I
don't want the bottom nodes(leaf nodes) to start any processes
themselves.
Any comments on the following code would be much appreciated!!

for(j=0;j<depth;j++){
for(i=0;i<numberOfProcesses;i=i+1){
if((pid=fork())< 0){
printf("Fork failed");
exit(1);
}
if(pid==0){
//then in child

}
if(pid){
//then in parent
}
}
}
 
M

Malcolm

Jani Yusef said:
I want to create a tree of processes. The depth of the tree is given
by the variable depth and the number of processes started by each node
is given by the variable numberOfProcesses.
Is the following code correct? I don't think it is, especially since I
don't want the bottom nodes(leaf nodes) to start any processes
themselves.
Any comments on the following code would be much appreciated!!

for(j=0;j<depth;j++){
for(i=0;i<numberOfProcesses;i=i+1){
if((pid=fork())< 0){
printf("Fork failed");
exit(1);
}
if(pid==0){
//then in child

}
if(pid){
//then in parent
}
}
}
Generally, if you want a tree you are looking at recurisive functions.

The logic is compilcated in this case because fork() spawns a child process,
and returns to both child and parent. Presumably you only want the children
to have children.

Note fork() isn't ANSI. However the problem is mainly C rather than UNIX, so
I'm answering it here.

void spawn(int depth, int nchildren)
{
int i;

if(depth == 0)
return;

/* terminate if parent */
if( fork() != 0)
return;

for(i=0;i<nchildren;i++)
spawn(depth-1, nchildren);
}
 
J

Jani Yusef

Generally, if you want a tree you are looking at recurisive functions.
The logic is compilcated in this case because fork() spawns a child process,
and returns to both child and parent. Presumably you only want the children
to have children.

Note fork() isn't ANSI. However the problem is mainly C rather than UNIX, so
I'm answering it here.

void spawn(int depth, int nchildren)
{
int i;

if(depth == 0)
return;

/* terminate if parent */
if( fork() != 0)
return;

for(i=0;i<nchildren;i++)
spawn(depth-1, nchildren);
}

I tried your code and if I start with a depth of 2 and a number of
processes of 2 and then do a ps on my system I see 8 processes total.
Huh? Shouldn't I have just 3 processes?
Depth1 1
/\
/ \
Depth2 2 2

My code:


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

void spawn(int depth,int nchildren)
{
int i;

if(depth==0)
return;

/* terminate if parent */
if( fork()!=0){

}

for(i=0;i<nchildren;i++)
spawn(depth-1, nchildren);
}
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]);

spawn(depth,numberOfProcesses);

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


After I run with arguments 2 and 2
$ps -ef | grep "2 2"
jani 2148 1979 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2149 2148 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2150 2148 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2151 2148 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2152 2149 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2153 2149 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2154 2152 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2155 2150 0 15:24 ttyp1 00:00:00 ./a.out 2 2
 
J

Jani Yusef

Jani said:
Generally, if you want a tree you are looking at recurisive functions.

The logic is compilcated in this case because fork() spawns a child
process,
and returns to both child and parent. Presumably you only want the
children
to have children.

Note fork() isn't ANSI. However the problem is mainly C rather than
UNIX, so
I'm answering it here.

void spawn(int depth, int nchildren)
{
int i;

if(depth == 0)
return;

/* terminate if parent */
if( fork() != 0)
return;

for(i=0;i<nchildren;i++)
spawn(depth-1, nchildren);
}


I tried your code and if I start with a depth of 2 and a number of
processes of 2 and then do a ps on my system I see 8 processes total.
Huh? Shouldn't I have just 3 processes?
Depth1 1
/\
/ \
Depth2 2 2

My code:


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

void spawn(int depth,int nchildren)
{
int i;

if(depth==0)
return;

/* terminate if parent */
if( fork()!=0){

}

for(i=0;i<nchildren;i++)
spawn(depth-1, nchildren);
}
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]);

spawn(depth,numberOfProcesses);

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


After I run with arguments 2 and 2
$ps -ef | grep "2 2"
jani 2148 1979 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2149 2148 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2150 2148 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2151 2148 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2152 2149 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2153 2149 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2154 2152 0 15:24 ttyp1 00:00:00 ./a.out 2 2
jani 2155 2150 0 15:24 ttyp1 00:00:00 ./a.out 2 2
OK, I modified my code based on another suggestion I got and this works
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!!
 
A

Amit

Generally, if you want a tree you are looking at recurisive functions.

The logic is compilcated in this case because fork() spawns a child process,
and returns to both child and parent. Presumably you only want the children
to have children.

Note fork() isn't ANSI. However the problem is mainly C rather than UNIX, so
I'm answering it here.

void spawn(int depth, int nchildren)
{
int i;

if(depth == 0)
return;

/* terminate if parent */
if( fork() != 0)
return;

for(i=0;i<nchildren;i++)
spawn(depth-1, nchildren);
}
I am a newbie in C,please comment on my following code.Is this OK for
this problem:
#include<stdio.h>

void spawn(int depth, int nchildren)
{
int i;
int nc=nchildren;
if(depth == 0)
return;

while(nc){
nc--;
if( fork() != 0)
{
printf("\npid=%d,ppid=%d\n",getpid(),getppid());
continue;
}
printf("\npid=%d,ppid=%d\n",getpid(),getppid());
spawn(depth-1, nchildren);
return;
}
return;
}

main()
{
int depth=4; /* depth i am taking 4 */
int nop=2; /* no of children */
printf("\nMAIN pid=%d,ppid=%d\n",getpid(),getppid());
spawn(depth-1,nop);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top