Thread: fork() ???

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    fork() ???

    can someone explain to me, why do I need the fork()?
    I know it creates a child process, but... what for? what does it do, and how does it help me with anything?


    Thanks.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Ever needed to split your application in two? Threads are one way, forking is another. Both have different effects.

    Try writing a command line shell, that is normally implemented using fork/exec. There are many posts on here about that subject.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    so.. if I understand it right, it just copies all the variables in the parent process into the child's... that's it??? I can use a function for that, no?


    Thanks.
    Last edited by Devil Panther; 09-19-2003 at 02:19 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It creates a second running process that shares and inherits from the parent.

    There's an example here
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    but it doesn't really do much.

    You could for example, create a menu program that ask the user to pick an option, then the program forks() and performs the selected task, whilst the menu program resumes, giving control back to the user.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    This means that the parent process goes on and does everything... while the child is running and doing something else?

    So, this means that I can create more than one child process?

    I kill the child process with exit(), but if I can create more than one, how do I kill process 1 instead of 2?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    root
    Join Date
    Sep 2003
    Posts
    232
    >how do I kill process 1 instead of 2?
    You mean kill the parent without killing the child or creating a zombie? Funny you should ask that, I answered that exact question in a previous thread about creating daemons. Daemons and zombies and forks, oh my!
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I did read the this post, after tI asked
    but still... my question was: can I create more than one child?

    except that, are the other things I asked correct?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    root
    Join Date
    Sep 2003
    Posts
    232
    >can I create more than one child?
    Sure. And you can do it several ways too. You can have each child have a child:
    Code:
    for (i = 1; i < n; i++) {
      if ((child = fork()) != 0)
        break;
    }
    You can have a single parent with several children:
    Code:
    for (i = 1; i < n; i++) {
      if ((child = fork()) <= 0)
        break;
    }
    You can go completely nuts and go completely nuts by having every child loop forever and create children :
    Code:
    for ( ; ; )
      fork();
    >are the other things I asked correct?
    Pretty much, yea.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  2. Fork() not working.
    By BENCHMARKMAN in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2007, 12:28 AM
  3. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 02:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM