Thread: problem with creating multiple child with fork() in C

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    problem with creating multiple child with fork() in C

    heres the code i have, the number of child is base on the user input, here i just put 5, my intention is to create multiple child to process individual data and then pipe it back to parent where it will store in an array, and print it out after collecting all data

    Code:
    pipe(fd);
    pid=fork();
    for(i=1; i<5; i++){
    
    if(pid<0){
    exit(-1);
    }
    else if (pid==0){ close(fd[0]); //process data and write() to parent exit(1);
    } else {
    wait(NULL); close(fd[1]); //read() from child then store in integer array[i] pipe(fd); pid=fork();
    }
    } // this is outside the for loop for(a=1; a<5;a++){
    printf("%d", array[a]);
    }
    everything works except that the print for-loop executes twice and give me 2 sets of inputs, i tried to put exit() and wait() in parent but no luck, anyone know what my problem is?

    thank you in advance
    Last edited by Gnivol; 02-06-2010 at 11:55 AM.

  2. #2
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    In the third line of your code you have:

    Code:
    for(i=1; i=5; i++){
    Did you intend to write

    i=5; or i < 5?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    i meant i<5, sry, my bad

  4. #4
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    When I'm trying to figure out a loop problem, I normally use DEBUG statements to help me see where the problem is occuring, for example

    Code:
    printf("DEBUG_1\n");
    
    // this is outside the for loop
    for(a=1; a<5;a++){
    
        printf("DEBUG_2\n");
        printf("%d", array[a]);
        printf("DEBUG_3\n");
    }
    
    printf("DEBUG_4\n");
    Then, depending on the output...it normally points me in the right direction to look for the problem. What happens when you implement something like this?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Fork

    Your problem is not the loop. When you fork a program the child created starts execution right after the fork call. You are basically duplicating the parent. The reason your print statement executes twice is because it executes in the parent and in the child. In order to solve this put it in an if statement to dectect it pid is greater than 0. I believe this will solve your problem.
    I think you will also need to put a wait command before this to assure that all child processes complete before executing the print loop.

    Code:
    if(pid > 0) 
    {
       for(a=1; a<5; ++a)
       {
              printf("%d", array[a]);
       }
    }
    Last edited by jtmurphree; 02-18-2010 at 12:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a child process then killing it
    By lonnieh in forum C Programming
    Replies: 21
    Last Post: 08-08-2009, 10:22 AM
  2. problem with fork command after solaris to Linux porting
    By amit.arali in forum Linux Programming
    Replies: 7
    Last Post: 07-21-2009, 11:48 PM
  3. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Multiple Knapsack Problem
    By polymeta in forum C Programming
    Replies: 3
    Last Post: 11-15-2001, 08:03 PM