首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unix - Waitpid‘状态’

Unix - Waitpid‘状态’
EN

Stack Overflow用户
提问于 2012-05-18 20:54:52
回答 1查看 2.7K关注 0票数 1

我正在开发一个Unix-Shell,使用的是C语言。我使用waitpid等待我的进程终止,我想知道我的子进程(用fork()创建)是否收到了类似于SIGSEGV的信号。

当前代码:

代码语言:javascript
复制
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这样的命令。

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-18 21:08:15

代码语言:javascript
复制
if (WIFEXITED(*status))
    *status = WEXITSTATUS(*status);

那是你的问题。在那之后你就不能在*statusWIFSIGNALED了,你已经丢失了那个int中所需的位。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10653070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档