Thread: Fork in c

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    12
    Rep Power
    0

    Fork in c


    Hey guys

    I am having problem using the fork command.

    I want to make a program that gathers the pid's of child processes and stores them somewhere.

    for (i=0;i<5;i++)
    {
    r=fork();
    s=getpid();
    // save this s
    }

    The problem is that both parent and child are being executed in each instance of for loop.
    How can I prevent this. I read about kill but using that it gives me just 1 output of s.

    Thanks a lot......
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Read the manual page for fork, it tells you how to use it properly.

    Return Value
    On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately.
    So you need to use a if statement to test the return value of fork. If the value is 0, then you need to branch off to the child code you want to execute. If the return value is non-zero, then you need to branch off in the parent side of the code and save the return value (which is the PID of the child that was just spawned).

    Code:
    in pid;
    if (pid=fork()){
       //Save pid of child
    }
    else {
       //do child code;
    }

    Comments on this post

    • DaWei_M agrees : Seems reasonable.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    12
    Rep Power
    0

    Hey


    Yup I understood it...Thanks a lot....

    I have one more question regarding this same.....

    Let's say I have a variable in main and I create a pointer of structure for queue with NULL.

    Now I create 2 times fork in a for loop then do they have different memory spaces for the pointer.

    I am getting a problem that for the first process I use the pointer and then for the second one the pointer is again reinitialised to NULL......
  6. #4
  7. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    Sheet fahr. First you say you're listening, then you mumble on like you're deaf. I could just crap and fall back in it.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    12
    Rep Power
    0
    What do you want to say?????
  10. #6
  11. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    Go sit on the corral fence and think about it. You don't want me to get specific.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Meh, leave them to it I say - they ain't listening.
    http://forums.devshed.com/c-programm...em-738385.html

    Probably figuring that if they repeat the question often enough, the manual pages will magically change to allow whatever bizarre magic they're trying to conjure up.
    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

IMN logo majestic logo threadwatch logo seochat tools logo