【问题标题】:Forking two processes results in multiple processes分叉两个进程导致多个进程
【发布时间】:2013-06-09 21:41:58
【问题描述】:

我希望设计一个从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;
}

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

【问题讨论】:

  • 第一次调用fork会创建两个进程,然后...

标签: c process fork pid


【解决方案1】:

您的代码的问题是,在子进程休眠后,它们从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.
              */
    }
}

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 2013-02-02
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多