473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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
}
}
}
Nov 14 '05 #1
4 3440

"Jani Yusef" <ja**@persian.com> wrote

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);
}
Nov 14 '05 #2
> 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

Nov 14 '05 #3
Jani Yusef wrote:
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!!

Nov 14 '05 #4
Malcolm wrote:
"Jani Yusef" <ja**@persian.com> wrote
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.
<snip> 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);
}
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: max khesin | last post by:
I am working on a windoze service (deamon) that needs to spawn up to NumCPU heavyweight processes on SMP and wait for results, which have to be logged. So I have to do something like (total...
19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
7
by: Antoon Pardon | last post by:
Comments are welcome: http://www.pardon-sleeuwaegen.be/antoon/avltree.html
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
10
by: preethamkumark | last post by:
- The program first creates a shared memory buffer containing an array of 20 integers. - Each slot of the buffer can have either 0 or 1, where 0 represents an empty slot, and 1 represents an...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
2
by: uche | last post by:
Guys, I am trying to build a Parse Tree with the following grammar. I have implemented my insertions using a standard binary tree inserting algorithm. With the psuedocode below, can you guys...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.