many forks

This is a discussion on many forks within the C++ Programming forums, part of the General Programming Boards category; hello everyone In my C++ program I have to use fork() a number of times that the program takes as ...

  1. #1
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116

    many forks

    hello everyone

    In my C++ program I have to use fork() a number of times that the program takes as an argument in the command line,let's say I have to use fork() k times.
    How am I supposed to refer to the children processes?
    I mean when I have only one child I do:

    Code:
    int pid=fork();
    if(pid==0){
    //child code
    }
    Now that I have k children ,for the fork I do:

    Code:
    int pid[k]
    for(i=0;i<k;i++)
      pid[i]=fork();
    but what about the if statement?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    Add your if statement as part of the for loop, or visa versa depending on what or the order your trying to test, making sure it is in same scope. You can do this with proper usage of braces.
    Last edited by Tropod; 06-07-2012 at 03:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. communication between forks
    By kaijuu in forum C Programming
    Replies: 6
    Last Post: 05-07-2008, 10:37 AM
  2. Forks v's threads
    By clancyPC in forum C Programming
    Replies: 7
    Last Post: 11-11-2005, 06:29 AM
  3. forks and children
    By suigeneris in forum C Programming
    Replies: 1
    Last Post: 01-22-2005, 05:48 AM
  4. Forks and Processes
    By osal in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 06:47 PM
  5. Pipes/Forks
    By Alexisa in forum C Programming
    Replies: 6
    Last Post: 11-06-2003, 07:31 PM

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