そのため、子スレッドを終了して親スレッドに戻ります。_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 を使用して適切に終了したかどうかを確認しています。子を作成して終了する正しい方法でこれを行っているかどうか疑問に思っていました。
次に、親の子の終了を正しくチェックしているかどうかも疑問に思っていました。
ご協力いただきありがとうございます!