簡體   English   中英

_exit(),fork()和waitpid()系統調用

[英]_exit(), fork() and waitpid() system calls

因此,我要從子線程返回到父線程。 我正在使用_exit()系統調用。 我想知道幾件事。 一個是我孩子的_exit參數。 這是我的子進程正在執行的代碼:

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()系統調用,以確保該子項已退出,然后使用status檢查它是否已正確退出。 我想知道我是否正在以正確的方式來創建孩子並退出它。

然后我也想知道我是否正確地檢查了孩子在父母中的存在。

謝謝你的幫助!

我很樂意為您提供幫助,但是我對這些電話感到非常生銹。 如果您已經閱讀了有關這些API調用的文檔,並且正在到處檢查錯誤返回,那么您應該處於良好狀態。

從總體上看,這個想法很好。

要記住的一件事是,您可能希望在一次嘗試/捕獲中將孩子方法的內容包含在內。 使用線程時,您通常不希望出現異常情況以使您的主流程混亂。

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

從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;

暫無
暫無

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

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