2016-10-15 86 views
1

我已經創建了一個爲父進程創建兩個子進程的程序。該程序將輸出顯示其進程ID的父進程,然後輸出顯示其ID和父進程ID的兩個子進程。在程序退出並打印輸出後,父進程應該使用wait()函數來捕獲子進程。創建過多的進程,似乎只能終止父進程

但是,我的程序不斷創建父進程並給這些進程提供子進程。我只需要一個父進程來處理兩個子進程。在while循環內部是wait()函數,該函數應該檢查子進程的更改狀態並打印「Child'xxx'進程已終止」。相反,它正在終止一些父進程和其他隨機進程。

#include<stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <stdlib.h> 

int main() 
{ 
    pid_t cpid, cpid2, wpid; 
    int child = fork(); 
    int child2 = fork(); 
    int status; 

    if ((child = fork()) == 0){ 
     child = cpid; 
    } 
    if((child2 = fork()) == 0){ 
     child2 = cpid2; 
    } 
    else{ 
     printf("I am the parent %d\n", getppid()); 
     printf("I am the process %d created by %d\n", cpid, getppid()); 
     printf("I am the process %d created by %d\n", cpid2, getppid()); 

     while ((wpid = wait(&status)) > 0){ 
     printf("Child process %d terminated\n", wpid); 
     } 

    } 

    return (0); 

} 

我的輸出顯示我這個

I am the parent 5764 
I am the process 2 created by 5764 
I am the process 6411548 created by 5764 
I am the parent 13720 
I am the process 2 created by 13720 
I am the process 6411548 created by 13720 
I am the parent 23612 
I am the process 2 created by 23612 
I am the process 6411548 created by 23612 
I am the parent 15096 
I am the process 2 created by 15096 
I am the process 6411548 created by 15096 
I am the parent 24276 
I am the process 2 created by 24276 
I am the process 6411548 created by 24276 
I am the parent 13720 
I am the process 2 created by 13720 
I am the process 6411548 created by 13720 
I am the parent 13720 
I am the process 2 created by 13720 
I am the process 6411548 created by 13720 
I am the parent 5764 
I am the process 2 created by 5764 
I am the process 6411548 created by 5764 
Child process 17016 terminated 
Child process 18584 terminated 
Child process 13984 terminated 
Child process 8480 terminated 
Child process 10816 terminated 
Child process 21968 terminated 
Child process 23388 terminated 
Child process 11452 terminated 
Child process 2776 terminated 
Child process 19328 terminated 
Child process 17116 terminated 
Child process 18352 terminated 
Child process 24276 terminated 
Child process 15096 terminated 
Child process 5764 terminated 
+1

你允許雙方家長和第一叉的孩子繼續執行該程序的其餘部分。他們*都*繼續分叉。 –

+1

當你使用它們時,'cpid'和'cpid2'是未定義的 – Tibrogargan

+0

刪除對「fork」的調用那裏「int child = fork(); int child2 = fork();」 –

回答

3

一旦你fork(),無論是孩子和家長再次呼籲fork()如果你想只調用fork()在父進程,檢查返回值再分叉之前。

int child = fork(); 
// This will be called by both, the child and the parent 
int child2 = fork(); 

fork()回報它返回的孩子在父母子女PID0

1

fork()是分叉的第二個孩子,也你沒有其他的處理在第一fork()

#include<stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/wait.h> 

#include <stdlib.h> 

int main() 
{ 
    pid_t cpid, cpid2, wpid; 
    int child = fork(); 
    int child2 = fork(); 
    int status; 

    if ((child = fork()) == 0){ 
     child = cpid; 
    } 
    else { 
     if((child2 = fork()) == 0){ 
      child2 = cpid2; 
     } 
     else{ 
      printf("I am the parent %d\n", getppid()); 
      printf("I am the process %d created by %d\n", cpid, getppid()); 
      printf("I am the process %d created by %d\n", cpid2, getppid()); 

      while ((wpid = wait(&status)) > 0){ 
       printf("Child process %d terminated\n", wpid); 
      } 
    } 

的方式你這樣做的第一個孩子,第一個孩子是分叉它自己的孩子這爲返回的PID!= 0的情況定義了一個處理程序。因此,您將產生兩個第二個子代並處理父代事件兩次。

2

首先閱讀fork手冊頁以及getppid/getpid手冊頁。

從叉的文檔:

成功時,子進程的PID返回父的 線程執行,並在執行 的孩子的線程則返回0。失敗時,-1將返回父代的上下文中, 將不會創建子進程,並且將適當地設置errno。

if ((child = fork()) == 0){ 
    printf(" %u and %u", getpid(), getppid()); 
} else{ /* avoids error checking*/ 
    printf("Parent - %u ", getpid()); 
} 
+0

所以我玩了上面的代碼,並能夠創建一個有兩個孩子的父母。但是,如果((孩子= fork())== 0)我從另一個進程中獲得第三個孩子 –

+1

printf(「我是孩子%u由%u創建的\ n」,getpid(), getppid()); (「我是母公司 - %u \ n」,getpid());其他{/ *避免錯誤檢查*/ \t \t printf \t} \t \t 如果((的child2 =叉())== 0){ \t \t的printf( 「我是孩子%U通過創建%U \ n」 個,GETPID(),getppid()); \t} –

+0

嘿,而不是讓兩個if語句使1和兩個條件之間添加** && **。這應該可以解決你的問題。 @ericeric – Vedant

相關問題