我正在使用 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.

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

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

有帮助吗?

解决方案

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

那是你的问题。你不能 WIFSIGNALED*status 之后,你就失去了其中所需的位 int.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top