我有一个简单的程序,使用 fork() 创建一个子进程,子进程使用 execvp() 函数来执行 Unix 方法,例如:“ls”或“echo”。 当子进程工作时,父进程正在使用 wait() 函数等待。 子进程完成后,父进程使用 WIFEXITED() 宏检查导致子进程结束的原因。如果 child 没有正常结束,家长会在屏幕上打印一条消息。 出于调试目的,我想让子进程不正常终止,我尝试运行一个除以零但仍然是行的程序:
if(!WIFEXITED(status))
每次都返回true。
我的代码:
int main(){
pid_t p;
int status;
p = fork();
if (p == 0){//child process
execlp("/bin/ls", "ls", NULL);
exit(0);
}else{
wait(&status);
if (!WIFEXITED(status))//if the child process terminated not normally
fprintf(stderr, "\tError Occured!\n");//display error message
}
return 0;}
来自 wait() 的手册页:
WIFEXITED(status) returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
使 WIFEXITED() 返回 false 的正确方法是什么?
最佳答案
What is the right way to make WIFEXITED() return false?
使用信号杀死 child 。然后 WIFSIGNALED
将为 true,并且 WTERMSIG
将为您提供信号。
请注意,您还应该检查 wait
的返回代码。
关于C - 让 WIFEXITED 返回 false 以进行调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031109/