当前位置:网站首页>sigaction的使用
sigaction的使用
2022-07-03 03:28:00 【___波子MI Pro.】
sigaction结构体定义
struct sigaction
{
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t*, void*);
sigset_t sa_mask;
int sa_flags;
};
sa_handler
:信号处理器函数的地址,亦或是常量SIG_IGN
、SIG_DFL
之一。仅当sa_handler
是信号处理程序的地址时,亦即sa_handler的取值在SIG_IGN
和SIG_DFL
之外,才会对sa_mask
和sa_flags
字段加以处理。sa_sigaction
:如果设置了SA_SIGINFO
标志位,则会使用sa_sigaction
处理函数,否则使用sa_handler
处理函数。sa_mask
:定义一组信号,在调用由sa_handler
所定义的处理器程序时将阻塞该组信号,不允许它们中断此处理器程序的执行。sa_flags
:位掩码,指定用于控制信号处理过程的各种选项。SA_NODEFER
:捕获该信号时,不会在执行处理器程序时将该信号自动添加到进程掩码中。SA_ONSTACK
:针对此信号调用处理器函数时,使用了由sigaltstack()
安装的备选栈。SA_RESETHAND
:当捕获该信号时,会在调用处理器函数之前将信号处置重置为默认值(即SIG_IGN
)。SA_SIGINFO
:调用信号处理器程序时携带了额外参数,其中提供了关于信号的深入信息
使用示例一(使用sa_handler)
void setupSignalHandlers(void)
{
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
act.sa_handler = sigtermHandler;
sigaction(SIGTERM, &act, NULL);
return;
}
static void sigtermHandler(int sig)
{
// TODO
}
使用示例二(使用sa_sigaction)
// 设置信号处理
void setupSignalHandlers(void)
{
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
act.sa_sigaction = sigsegvHandler;
sigaction(SIGSEGV, &act, NULL);
return;
}
static void sigsegvHandler(int sig, siginfo_t *info, void *secret)
{
// TODO
}
对段错误等致命信号的处理
当接收到段错误等致命信号时,可以先捕获该信号,做一些处理,比如保存调用堆栈信息等,再向进程发送该信号,确保程序能够以正常方式结束,比如设置生成dump文件等。
struct sigaction act;
// TODO
sigemptyset (&act.sa_mask);
act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
act.sa_handler = SIG_DFL;
sigaction (sig, &act, NULL);
kill(getpid(),sig);
参考
- 《Linux/UNIX系统编程手册》
- https://github.com/antirez/redis/tree/2.2
边栏推荐
- MongoDB简介
- Open Visual Studio 2010 hangs when opening a SQL file sql file
- Hutool动态添加定时任务
- Hi3536c v100r001c02spc040 cross compiler installation
- Compare float with 0
- MySQL practice 45 lecture [row lock]
- @Accessors注解作用指定前缀遵守驼峰命名
- C # webrequest post mode, based on "basic auth" password authentication mode, uploads files and submits other data using multipart / form data mode
- labelme标记的文件转换为yolov5格式
- [Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
猜你喜欢
FileZilla Client下载安装
Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
Limit of one question per day
ffmpeg下载安装教程及介绍
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
Why does thread crash not cause JVM crash
leetcode:动态规划模板
Introduction à mongodb
Lvgl usage experience
node,npm以及yarn下载安装
随机推荐
FileZilla Client下載安裝
ffmpeg录制屏幕和截屏
Ansible简介【暂未完成(半成品)】
Limit of one question per day
The idea setting code is in UTF-8 idea Properties configuration file Chinese garbled
[mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
Node start server
Idea set method call ignore case
[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
Table structure of Navicat export database
Open Visual Studio 2010 hangs when opening a SQL file sql file
The file marked by labelme is converted to yolov5 format
PHP constructor with parameters - PHP constructor with a parameter
numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
用Three.js做一個簡單的3D場景
Idea format code idea set shortcut key format code
基于QT的tensorRT加速的yolov5
[combinatorics] Application of exponential generating function (multiple set arrangement problem | different balls in different boxes | derivation of exponential generating function of odd / even sequ
递归:深度优先搜索
Compare float with 0