C Unix-Waitpid';状态';
我正在使用C语言开发一个Unix Shell。 我使用waitpid等待进程终止,我想知道我的子进程(使用C Unix-Waitpid';状态';,c,unix,signals,status,waitpid,C,Unix,Signals,Status,Waitpid,我正在使用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
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);
}
静态常量错误[error\u NBR]={
{叹气,“挂断”},
{SIGQUIT,“退出(核心转储)”},
{SIGABRT,“中止(核心转储)”},
{SIGBUS,“总线错误(内核转储)”},
{SIGFPE,“浮动异常(内核转储)”},
{SIGKILL,“Killed”},
{SIGUSR1,“用户信号1”},
{SIGSEGV,“分段故障(堆芯转储)”},
{SIGUSR2,“用户信号2”},
{SIGPIPE,“断管”},
{SIGTERM,“终止”},
};
无效打印信号消息(int状态)
{
int i;
if(WIFSIGNALED(状态))
状态=WTERMSIG(状态);
其他的
返回;
i=-1;
而(++i
xwaitpid
在父进程中调用
WIFSIGNALED似乎总是返回TRUE,即使在执行类似false | | TRUE
的命令时也是如此
有人有个好主意可以帮我吗
->找到了我的答案,谢谢
if (WIFEXITED(*status))
*status = WEXITSTATUS(*status);
那是你的问题。您不能在
*状态下wifsignalid
,此后,您丢失了int
中所需的位,发出的明显信号是什么?您确定启动孩子的代码是正确的吗?(顺便说一句,你的代码看起来像是有一个全局变量i
。如果是这样的话,呃…,不要这样做。)你确定你也正确调用了waitpid
?在您显示的代码中,我看不到对它的任何调用,但如果您传入的状态是“错误”的,我也不会感到惊讶。@Mat:只是忘了输入int I
。孩子们只需调用execve()
。您可以显示print\u signal\u message()
,但可以调用print\u sigerr()
?+1来发现问题。状态为16位值,退出值为高阶8位,信号信息为低阶8位。当您使用WEXITSTATUS提取退出状态并将其分配给同一变量时,您实际上是在将退出状态信息传输到存储信号信息的位置,因此(在诊断时)似乎所有内容都发出了信号。