无法处理的POSIX信号的返回码 [英] Return code for POSIX signals that cannot be handled

查看:58
本文介绍了无法处理的POSIX信号的返回码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与在POSIX(Linux)环境上运行的应用程序有关.大多数信号(例如 Ctrl + C -信号2,SIGINT)和其他信号都可以处理.完成此操作后,将使用所需的退出代码从处理程序中调用 exit()系统调用.

This is regarding the application that runs on POSIX (Linux) environment. Most signals (e.g. Ctrl+C - signal 2, SIGINT), and few others are handled. When that is done the exit() system call is called from the handler with a desirable exit code.

但是,有些信号如信号9和信号15无法处理.

However, there are some signals like Signal 9 and Signal 15 can't be handled.

不幸的是,如果信号9或15是终止的原因,则启动给定应用程序的父进程(外部脚本)需要知道并清除一些内容.

Unfortunately, the parent process (an external script) which launches the given application needs to know and clean up some stuff if the signal 9 or 15 was the reason for termination.

父进程是否可以接收预定义的退出代码以了解上述内容?

Is there a predefined exit code that can be received by parent process to know the above?

启动应用程序的脚本是bash_script.该应用程序本身位于C中.

The script that launches the app is a bash_script. The application itself is in C.

推荐答案

wait()

The return status from wait() or waitpid() encodes the information you need.

POSIX宏是:

    如果孩子通过 exit()或其亲戚之一退出,则
  • WIFEXITED(status)返回true.
  • WEXITSTATUS(status)告诉您退出状态是(0..255).
  • 如果孩子由于信号(任何信号)而退出,则
  • WIFSIGNALED(status)返回true.
  • WTERMSIG(status)返回杀死该孩子的信号编号.
  • WIFEXITED(status) returns true if the child exited via exit() or one of its relatives.
  • WEXITSTATUS(status) tells you what that exit status was (0..255).
  • WIFSIGNALED(status) returns true if the child exited because of a signal (any signal).
  • WTERMSIG(status) returns the signal number that killed the child.

非标准但通用的宏 WCOREDUMP(status)会告诉您该进程是否转储了核心.您还可以判断状态是反映进程已停止还是继续(以及停止信号是什么).

The non-standard but common macro WCOREDUMP(status) tells you if the process dumped core. You can also tell whether status reflect that the process was stopped, or continued (and what the stop signal was).

请注意,信号15通常是SIGTERM,并且SIGTERM可以被应用程序捕获.无法捕获的信号是SIGKILL(9)和SIGSTOP(在Mac OS X上为17;在每个地方可能都不相同).

Note that signal 15 is usually SIGTERM and SIGTERM can be trapped by an application. The signals that cannot be trapped are SIGKILL (9) and SIGSTOP (17 on Mac OS X; may not be the same everywhere).

然后问题是 bash 是否提供脚本的此信息.

The question then is if bash provides this info for a script.

答案是肯定的,但是只是间接的,而不是100%的明确.对于由于信号< signum> 而终止的进程, bash 报告的状态值将为 128 +< signum> ,但是您可以例如,不能区分以状态 130 退出的进程和被SIGINT(也称为信号2)中断的进程.

The answer is yes, but only indirectly and not 100% unambiguously. The status value reported by bash will be 128 + <signum> for processes that terminate due to signal <signum>, but you can't distinguish between a process that exits with status 130, say, and a process that was interrupted by SIGINT, aka signal 2.

这篇关于无法处理的POSIX信号的返回码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆