2014-02-18 68 views
0

所以,我退出子線程回父。我正在使用_exit()系統調用。我想知道幾件事情。一個是我的孩子的_exit參數。這裏是我的孩子進程正在執行的代碼:_exit(),fork()和waitpid()系統調用

printf("\n****Child process.****\n\nSquence: "); 

do{ 
     //Print the integer in the sequence. 
     printf("%d\t",inputInteger); 
     if((inputInteger%2) == 0){ 
      //printf("inputInteger = %d\n", inputInteger); 
      inputInteger = inputInteger/2; 
     }else{ 
      inputInteger = 3*inputInteger +1; 
      //printf("%d\t",inputInteger); 
     } 

    }while(inputInteger != 1); 

    //Makes sure we print off 1! 
    printf("%d\n\n", inputInteger); 

    //Properly exit 
    _exit(status); 

我使用狀態,因爲早在我父線程我在waitpid函數()系統調用中使用它。這是在完成子項後執行的父進程的代碼。

waitpid_check = waitpid(processID, &status, 0); 
     printf("\n****Parent process.****\n"); 

     if(waitpid_check == -1){ 
      printf("Error in waitpid.\n"); 
      exit(EXIT_FAILURE); 
     } 

     if(WIFEXITED(status)){ 
      printf("Child process terminated normally!\n"); 
     } 

在這裏,我使用waitpid()系統調用確保孩子已退出,然後使用狀態檢查它是否已正確退出。我想知道是否以正確的方式去創造孩子並退出。

然後我還想知道我是否正確檢查了父母孩子的退出。

感謝您的幫助!

+0

我們在這裏或進程說話線程?從調用中,它看起來像是在處理子進程,而不是子進程。 – jia103

+0

是的,我正在處理流程。 – pmac89

回答

0

從waitpid linux手冊。 「

」如果狀態不爲NULL,wait()和waitpid()會在其指向的 所指向的int中存儲狀態信息。「

您不需要等待支付的返回值來檢查孩子是否失敗。你需要檢查狀態的值。有幾個宏來檢查狀態。

WIFEXITED(status) 
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main(). 
WEXITSTATUS(status) 
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true. 
WIFSIGNALED(status) 
returns true if the child process was terminated by a signal. 
WTERMSIG(status) 
returns the number of the signal that caused the child process to terminate. This macro should only be employed if WIFSIGNALED returned true. 
WCOREDUMP(status) 
returns true if the child produced a core dump. This macro should only be employed if WIFSIGNALED returned true. This macro is not specified in POSIX.1-2001 and is not available on some UNIX implementations (e.g., AIX, SunOS). Only use this enclosed in #ifdef WCOREDUMP ... #endif. 
WIFSTOPPED(status) 
returns true if the child process was stopped by delivery of a signal; this is only possible if the call was done using WUNTRACED or when the child is being traced (see ptrace(2)). 
WSTOPSIG(status) 
returns the number of the signal which caused the child to stop. This macro should only be employed if WIFSTOPPED returned true. 
WIFCONTINUED(status) 
(since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT. 

至於你是否退出真正取決於子進程的權利。你會像在任何其他程序中那樣退出,因爲當你分支一個進程時,你實際上只是複製一個地址空間和孩子,當它作爲自己的獨立程序運行時(當然,使用相同的開放式FD,已經聲明的值等作爲父級) 。下面是此問題的典型實現(儘管NULL被傳遞到等待,而不是一個狀態,所以我認爲你正在做的是正確的。)

/* fork a child process */ 
pid = fork(); 

if (pid < 0) { /* error occurred */ 
    fprintf(stderr, "Fork Failed\n"); 
    return 1; 
} 
else if (pid == 0) { /* child process */ 
    printf("I am the child %d\n",pid); 
    execlp("/bin/ls","ls",NULL); 
} 
else { /* parent process */ 
    /* parent will wait for the child to complete */ 
    printf("I am the parent %d\n",pid); 
    wait(NULL); 

    printf("Child Complete\n"); 
} 

return 0; 
+0

謝謝,我錯過了手冊頁中的_exit(2)部分。這是我的主要問題。我只是不想顯示所有的代碼,因爲它是一個任務。 – pmac89

0

我很樂意幫助,但在這些電話中我真的很生疏。如果您已經閱讀了關於這些API調用的文檔,並且您到處檢查錯誤返回,那麼您應該狀態良好。

這個想法似乎很好。

有一點需要記住的是,你可能想在try/catch中包圍你的孩子方法的肉。使用線程,你通常不希望有一個例外來搞亂你的主流。

您將不會遇到多個進程的問題,但請考慮是否要在面臨異常時調用_exit以及如何與(向父級或用戶)通信發生異常。