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
}
}
} 4 3467
"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);
}
> 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
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!!
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);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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())
|
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.
|
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...
|
by: Antoon Pardon |
last post by:
Comments are welcome:
http://www.pardon-sleeuwaegen.be/antoon/avltree.html
|
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...
| |
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.
|
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)
|
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;
|
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
|
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,...
|
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...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |