Thread: parent child process

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    13

    parent child process

    hi new to processes have a couple of questions ....


    1 is main() 'the' initial parent process

    ie when i call getppid() is that returning a reference to mains process ?*

    2 when i call fork() is that the only time that a child process is created ?

    3 how do you tell when a child process has ended

    waitpid (), wait()..? and this causes the program to wait until it gets a signal

    have done some reading and experimented with some code.... but still sketchy uderstanding ...



    *apologies if this is a really stupid question

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 1 is main() 'the' initial parent process
    main is what the C runtime library calls at the end of it's initialisation. It marks the start of your code.

    Code:
    void myfunc ( ) {
      printf( "hello world\n" );
    }
    int main ( ) {
      myfunc();
      return 0;
    }
    myfunc isn't a process any more than main is. A single process exists sometime before main is called until sometime after main returns. Everything in between is all the same process.


    > ie when i call getppid() is that returning a reference to mains process ?
    No, it's the ID of the thing which ran it.
    If you double-clicked on an executable, then getpid() would be yourself, and getppid() would be explorer.

    > 2 when i call fork() is that the only time that a child process is created ?
    Yes (well, mostly).

    > 3 how do you tell when a child process has ended
    Those you've listed are good methods.
    Or write a signal handler for SIGCHLD
    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.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    13
    thanks heaps totally cool and useful response...

    that makes sense of a lot of the code behavior that i have been observing...

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inter process communcation, parent - child
    By tallan in forum C Programming
    Replies: 5
    Last Post: 02-28-2009, 04:04 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Sending a message to parent from child process
    By maxorator in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 04:23 PM