Thread: Spawning multiple processes

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

    Spawning multiple processes

    Hi,

    I would like to know how to spawn multiple processes from one process, and have them all doing something at once. I know that you can create a process with the fork() function, but aren't I in the child process when I do that?

    So, if I did:
    pid_t child;
    child = fork();
    , then if pid_t is 0, then I'm in the child process now, right? So, if I'm the child now, I could get that child process started on some task, but how do I go back up to the parent to create other processes and have them start other tasks in parallel, without waiting for this current child to die?

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    If fork() is successful, the return value is 0 in the child and > 0 (actually the PID of the child) in the parent. An example of how to do this might look something like this:

    Code:
    pid_t ret = fork();
    
    if (ret == -1) {
        // no child process created - handle error
        perror("fork()");
        exit(EXIT_FAILURE);
    } else if (ret == 0) {
        // this code is executed in the child process
        while (1) {
            printf("This is the child process\n");
            sleep(1);
        }
    } else {
        // this code is executed in the parent process
        while (1) {
            printf("This is the parent process (our child has pid %d)\n", ret);
            sleep(1);
        }
    }
    In other words, if fork() is successful then both parent and child are executing concurrently (conceptually, at least...) and a program can differentiate between whether it is the child or parent by examining the return value of fork().

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    OK, that's very helpful. So, since parent and child are running concurrently after a successful fork(), any code executed after the fork() needs to be in a conditional statement based on the PID to prevent the code from being executed by *both* parent and child, correct?

    This makes me curious. So, if I did something like this:

    pid_t ret1 = fork();
    pid_t ret2 = fork();
    pid_t ret3 = fork();

    , would there be eight processes (including the parent)? I'm thinking this would happen:

    Line 1 - Parent creates a child (Child 1).
    Line 2 - Parent creates a second child (Child 2), and Child 1 creates a child (Child 3).
    Line 3 - Parent creates Child 4, Child 1 creates Child 5, Child 2 creates Child 6, Child 3 creates Child 7.

    Exponentially growing processes, scary. Is this correct?

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ncurses and multiple processes
    By The Jar in forum Linux Programming
    Replies: 4
    Last Post: 12-28-2009, 11:39 AM
  2. Same pipe to multiple processes?
    By Ironic in forum C Programming
    Replies: 7
    Last Post: 10-25-2008, 10:10 AM
  3. shared libraries, datasegment and multiple processes
    By ashim_k1 in forum Linux Programming
    Replies: 1
    Last Post: 02-28-2008, 02:23 PM
  4. Multiple processes
    By cpsh007 in forum C Programming
    Replies: 10
    Last Post: 11-22-2007, 05:30 AM

Tags for this Thread