Unix — статус Waitpid
Вопрос
Я работаю над Unix-Shell, используя язык C.Я использую 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
.
Не связан с StackOverflow