Thread: create a child process that creates a child process

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    create a child process that creates a child process

    Hi all I have been asked to: Write a C program that will create a child process which is a simple copy of the parent process and each should report their existence by outputting its own PID and its PPID to the screen. Modify the program so that the child process creates its own child process which also reports its own and its parents PID.

    The following compiles successfully with no errors - however only creates one child process and would like some advice - on how the child process that I have created can create its own process.

    Code:
    #include <stdio.h>
    
    main ()
    
    {
    
     int pid;
    
     printf ("I'm the original process with PID %d and PPID %d.\n",
    
             getpid (), getppid ());
    
     pid = fork ();                             /* Duplicate process. Child and parent continue from here */
    
     if (pid != 0)                              /* pid is non-zero, so I must be the parent */
    
       {
    
         printf ("I'm the parent process with PID %d and PPID %d.\n",
    
                  getpid (), getppid ());
    
         printf ("My child's PID is %d\n", pid);
    
       }
    
     else                                                                                  /* pid is zero, so I must be the child */
    
       {
    
         printf ("I'm the child process with PID %d and PPID %d.\n",
    
                  getpid (), getppid ());
    
       }
    
     printf ("PID %d terminates.\n", getpid () );                     /* Both processes
     execute this */
    
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Do another fork() inside the ELSE leg.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by Dino View Post
    Do another fork() inside the ELSE leg.
    cheers... i will try in a moment - should I use the int variable 'pid' or create another?
    Last edited by cus; 01-12-2009 at 02:26 PM. Reason: added a question!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cus View Post
    cheers... i will try in a moment - should I use the int variable 'pid' or create another?
    Use the same - since it was zero already, so not really much "value" in that value.

    You can ALSO create another child in the child process, but you'd then have to prevent it from going around doing that forever.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    i have created another fork() inside the else tag, and think that I am right in saying that the code now creates a child process that in itself then creates its own child process.
    This is the code I have come up with:
    Code:
    #include <stdio.h>
    main ()
    {
     int pid, pid2;
     printf ("\nI'm the original process with PID %d and PPID %d.\n\n", getpid (), getppid ());
     pid = fork ();						/* Duplicate process. Child and parent continue from here */
     if (pid != 0) 					       /* pid is non-zero, so I must be the parent */
       {
         printf ("I'm the parent process with PID %d and PPID %d.\n",
                  getpid (), getppid ());
         printf ("My child's PID is %d\n", pid);
       }
     else 						/* pid is zero, so I must be the child */
       {
         printf ("I'm the child process with PID %d and PPID %d.\n", 
    		getpid (), getppid ());
         pid2= fork(); 		
         printf ("I'm the child's child with PID %d and PPID %d.\n", 
    		getpid (), getppid ());    
       }
     printf ("PID %d terminates.\n", getpid () );
    }
    The output of this is:
    Code:
    I'm the original process with PID 9213 and PPID 7921.
    I'm the child process with PID 9214 and PPID 9213.
    I'm the child's child with PID 9215 and PPID 9214.
    PID 9215 terminates.
    I'm the parent process with PID 9213 and PPID 7921.
    My child's PID is 9214
    PID 9213 terminates.
    I'm the child's child with PID 9214 and PPID 1.                        /*THE PROBLEM IS HERE!!*/
    PID 9214 terminates.
    Why is the i'm the child's child' printf statement being printed out TWICE? i don't want it to do this - also why is the PPID 1? which i realize is the PID of init.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by cus View Post
    Why is the i'm the child's child' printf statement being printed out TWICE? i don't want it to do this - also why is the PPID 1? which i realize is the PID of init.
    Because both the parent (which is the first child off the main parent) and the (second) child are executing that code.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by Dino View Post
    Because both the parent (which is the first child off the main parent) and the (second) child are executing that code.
    So should the second fork() not be inside the else tag ?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    No, it should be there, you just forgot to add the
    Code:
    if (pid != 0) {... }   // parent leg 
    else {...} // child leg
    logic like you added after your first fork().
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you made each printf unique, say
    printf ("1. I'm the child's child with PID %d and PPID %d.\n"
    printf ("2. I'm the child's child with PID %d and PPID %d.\n"
    printf ("3. I'm the child's child with PID %d and PPID %d.\n"
    it might be easier to follow what's going on.

    And a PPID of 1 means that the parent which created the child process has already died, and the (now orphaned) child has been adopted by the init process (pid=1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    thanks all... works fine now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 PM
  2. How to tell when shell create a process?
    By hanhao in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2004, 07:04 AM
  3. how to create a process in c++?
    By cc246 in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2003, 07:24 AM
  4. How to execute a child process in dos, without closing the parent.
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 08-22-2002, 11:48 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM