我正在开发一个Unix-Shell,使用的是C语言。我使用waitpid等待我的进程终止,我想知道我的子进程(用fork()创建)是否收到了类似于SIGSEGV的信号。
当前代码:
static const t_error error[ERROR_NBR]= {
{SIGHUP, "Hangup"},
{SIGQUIT, "Quit (core dumped)"},
{SIGABRT, "Abort (core dumped)"},
{SIGBUS, "Bus error (core dumped)"},
{SIGFPE, "Floating exception (core dumped)"},
{SIGKILL, "Killed"},
{SIGUSR1, "User signal 1"},
{SIGSEGV, "Segmentation fault (core dumped)"},
{SIGUSR2, "User signal 2"},
{SIGPIPE, "Broken pipe"},
{SIGTERM, "Terminated"},
};
void print_signal_message(int status)
{
int i;
if (WIFSIGNALED(status))
status = WTERMSIG(status);
else
return ;
i = -1;
while (++i < ERROR_NBR)
{
if (status == error[i].error_status)
{
fprintf(stderr, "%s\n", error[i].error);
return ;
}
}
return ;
}
int xwaitpid(int pid, int *status, int opt)
{
int ret;
ret = waitpid(pid, status, opt);
if (ret == -1)
fprintf(stderr, "Can't perfom waitpid (pid = %d)\n", pid);
if (WIFEXITED(*status))
*status = WEXITSTATUS(*status);
else
**print_sigerr(*status);**
return (ret == -1 ? (1) : ret);
}在父进程中调用xwaitpid。
似乎WIFSIGNALED总是返回TRUE,即使我执行像false || true这样的命令。
有人有一个好主意可以帮助我吗?
->找到我的答案了,谢谢。
发布于 2012-05-18 21:08:15
if (WIFEXITED(*status))
*status = WEXITSTATUS(*status);那是你的问题。在那之后你就不能在*status上WIFSIGNALED了,你已经丢失了那个int中所需的位。
https://stackoverflow.com/questions/10653070
复制相似问题