1

我正在使用 C 语言开发 Unix-Shell。我使用 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.

有人有一个好主意可以帮助我吗?

-> 找到我的答案,谢谢。

4

1 回答 1

3
if (WIFEXITED(*status))
    *status = WEXITSTATUS(*status);

那是你的问题。在那之后你不能WIFSIGNALED继续*status,你已经丢失了int.

于 2012-05-18T13:08:15.173 回答