所以,我从子线程退出回到父线程。我正在使用 _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);
我使用 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 检查它是否已正确退出。我想知道我是否以正确的方式来创建孩子并退出它。
然后我还想知道我是否正确地检查了父母中孩子的退出情况。
谢谢你的帮助!