create a child process that creates a child process

This is a discussion on create a child process that creates a child process within the Linux Programming forums, part of the Platform Specific Boards category; Hi all I have been asked to: Write a C program that will create a child process which is a ...

  1. #1
    cus
    cus is offline
    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
    Katy, Texas
    Posts
    2,309
    Do another fork() inside the ELSE leg.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    cus
    cus is offline
    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
    cus
    cus is offline
    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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    cus
    cus is offline
    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
    Katy, Texas
    Posts
    2,309
    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().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    cus
    cus is offline
    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, 08: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-23-2002, 12:48 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21