How can I get a human-readable description from a signal number?

我怕爱的太早我们不能终老 提交于 2019-12-23 09:26:54

问题


Does the POSIX standard or another C standard provide a way to recover a meaningful message from a signal number, in the same way that strerror() makes it possible to recover a message from errno? The Gnu C library has strsignal(), but if possible, I would like something portable to BSD and other Unix variants.


回答1:


Yes, interestingly, there is a standard way to get a string message from a signal in POSIX. It is, quite coincidentally, strsignal(). From POSIX.1-2008:

The strsignal() function shall map the signal number in signum to an implementation-defined string and shall return a pointer to it. It shall use the same set of messages as the psignal() function.

An environment that does not provide you this function is not POSIX-compliant. Although relatively new (Issue 7 came out in 2008), I have a man page for strsignal() on Mac OS X, so that's a good sign.




回答2:


The externally defined array sys_siglist contains the signal descriptions for each signal number, and is standard issue on BSD.

#include <signal.h>

extern const char *const sys_siglist[];


来源:https://stackoverflow.com/questions/1719481/how-can-i-get-a-human-readable-description-from-a-signal-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!