2014-10-01 33 views
-1

我還是新來分叉,我正在創建2-4個子進程,具體取決於命令行參數的數量。我得到的輸出對我來說非常困惑,我不知道我是否正確地做了這件事,如果我有這樣的想法,有人可以解釋爲什麼輸出結果是這樣嗎?分叉創建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 
+0

哪裏是'從孩子'和'從父母'印?我可以建議你在打印時加上'getpid()',這會更容易找出哪個進程正在做什麼。 – SSC 2014-10-01 06:37:44

+0

這是在代碼中...我的理解是,如果你想讓x個孩子,你循環x次循環。 – user3603183 2014-10-01 06:38:44

+0

@ user3603183:如果你循環x次,你會得到(2^x) - 1個孩子 – Haris 2014-10-01 06:42:57

回答

1

聲明雙方父母和孩子在單獨的線程中執行。你有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. 
0

argc如果是6,則循環:

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

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(因爲它在else brachch中,其中game->pid != 0)和您的create_player將永遠不會被調用。

我建議使用一些代碼格式化/縮進工具(幸運的是你的IDE可以做到這一點),如果你有奇怪的縮進,你知道你有這樣的問題。

+0

ahaha!我並沒有意識到發生了這種事情!我實際上故意將第二個大括號放在switch語句的底部,現在輸出看起來就像我期望的那樣 – user3603183 2014-10-01 07:15:16