从分叉创造2-4个孩子

时间:2014-10-01 06:30:47

标签: c fork

我仍然是分叉的新手,我希望在2-4个子进程之间创建,具体取决于命令行参数的数量。我得到的输出对我来说非常混乱,我不确定我是否做得非常正确,如果我这样做,有人可以解释为什么输出是这样的吗?

 for (int i = 2; i <= argc; i++) {    //argc c is <=6

    Player *player = malloc(sizeof(*player));

    game->numPlayers = argc - 2;


    // Fork and grab the pid (modify childPid global)
    // -1 failed, 0 child, otherwise parent.
    game->pid = fork();

    if(game->pid == 0) {
         printf("From parent\n");
    } else {
        printf("From child\n");

    switch (childPid = game->pid) {
            // There was an error with fork(), quit and tell the user
        case -1:
            exit_prog(EXIT_BADSTART);
            break;
            // Create the child
        case 0:
            create_player(&game, player, i);
            break;
            // Create the parent
        default:
           // create_parent(game);
            break;
        }
    }
}

From child
From child
From parent
From parent
From child
From parent
From child
From child
From parent
From child
From parent
From child
From parent
From parent

3 个答案:

答案 0 :(得分:1)

fork 语句之后,父和子都在不同的线程中执行。你得到7次来自“父母”和“来自孩子”。所以,我猜你的argc是4.父和子都从 fork 之后的下一行开始执行。

For i = 2
p     will print "From Parent"
c1    will print  "From child"

For i = 3
p, c1    will print "From parent"
c2, c3   will print "Form child"

For i = 4
p,c1,c2,c3     will print "From parent"
c4,c5,c6,c7    will print "From child"


Where p is parent and cx are corresponding children.
What you are missing is that chlid thread will further create more children until i > argc.

if(child) {
    printf("From child");
    break;
}

It will prevent children to continue inside loop.

答案 1 :(得分:1)

以下是代码中的代码段:

 if(game->pid == 0) {
         printf("From parent\n");
    } else {
        printf("From child\n");

    switch (childPid = game->pid) {
            // There was an error with fork(), quit and tell the user
        case -1:
            exit_prog(EXIT_BADSTART);
            break;
            // Create the child
        case 0:
            create_player(&game, player, i);
            break;

}之后缺少printf("From child\n");。因此,switch永远不会有值0(因为它位于game->pid != 0的其他位置,并且永远不会调用create_player

我建议使用一些代码格式化/缩进工具(幸运的是你的IDE可以做到这一点),如果你得到奇怪的缩进,你知道你有这样的问题。

答案 2 :(得分:0)

如果argc6,那么循环:

for (int i = 2; i <= argc; i++)
{
   game->pid = fork();
}

将结束创建2^5 = 32进程,包括主进程。这是尝试解释数字。

让我们将循环转换为

for (int i = 0; i < K; i++)
{
   game->pid = fork();
}
For K = 1, you'll get processes:

Process 1
Process 1.0

which total to 2.

For K = 2, you'll get processes:

Process 1

Process 1.0
Process 1.1

Process 1.0.1

which total to 4.

For K = 3, you'll get processes:

Process 1

Process 1.0
Process 1.1
Process 1.2

Process 1.0.1
Process 1.0.2

Process 1.1.2

Process 1.0.1.2

which total to 8.

For K = 4, you'll get processes:

Process 1

Process 1.0
Process 1.1
Process 1.2
Process 1.3

Process 1.0.1
Process 1.0.2
Process 1.0.3

Process 1.1.2
Process 1.1.3

Process 1.2.3

Process 1.0.1.2
Process 1.0.1.3

Process 1.0.2.3

Process 1.1.2.3

Process 1.0.1.2.3

which total to 16.

For K = 5, you'll get processes:

Process 1

Process 1.0
Process 1.1
Process 1.2
Process 1.3
Process 1.4

Process 1.0.1
Process 1.0.2
Process 1.0.3
Process 1.0.4

Process 1.1.2
Process 1.1.3
Process 1.1.4

Process 1.2.3
Process 1.2.4

Process 1.0.1.2
Process 1.0.1.3
Process 1.0.1.4

Process 1.0.2.3
Process 1.0.2.4

Process 1.0.3.4

Process 1.1.2.3
Process 1.1.2.4

Process 1.1.3.4

Process 1.2.3.4

Process 1.0.1.2.3
Process 1.0.1.2.4

Process 1.0.1.3.4

Process 1.0.2.3.4

Process 1.1.2.3.4

Process 1.0.1.2.3.4

which total to 32.

此模式最终会创建公式2^N。对于N = 5,您最终会创建总共32个进程。