我正在尝试一些进程操作,例如 fork()ing 和 exec()ing 我尝试了这个示例,其中父 wait()s 他的 child 返回并测试他的 child 是否正常结束(通过 exit()ing 或返回到他的调用者)或异常(接收 SIGABRT 之类的信号)
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include <sys/wait.h>
int spawn (char* program, char** arg_list)
{
pid_t child_pid;
child_pid = fork ();
if (child_pid != 0)
return child_pid;
else {
execvp (program, arg_list);
abort();
}
}
int main ()
{
int child_status;
char* arg_list[] = {"ls","/",NULL};
spawn ("ls", arg_list);
wait (&child_status);
if (WIFEXITED (child_status))
printf ("the child process exited normally, with exit code %d\n",
WEXITSTATUS (child_status));
else
printf ("the child process exited abnormally\n");
return 0;
}
我希望看到“子进程异常退出”这句话,但我看到“子进程正常退出,退出代码为0”!!!即使 child 通过调用发送 SIGABRT 的 abort() 结束 有帮助吗? 提前致谢
最佳答案
当您调用 exec()
系列中的任何函数时,当前正在执行的程序将替换为调用 exec()
时指定的程序。这意味着永远不会调用 abort()
。所以 ls 程序运行完成然后正常退出。
关于c - 即使我向它发送 SIGABRT,子进程也会正常退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3319596/