![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| hell & mathematics | ogoy | Shell Programming and Scripting | 5 | 05-26-2008 10:58 PM |
| hell and sqlite | ogoy | Shell Programming and Scripting | 2 | 05-21-2008 12:07 AM |
| rpm hell! | knmwt15000 | UNIX for Dummies Questions & Answers | 7 | 03-27-2002 06:06 AM |
| negative UID/GID?!! I can see 'em but what the hell do they mean?! | hellz | UNIX for Dummies Questions & Answers | 2 | 09-07-2001 03:18 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Hello I am having serious trouble with the fork command, i basically want to create 9 or 10 child processes and store their pid numbers in array while the children stay resident until i kill() them later , i cannot seem to control how many are made as they all seem to create their own children.
I have managed to stop crashing my system, can somebody help please, none of my books are any use. |
|
||||
|
This is a very common problem observed with fork.
You can have some flag variable which will prevent the child process to fork again when the parent forks. A very primitive code may be somthing like this:- #include <stdio.h> #include <unistd.h> #include <errno.h> void main() { int child1, child2, val, if_child; char err[1000]; if_child = 1; memset(err,'\0',strlen(err)); printf("\nParent process ID is %d \n",getpid()); child1 = fork(); if (child1 == -1) { strcpy(err, strerror(errno)); } else { if(child1 > 0) /* If fork command is successful child PID will be greater than zero. */ { printf("\nThe child1 process ID is %d \n", child1); if_child = 0; /* Set the flag here to prevent child process from forking */ } } memset(err,'\0',strlen(err)); if (if_child == 0) { child2 = fork(); if (child2 == -1) { strcpy(err, strerror(errno)); } printf("\nThe child2 process ID is %d \n", child2); kill(child2); } kill(child1); exit(0); } |
|
||||
|
I had the same problem with one of my projects (you can see it if the thread called 'making a process tree') and getting more child processes than i expected was really a nasty problem.
Still haven't figured out how it works exactly but i hope aniruddha's advice will help me too. |
|
||||
|
Thanks annibuddha (was that how you spell it?) seeing bb666's post i thought i'd post this...
I had a bit of trouble as i added more children but decided to store the parent id at the start: pid=getpid() than later i just checked the variable which i understand would be in all the children against against getpid() i.e. to make a child process stop and wait forever after it was created: if(pid!=getpid()) //this is not the parent for(;;) then the processes just sit there until they are killed (hopefully ) Last edited by theultimatechuf; 01-24-2002 at 06:13 PM.. |
|
||||
|
Yep. That's one way to do it or you could just send a stop signal so the child processes won't waste your memory for nothing. Just replace for(;;)
with kill(getpid(),SIGSTOP). That's all fine and dandy but what if you want the child process to actually do something? Cause if you stop it or send it in an infinite loop then you can't quite work within the child can you? That's actually the part i'm interested in: working within the child but prevent it from forking once the parent forks again. |
| Sponsored Links | ||
|
|