0

そのため、子スレッドを終了して親スレッドに戻ります。_exit() システム コールを使用しています。私はいくつかのことを疑問に思っていました。1 つは、私の子供の _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);

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 を使用して適切に終了したかどうかを確認しています。子を作成して終了する正しい方法でこれを行っているかどうか疑問に思っていました。

次に、親の子の終了を正しくチェックしているかどうかも疑問に思っていました。

ご協力いただきありがとうございます!

4

2 に答える 2

0

私は助けたいのですが、私はこれらの電話に本当に錆びています. これらの API 呼び出しに関するドキュメントを読み、あらゆる場所でエラーが返されていないか確認している場合は、問題なく動作しているはずです。

アイデアは高いレベルで良いようです。

心に留めておくべきことの 1 つは、子メソッドの肉を try/catch で囲みたい場合があるということです。スレッドでは、多くの場合、例外によってメイン フローが台無しになることは望ましくありません。

複数のプロセスではその問題は発生しませんが_exit、例外が発生したときに呼び出されるかどうか、および例外が発生したことを (親またはユーザーに) 伝える方法について考えてください。

于 2014-02-18T17:19:13.570 に答える
0

waitpid Linux マニュアルから。

「ステータスが NULL でない場合、wait() と waitpid() はステータス情報をそれが指す int に格納します。」

子が失敗したかどうかを確認するために、waitpaid の戻り値は必要ありません。ステータスの値を確認する必要があります。ステータスをチェックするためのマクロがいくつかあります。

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;
于 2014-02-18T18:30:58.423 に答える