fork()ing hell!!


 
Thread Tools Search this Thread
Top Forums Programming fork()ing hell!!
# 1  
Old 01-22-2002
Network fork()ing hell!!

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.
# 2  
Old 01-22-2002
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);
}
# 3  
Old 01-22-2002
Thanks a lot, looks promising, i look forward to the day when i can answer someone else's *NIX programmig question. Looks like it's gonna take longer than with VB.
# 4  
Old 01-23-2002
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.
# 5  
Old 01-24-2002
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..
# 6  
Old 01-25-2002
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.
# 7  
Old 01-26-2002
You guys are making this much harder than it needs to be. Let's start by forking one process and storing its pid:
Code:
#include &lt;stdio.h>
#include &lt;unistd.h>

void main()
{
        int pid, parentpid, childpid;

        parentpid=getpid();
        printf("I am the parent process and my pid is %d\n", getpid());

        if (pid=fork()) {
                childpid=pid;
        } else {
                printf("I am a child process and my pid is %d\n", getpid());
                exit(0);
        }

        printf("I am still the parent process and my pid is %d\n", getpid());
        exit(0);
}

Here the child process just displays its pid then exits. You will probably want to do more with your child processes, but after your children take care of business they must exit so they participate in any further forking. Also in my example the parent process exits fairly quickly. This means that init will inherit the child process and will reap it when it dies. If I wanted to keep the parent around, I would need to insure that it issues wait() calls for each child who dies. If I didn't do this, the children would become zombies. I usually just let the parent die.

Once we have some code that does what we want, if we want to do it n times, we use a loop:
Code:
#include &lt;stdio.h>
#include &lt;unistd.h>

void main()
{
        int n, pid, parentpid, childpids[10];

        parentpid=getpid();
        printf("I am the parent process and my pid is %d\n", getpid());

        for(n=0; n<5; n++) {
                if (pid=fork()) {
                        childpids[n]=pid;
                } else {
                        printf("I am a child process and my pid is %d\n", 
                                        getpid());
                        exit(0);
                }
        }

        printf("I am still the parent process and my pid is %d\n", getpid());
        exit(0);
}

As requested by the OP, the children's pids are recorded in an array. But I still just let the parent die.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Issue when fork()ing processes

Hi guys! I'll simplify my problem. I have the following code: #include <fcntl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <signal.h> #include <fcntl.h> #include <unistd.h> #include <sys/wait.h> #define max 25 #define buffdim 50 void p1(); void p2();... (2 Replies)
Discussion started by: pfpietro
2 Replies

2. Shell Programming and Scripting

quoting hell - help needed!!

I am writing a bash script to automate the installation of web environment on a base install of Fedora. And I'm at the limit of my last nerve and my bash skills. My brain is screaming at me: "Give up and use perl", but I am trying to stick to bash since the script will modify the perl environment... (6 Replies)
Discussion started by: lbe
6 Replies

3. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

4. What is on Your Mind?

The Hell of colaboration in UNIX and Linux

I don't want to speak about the goods or bads of both kinds of Operating systems, I only want to share a little experience with you to comment it. I live in Spain and I have home some old unix systems, some of them that I want to sell or change for other things, like a pair of Sun Blade 2000... (0 Replies)
Discussion started by: Golfonauta
0 Replies

5. UNIX for Dummies Questions & Answers

Confussed as hell

:eek: (1 Reply)
Discussion started by: Kevinfine
1 Replies

6. Shell Programming and Scripting

hell & mathematics

I've been able to generate output based on the code scarfake provided me (thanks again man). A little background so everyone more or less knows whats going on: I needed code that would propagate a database with 100,000 entries, for capacity testing purposes, something like a stress test. ... (5 Replies)
Discussion started by: ogoy
5 Replies

7. Shell Programming and Scripting

hell and sqlite

Hi everyone, I have a requirement that requires me to fill an sqlite database with 100,000 entries (no duplicates). I will start out by giving the command that will insert the values necessary to populate the database: # sqlite /var/local/database/dblist "insert into list... (2 Replies)
Discussion started by: ogoy
2 Replies

8. UNIX for Dummies Questions & Answers

rpm hell!

I've just installed redhat 6.2 on one of my systems and am trying to install the gcc c compiler after downloading an rpm from the redhat site. The damn thing gives me: only major numbers <= 3 are supported by this version of RPM what do I do, it does the same with the latest rpm of php ... (7 Replies)
Discussion started by: knmwt15000
7 Replies
Login or Register to Ask a Question