473,804 Members | 3,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help creating a tree of processes(imple mented 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 numberOfProcess es.
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<numbe rOfProcesses;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 3468

"Jani Yusef" <ja**@persian.c om> 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 numberOfProcess es.
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<numbe rOfProcesses;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<nchil dren;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<nchil dren;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 numberOfProcess es=-1;

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

if(depth==0)
return;

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

}

for(i=0;i<nchil dren;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]);
numberOfProcess es=atoi(argv[1]);

spawn(depth,num berOfProcesses) ;

read(0,input,MA XBUFFER);
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<nchil dren;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 numberOfProcess es=-1;

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

if(depth==0)
return;

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

}

for(i=0;i<nchil dren;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]);
numberOfProcess es=atoi(argv[1]);

spawn(depth,num berOfProcesses) ;

read(0,input,MA XBUFFER);
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 numberOfProcess es=-1;

void procTree(int depth)
{
for(i=0;i<numbe rOfProcesses;i= i+1){
int socko=socket(2, 1,0);
struct sockaddr_in addr;
addr.sin_family =2;
addr.sin_addr.s _addr=htonl((12 7<<24)+1);
addr.sin_port=0 ;
bind(socko,(str uct 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]);
numberOfProcess es=atoi(argv[1]);

procTree(depth-2);

read(0,input,MA XBUFFER);
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.c om> 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 numberOfProcess es.
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<nchil dren;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",ge tpid(),getppid( ));
continue;
}
printf("\npid=% d,ppid=%d\n",ge tpid(),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(),get ppid());
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
1257
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 pseudocode, thread pool not implemented, but demonstrates the problem): Logger log def ThreadFoo(job): log.write(os.open(job).read())
19
3229
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 pretty interesting.
15
11614
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 so they can return a pointer to their internal buffer, or are they equally likely to create a new buffer on demand? I know the standard doesn't require any particular implementation, which is why I'm curious if there is a consensus among...
7
2063
by: Antoon Pardon | last post by:
Comments are welcome: http://www.pardon-sleeuwaegen.be/antoon/avltree.html
19
6791
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 one, and only one parent. The depth should not exceed 6 or 7 levels. The initial import will have about 6 million leaves, and 3 million branches. I would expect the leaves to grow significantly, in number easily tripling. However, the branches will...
10
2023
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 occupied one. - Initially, the buffer is empty. Thus, all the slots are initialized with 0.
17
1951
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
2954
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 <typename Tclass Tree{ private: Tree<T*left;
2
4444
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 provide any helpful feedback to placing insert statements into building nodes of the tree. Any suggestions on how to build the tree will be appreciated. I hope to build the tree to look like that 5+4 Goal
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.