1

私はC言語を使用してUnixシェルに取り組んでいます。私は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います。*statusint

于 2012-05-18T13:08:15.173 に答える