LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   parent and child processes (http://www.linuxquestions.org/questions/programming-9/parent-and-child-processes-111301/)

skora 11-01-2003 09:09 PM

parent and child processes
 
I have a problem to make a program that is going to create n processes.
The first process, which is initial program, creates the second process, which in turn creates the third process, and so on until the nth process is created.
my code so far looks like this and when i run it some child processes create more then one child process (every process should create only one child!)
any suggestions how to fix this?

Code:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream.h>


static int n;
void makeprocess();

int main (int argc, char *argv[])
{

        if (argc != 2) {
                printf ("Usage: %s <#processes>\n", argv[0]);
                      exit (1);
          }

          n = atoi (argv[1]);
          if (n > 10 && n < 1)  // create up to 10 new processes
          {
                      printf ("Wrong number of processes (> 10)\n");
                      exit (1);
          }       
        if (fork() == -1)  {        // create 1st process
                      perror ("fork");
                      exit (1);
          }
        n--;

        printf("n:%d  process ID:%d  parent ID:%d\n",
          n, getpid(), getppid());

          makeprocess();


  return 0;
}

void makeprocess()
{
  n--;
        cout <<"n "<<n<<endl;
  if (fork() == -1)  {
        perror ("fork");
        exit (1);
    }
  printf("n:%d  process ID:%d  parent ID:%d\n",
          n, getpid(), getppid());

    if (n > 0) {
        makeprocess();
        }
 
}


SaTaN 11-01-2003 09:50 PM

Quote:


The first process, which is initial program, creates the second process, which in turn creates the third process, and so on until the nth process is created

I don't think that is what is going to happen.It will be more like a binary tree i.e, the parent will be producing another child also .
Code:

                                          PARENT
                      PARENT                          PARENT_CHILD1
            PARENT      PARENT_CHILD2      PARENT_CHILD1  PARENT_CHILD1_CHILD

I suppose,
You will have to stop any process from creating more than one process to implement that

skora 11-01-2003 10:11 PM

Quote:

Originally posted by SaTaN
I don't think that is what is going to happen.It will be more like a binary tree i.e, the parent will be producing another child also .
Code:

                                          PARENT
                      PARENT                          PARENT_CHILD1
            PARENT      PARENT_CHILD2      PARENT_CHILD1  PARENT_CHILD1_CHILD

I suppose,
You will have to stop any process from creating more than one process to implement that

you are right (that is what happened after i run my code).
But the thing is i have to make it so it every parent creates only one child and its child creates only one child an so on... so my question is do you know how i can fix my code so i can get structure like this
Code:

                                          PARENT
                      PARENT                          CHILD1
                      CHILD1                          CHILD2 
                      CHILD2                          CHILD3
                      CHILD3                          CHILD4
...........
.                    CHILDn -1                      CHILDn


champ 11-01-2003 11:15 PM

posted twice: look at the next one

champ 11-01-2003 11:22 PM

you will have to check the return value of the fork() call.
In the parent process, fork() will return the pid of the child, while the fork() returns 0 in the child process.

So if you don't want both processes to create a new process you will have to do something like this.

Code:

// in makeprocess()

pid_t pid;
pid = fork();

switch (pid)
{
 case -1:
      perror ("fork");
      exit (1);
 case 0:
      makeProcess();
}

This way only the child will create a new process.

skora 11-02-2003 10:41 AM

thank you guys
i fixed my code...


All times are GMT -5. The time now is 11:38 PM.