fork() command

This is a discussion on fork() command within the C Programming forums, part of the General Programming Boards category; Hi, I was wondering how fork() executes, that is does the child finish first then lets the parent finish or ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    14

    fork() command

    Hi,
    I was wondering how fork() executes, that is does the child finish first then lets the parent finish or do they execute at the same time working on the same code?

    Also my other question is: if i have 2 processes parent and its child and now I want to create a 3rd process running of the child of the parent. Now the 3rd process will become the child of the child process. So the 2nd process will now also be a parent process as well as a child process. How would I use if thens to do this?

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > or do they execute at the same time working on the same code?
    The OS shares time between them (and between all the other processes)

    > How would I use if thens to do this?
    Code:
    if ( fork() == 0 ) {
      if ( fork() == 0 ) {
        // grandchild
      }
      // child
    }
    // parent
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    thank you very much

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    14

    Fork()

    Question:
    do I have to include fork() again to create a grandchild? such as:


    if ( fork() == 0 ) {
    fork()
    if ( fork() == 0 ) {
    // grandchild
    }
    // child
    }
    // parent

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Did I use three forks?
    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.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    oh man..sorry, im a fool.
    i usually assigne pid = fork()

    i did not see at first the fork()==0.

    heh

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    what if the parent process..may it be the parent of the grandchild or just the child of the original parent, need to finish something first such as read or write some data to a file then the child has to do some work on that file. How can I make sure the the oder is preserved, or should I use pointers some how?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    It sounds like you want to create a pipe - one process writes to one end of the pipe, and the other process reads from the other end of the pipe.

    man pipe
    for more info.
    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.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    is there any other way to do this other then pipe? I just want to make sure that parent does something things first before going onto the child without shared memory.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    What's wrong with pipes?
    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.

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by saphiroth
    is there any other way to do this other then pipe? I just want to make sure that parent does something things first before going onto the child without shared memory.
    This is called a race condition and is solved by the parent telling the child when it is done. A pipe is the mechanism the two process can use to communicate.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Continue here
    forking
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Windows command analaguous to fork
    By Happy_Reaper in forum Windows Programming
    Replies: 2
    Last Post: 06-06-2006, 08:14 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 05:36 PM

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