Search code examples
cprocessforkpid

Forking two processes results in multiple processes


I wish to design a function called from main that will fork off any process to sleep and then update the "process array" containing all forked pids and a counter of them. It seems to work, only there's other processes being forked as well (here with pid -1 and 11957) that I'm not sure where comes from. The test run gives:

Parent 11954 forks off children..
Children started: 2
Proc 11955 started
Proc 11956 started
Children started: 1
Child -1 terminated with status 0
Children started: 1
Proc 11957 started
Children started: 0
Child 11957 terminated with status 0
Child 11955 terminated with status 0
Child 11956 terminated with status 0

The code:

#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#define MAXPROC 100

void fork_off(int * proc_t, int * proc_i) {
    int f = fork();
    if (f == 0) {
        fprintf(stderr, "Proc %d started\n", getpid());
        usleep(5000000);
    } else {
        proc_t[*proc_i] = f;
        (*proc_i)++;
    }
}

int main(void) {

    int proc_table[MAXPROC], proc_index, status, i;
    proc_index = status = i = 0;

    printf("Parent %d forks off children..\n", getpid());

    fork_off(proc_table, &proc_index);
    fork_off(proc_table, &proc_index);

    printf("Children started: %d\n", proc_index);

    for (i = 0; i < proc_index; i++) {
        printf("Child %d terminated with status %d\n", 
                waitpid(proc_table[i], &status, 0), status);
    }

    return 0;
}

I wish only to fork off two processes, not more. What's causing this behaviour?


Solution

  • The problem with your code is that after the child processes have slept, they return from fork_off and repeat everything the parent is doing.

    void fork_off(int * proc_t, int * proc_i) {
        int f = fork();
        if (f == 0) {
            fprintf(stderr, "Proc %d started\n", getpid());
            usleep(5000000);
            exit (0); /* exit() closes the entire child process
                       * instead of simply return from the function
                       */
        } else if (f > 0) { /* Make sure there isn't an error being returned.
                             * Even though I've never seen it happen with fork(2),
                             * it's a good habit to get into
                             */
            proc_t[*proc_i] = f;
            (*proc_i)++;
        } else { /*  Adding to the aforementioned point, consider changing
                  *  the return type to int - so that you can return -1
                  *  and check for error.
                  */
        }
    }