簡體   English   中英

從分叉中創造2-4個孩子

[英]Creating 2 - 4 children from forking

我仍然對派生還很陌生,我希望根據命令行參數的數量在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

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.

這是您的代碼段:

 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 (因為它位於else分支中, game->pid != 0 ),並且您的create_player將永遠不會被調用。

我建議您使用一些代碼格式化/縮進工具(如果您的IDE可以做的話,運氣不錯),並且如果您遇到奇怪的縮進,您就知道您有這樣的問題。

如果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進程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM