分叉两个进程会导致多个进程

时间:2013-06-09 21:41:59

标签: c process fork pid

我希望设计一个从main调用的函数,该函数将分离任何进程以进入休眠状态,然后更新"进程数组"包含所有分叉的pid和它们的计数器。它似乎有效,只有其他进程也被分叉(这里有pid -111957),我不知道它来自哪里。测试运行给出:

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

代码:

#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;
}

我希望只分叉两个进程,而不是更多。导致这种行为的原因是什么?

1 个答案:

答案 0 :(得分:0)

代码的问题在于,在子进程睡眠后,它们从fork_off返回并重复父进程所做的一切。

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.
              */
    }
}