gpt4 book ai didi

c - _exit()、fork() 和 waitpid() 系统调用

转载 作者:行者123 更新时间:2023-11-30 17:39:16 32 4
gpt4 key购买 nike

所以,我将从子线程退出回到父线程。我正在使用 _exit() 系统调用。我想知道一些事情。其中之一是我的 child 的 _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 来检查它是否已正确退出。我想知道我是否以正确的方式来创建 child 并退出它。

然后我还想知道我是否正确检查了父级中子级的退出。

感谢您的帮助!

最佳答案

来自 waitpid Linux 手册。

“如果status不为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;

关于c - _exit()、fork() 和 waitpid() 系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859954/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com