本文介绍了如何将信号名称(字符串)转换为信号代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,从命令行读取信号名称(例如SIGSTOP,SIGKILL等)作为字符串,并调用kill()系统调用来发送信号。我想知道是否有一个简单的方法来将字符串转换为信号代码(在signal.h)。

I am writing a program that reads the name of the signal (e.g. SIGSTOP, SIGKILL etc) as a string from the command line and calls the kill() system call to send the signal. I was wondering if there is a simple way to convert the string to signal codes (in signal.h).

目前,我正在编写自己的地图,如下所示:

Currently, I'm doing this by writing my own map that looks like this:

signal_map["SIGSTOP"] = SIGSTOP;
signal_map["SIGKILL"] = SIGKILL;
....

但是它为所有信号写这个乏味。

But its tedious to write this for all signals. So, I was looking for a more elegant way, if it exists.

推荐答案

不知道这是不是你要的,但是:
strerror()将错误代码转换为错误消息
类似strsignal()将信号转换为信号消息。

Not sure if that is what you are loking for, but:strerror() converts a error code to the error message,similar strsignal() converts a signal to the signal message.

fprintf(stdout, "signal 9: %s\n", strsignal(9));
fprintf(stdout, "errno 60: %s\n", strerror(60));

Output:
signal 9: Killed
errno 60: Device not a stream

这篇关于如何将信号名称(字符串)转换为信号代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:19