Domanda

Sto lavorando su un gruppo UNIX, usando C Langage. Uso Waitpid per attendere i miei processi per terminare e voglio sapere se il processo di mio figlio (creato con fork()) ha ricevuto un segnale come SIGSEGV.

Codice corrente:

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 è chiamato nel processo genitore.

Sembra che Wifsignaled restituisca sempre true, anche quando eseguo un comando come false || true.

Qualcuno ha una buona idea che potrebbe aiutarmi?

-> Ho trovato la mia risposta, grazie.

È stato utile?

Soluzione

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

Questo è il tuo problema.Non è possibile generarecodicitagcode su WIFSIGNALED dopo di ciò, hai perso i bit richiesti in quel *status.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top