Thread: forking

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    32

    forking

    Hi

    I've been trying to fork 10 children by the same parent, however the parent copies itself each time (as it should do afterall).. My question is now is there a way to fork n children such that all will have the same parent? and you will keep all the returned children ids in the parent in an array?

    thx

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cmaniac View Post
    I've been trying to fork 10 children by the same parent, however the parent copies itself each time (as it should do afterall).. My question is now is there a way to fork n children such that all will have the same parent? and you will keep all the returned children ids in the parent in an array?
    I don't understand the question. How are you doing it currently? If the parent (original process) goes in a loop and fork()'s n times, then there will be n children, all children of the same original process.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Cmaniac View Post
    My question is now is there a way to fork n children such that all will have the same parent?
    Yes.

    Quote Originally Posted by Cmaniac View Post
    and you will keep all the returned children ids in the parent in an array?
    Sure, why not?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use a for loop

    Code:
    pid_t pids[10];
    for ( i = 0 ; i < 10 ; i++ ) {
      pid[i] = fork();
      if ( pid[i] == 0 ) break;  // now in child, no more fork
    }
    // at this point in the code
    if ( i < 10 ) {
      // in the i'th child
    } else {
      // in the 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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    thanks salem, i did something similar to what you answered me... at first i was trying it from 'main' when i then moved it into a function all i started getting was segmentation faults. I started eliminating line by line and testing and noted that:

    Code:
      pid_t pids[10];
    cannot be of that type when inside a function.... Strange really, i don't know why it happens. Anyay i changed it to int and it now seems to be working.. However im not sure if an int covers all the range in process ids.. some1 can confirm this? an in is 32bits no.. i think it does anyway

    Listen another small question as the server i was working on is down and I can't try it myself at the moment and i'm in the design phases and will block for sometime if I don't clear this out....

    a child can kill his siblings considering it knows their process ids right? which i will keep in an array on shared memory...

    Thanks
    Last edited by Cmaniac; 04-14-2007 at 12:36 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > cannot be of that type when inside a function.... Strange really
    Not really, there's bugs in your code.
    Especially since your 'fix' makes no sense at all.

    > However im not sure if an int covers all the range in process ids
    No, a pid_t covers the range of process IDs, that's what it's there for.

    > a child can kill his siblings considering it knows their process ids right?
    Well the pid array will contain the process IDs of all the children created before itself (think about it).
    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.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    >> However im not sure if an int covers all the range in process ids
    >No, a pid_t covers the range of process IDs, that's what it's there for.

    as for that i meant that an int will cover all process IDs, that is will i be guaranteed that every process id will fit in an int variable?

    as for the other about forking, i just found out that its a better design choice for what i'm trying to achieve to kill the children from the parent, after saving a chunk in shared memory with the child's id wishing to kill his siblings. The parent will then have the pids of all its children and will start killing the children from a for loop, and will only skip the one with its cid saved in the shared memory...

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forking
    By sean in forum Linux Programming
    Replies: 5
    Last Post: 02-24-2009, 02:45 PM
  2. Forking Issue...
    By cookie in forum Linux Programming
    Replies: 1
    Last Post: 07-10-2008, 04:46 PM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. have I got forking problems, or what?
    By reptonite in forum C Programming
    Replies: 8
    Last Post: 02-23-2006, 06:11 PM
  5. Forking advice
    By zee in forum C Programming
    Replies: 4
    Last Post: 06-18-2004, 02:35 PM