LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2003, 09:09 PM   #1
skora
LQ Newbie
 
Registered: Aug 2003
Location: Somewhere out in space
Distribution: SuSe 8.0
Posts: 18

Rep: Reputation: 0
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();
        }
 
}
 
Old 11-01-2003, 09:50 PM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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
 
Old 11-01-2003, 10:11 PM   #3
skora
LQ Newbie
 
Registered: Aug 2003
Location: Somewhere out in space
Distribution: SuSe 8.0
Posts: 18

Original Poster
Rep: Reputation: 0
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

Last edited by skora; 11-01-2003 at 10:33 PM.
 
Old 11-01-2003, 11:15 PM   #4
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
posted twice: look at the next one

Last edited by champ; 11-01-2003 at 11:25 PM.
 
Old 11-01-2003, 11:22 PM   #5
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
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.
 
Old 11-02-2003, 10:41 AM   #6
skora
LQ Newbie
 
Registered: Aug 2003
Location: Somewhere out in space
Distribution: SuSe 8.0
Posts: 18

Original Poster
Rep: Reputation: 0
thank you guys
i fixed my code...
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parent child processes signal sharing iftiuk Programming 8 06-24-2004 01:32 PM
Getting a parent to communicate with its child -- fork() kamel Programming 3 06-02-2004 03:04 AM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM
child and parent process error jdevanand Programming 1 04-29-2002 08:13 AM
child-parent-&-fork mukul Programming 2 03-24-2001 01:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration