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.

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\);

}

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.

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(;:wink:

then the processes just sit there until they are killed (hopefully )

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(;:wink:
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.

You guys are making this much harder than it needs to be. Let's start by forking one process and storing its pid:

#include <stdio.h>
#include <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:

#include <stdio.h>
#include <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.

bb666 sorted your problem?

Nope, not quite.
I was able to make a father process with more then one child by myself. But what I really want is after creating a number of child prcesses, starting with one of them as a father, it should again fork a few times, thus obtaing a tree-structure of processes.
So I guess I need some kind of recursive algorithm to do that. But here comes the problem: if I fork in a recursive way, I can't seem to get that tree structure right.
And also another problem: using the program shown by Perderabo and also my own, I changed the line:
printf("I am a child process and my pid is %d\n", getpid());
with this one:
printf("I am a child process id=%d father=%d\n",getpid(),getppid());
so I could see if the father is the right one, and after a few forks, all the child processes were generated by the process with pid=1. I avoided that by placing a sleep(2) command right before the end of the program, but I'm wondering: who's this process and is there another way to stop that?

Your post helped me out with a fork I am writing, but I am having a little trouble because the execve command only executes the "ls" command when i type one in - no matter WHAT command I type in. if i modify execve("/bin/ls", .....) to only read execve("/bin/", .....), it doesnt execute ANYTHING. what do i need to do? Here is my code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
#include <iomanip.h>
#include <sys/wait.h>

main()
 {
  cout<<"You have entered my 1814 shell"<<endl;
//  string command;
  char line[1024], *doit[20];
  int n, pid, parentpid, childpids[0];


  while (line !="exit")
      {

        cout<<"GIMME:#";
        cin.getline(line,sizeof(line));
        cout<<"The command you typed was: " <<line<<endl;
        if (strcmp(line,"exit")==0)
           return 0;

        parentpid=getpid();
        printf("I am the parent process and my pid is %d\n", getpid());
        if (pid=fork())
          {
             int status;
             waitpid(-1, &status, 0);
             childpids[n]=pid;
          }
                  else
          {
             printf("I am a child process and my pid is %d\n", getpid());
             doit[0] = "ls";
             doit[1] = "-la";
             doit[2] = NULL;
             execve("/bin/ls", doit, NULL);
             exit(0);
          }
      printf("I am still the parent process and my pid is %d\n",
         getpid());
   }//end of while
 if (line =="exit")
 return 0;

printf("I am still the parent process and my pid is %d\n", getpid());
}//end of main

We are not allowed to do your schoolwork for you. But I'll give you a few hints. Code like this:

is not going to run a "date" command. It's simply incredible that you think it might. execve is not going to ignore its arguments. You need to parse the command line and dynamically build arguments for the execve call.

And you should switch to execv anyway. As it is, you are clobbering the environment.

And, for now, require the user to enter "/usr/bin/date" rather than "date" so you don't need to search the PATH. You can get fancy later.

i meant to have commented that code out. I was using it for another purpose. At any rate, I got my shell to work. thaaaaaaaaaanx

mars:$ cat modshell1.3.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
#include <iomanip.h>
#include <sys/wait.h>

int main()
 {
  cout<<"You have entered my 1814 shell"<<endl;
//  string command;
  char line[256], *doit[20];
  int n, pid, parentpid, childpids[0];


  while (line !="exit")
      {

        cout<<"GIMME:#";
        cin.getline(line, 256);
//     cin.getline(line,sizeof(line));
        cout<<"The command you typed was: " <<line<<endl;
        doit[0] = strtok(line, " ");
        int i=1;
        while (doit=strtok(NULL, " "))
            i++;
        doit=NULL;

        if (strcmp(line,"exit")==0)
           return 0;

        parentpid=getpid();
        printf("I am the parent process and my pid is %d\n", getpid());
        if (pid=fork())
 {
             int status;
             waitpid(-1, &status, 0);
             childpids[n]=pid;
          }
                  else
          {
             printf("I am a child process and my pid is %d\n", getpid());
             cout<<"Parameters are: "<<endl;
              for (int j=0; j<i; j++)
                  cout<<doit[j]<<endl;
             cout<<"       EXECUTE CHILD"<<endl;
             execve(doit[0], doit, NULL);
             exit(0);
          }
       cout<<"     KILL CHILD AND RETURN TO PARENT"<<endl;
       printf("I am still the parent process and my pid is %d\n",
         getpid());
   }//end of while
 if (line =="exit")
 return 0;

printf("I am still the parent process and my pid is %d\n", getpid());
}//end of main

Compiling with g++ on an OpenBSD 3.0 box, I get a "Memory Fault", and it dumps core when I try to run a command, like "ls".
The exit value is 139, and gdb reports a "Program received signal SIGSEGV, Segmentation fault.
0x1ad8 in main () "

Is this machine specific?
(I'm a no-C / C++ goof - Trying, though)

I just downloaded the code and works for me on Sun. I don't see anything machine specific here. He is not testing for too many arguments and, if you entered 20 or more arguments to that ls, you could get a memory fault. Could that be it?

hey guys,
i used aniruddha's code in sun and it doesn't work. can somebody give a more rfined and a compiled error free code that works.

Hmm... no. The first time I tried it, I didn't enter anything - I figured maybe that was the problem. But it does the same thing when I type "ls", "/bin/ls", "ls -l", etc...

I scrapped the BSD box to try out Solaris x86, so I'll try it once I get g++ installed...

Here is another one, using a guard and separating out the child code.
Somehow, I feel this one can be easier to read in some situations.
Especially with the requirement that all control stay in the parent.

/*
bla, bla, code example, bla, bla, freely distributed, bla.
(C) Jan Atle Ramsli, Bug producer extraordinaire, esq.
trollet@skynet.be
*/
#include <unistd.h>
#include <stdio.h>

/* A static variable will only be 0 when the parent is loaded */
pid_t statpid = 0;

child_stuff()
{
printf("\tI am a child\n");
for (;:wink:
{

\}

}

main(int ac, char *av[])
{
int i, num;
pid_t mypid;
pid_t children[100];

num = atoi\(av[1]\);
if\(num &lt; 1 || num &gt; 99\)
	num = 10;

mypid = getpid\(\);
printf\("Hi, I am %d\\n", mypid\);

/*
* First time : statpid = 0, mypid = 0xef
* Other times: statpid = 0xef, mypid = 0xef\+x
*	where x != 0
* I think it makes the code clearer if the flag is show here,
* documented here, and acted upon here.
*/
if \(statpid != 0\)
\{
	child_stuff\(\);
	exit\(0\); /* You may or may not get here */
\}

/*
* We are adult now, so we can allow ourselves to have 
* children.
*/
for \(i = 0; i &lt; atoi\(av[1]\); i\+\+\)
\{
	if \(! statpid\)
	\{
		switch\(fork\(\)\)
		\{
			case 0:
			statpid = getpid\(\); /* Note that we have had a child */
			printf\("Giving birth to child nr. %d is a wonderful thing\\n", i\+1\);
			break;
			default:
			printf\("U��������h, buuu-����h!\\n"\);
			break;
		\}
	\}
\}
/*
* WE set it here, too :-]
*/
statpid = getpid\(\);

}