The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-22-2002
theultimatechuf theultimatechuf is offline
Registered User
  
 

Join Date: Jan 2002
Location: UK
Posts: 6
Angry 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 (permalink)  
Old 01-22-2002
aniruddha aniruddha is offline
Registered User
  
 

Join Date: Jan 2002
Location: Pune, India
Posts: 3
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 (permalink)  
Old 01-22-2002
theultimatechuf theultimatechuf is offline
Registered User
  
 

Join Date: Jan 2002
Location: UK
Posts: 6
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 (permalink)  
Old 01-23-2002
bb666 bb666 is offline
Registered User
  
 

Join Date: Jan 2002
Location: romania
Posts: 10
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 (permalink)  
Old 01-24-2002
theultimatechuf theultimatechuf is offline
Registered User
  
 

Join Date: Jan 2002
Location: UK
Posts: 6
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 (permalink)  
Old 01-25-2002
bb666 bb666 is offline
Registered User
  
 

Join Date: Jan 2002
Location: romania
Posts: 10
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 (permalink)  
Old 01-26-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 06:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0