Multiple processes from one Parent using fork() in C

This is a discussion on Multiple processes from one Parent using fork() in C within the Linux Programming forums, part of the Platform Specific Boards category; So I'm trying to write this program that requires only the parent to fork multiple process that will all do ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    37

    Multiple processes from one Parent using fork() in C

    So I'm trying to write this program that requires only the parent to fork multiple process that will all do the same time.

    I am able to generate 3 processes, but only two are from the parent and other one is a result of one of the children's.

    Code:
       printf("Parent = %d\n", getpid());
       for(i=0;i<2;i++) {
          if((pid[i] = fork()) == 0 ) {
             pid = getpid();
             printf("\tChild = %d\n", pid);
             printf("I JUST GOT FORKED %d\n", getpid());
          }
          else
             printf("I AM PARENT %d\n", getpid());
       }
    Output:
    Code:
    Parent = 9727
    I AM PARENT 9727
    	Child = 9728
    I JUST GOT FORKED 9728
    I AM PARENT 9727
    I AM PARENT 9728
    	Child = 9730
    I JUST GOT FORKED 9730
    	Child = 9729
    I JUST GOT FORKED 9729

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    I also tried

    fork();
    fork();

    I got the same result as above.
    3 children process with two being children of the parent and one being the child of one of the the two childrens

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,281
    O_o

    This behavior you are describing is exactly what you've told the source to do.

    Walk through your code, manually with text, and watch what is going to happen.

    I'm not talking about `printf' lines. That is not sufficient. That will always just be the result of what you've coded.

    You need to write it down so that you can see what is really happening.

    Soma

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    Thanks for the reply.
    I've been using ps -eaf in the terminal to view the processes and after looking at things a little more closely, I see what I'm doing now. Though, there is still one child process still hanging out there that I'm not really using but I guess it doesn't really matter. Since I can't seem to find away to ONLY have 3 child process from the parent.

    I have another question, how can I "talk" to different terminals that are open through the different processes. For example, i have processes a, b, and c. I have terminals 1, 2, and 3 open. How can I only let a talk to 1, b talk to 2, and c talk to 3?

    Right now, I am first opening the terminals, then I fork. And no I'm trying to associate the processes (via pid) to the different terminals.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,281
    Since I can't seem to find away to ONLY have 3 child process from the parent.
    And did you do what I told you do? No. You did not. You tried to look at the results. That can only tell you that you've done something wrong. You already know that. Look at the code. If you walk through the code you can see why you getting a forked process from one of the children.

    Soma

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    37
    Sorry.

    Okay, I got rid of the for loop and now have

    Code:
       spawn = fork();
       if(spawn > 0) spawn = fork();
       if(spawn > 0) spawn = fork();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-01-2011, 11:06 AM
  2. Parent and child processes program variables
    By tfarmer4 in forum Linux Programming
    Replies: 1
    Last Post: 03-01-2011, 04:36 PM
  3. Trouble understanding parent and child processes
    By cardinals03 in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2009, 05:51 PM
  4. Fork multiple processes()??
    By Paul22000 in forum C Programming
    Replies: 8
    Last Post: 11-12-2008, 03:47 PM
  5. Sighandler questions with child/parent processes
    By ninjacookies in forum C Programming
    Replies: 2
    Last Post: 07-05-2005, 06:38 AM

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