当前位置:网站首页>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
边栏推荐
- Réglez la hauteur et lancez le système. Currenttimemillis catton
- MySQL practice 45 lecture [row lock]
- 二进制流转换成字节数组
- Node start server
- 900w+ data, from 17s to 300ms, how to operate
- PAT乙级“1104 天长地久”DFS优化思路
- Ansible简介【暂未完成(半成品)】
- softmax的近似之NCE详解
- Bigvision code
- Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)
猜你喜欢

Tidal characteristics of the Bohai Sea and the Yellow Sea

Pytorch multi card distributed training distributeddataparallel usage

Why does thread crash not cause JVM crash

Téléchargement et installation du client Filezilla

Summary of electromagnetic spectrum

Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
![MySQL practice 45 lecture [transaction isolation]](/img/a5/5420651d6be51e892976f02be8c43c.png)
MySQL practice 45 lecture [transaction isolation]

Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop

PAT乙级“1104 天长地久”DFS优化思路

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
随机推荐
The series of hyperbolic function in daily problem
[set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
Ansible introduction [unfinished (semi-finished products)]
labelimg生成的xml文件转换为voc格式
[combinatorics] Application of exponential generating function (multiple set arrangement problem | different balls in different boxes | derivation of exponential generating function of odd / even sequ
MongoDB基本操作【增、删、改、查】
QT based tensorrt accelerated yolov5
Limit of one question per day
MongoDB主配置文件
Mongodb installation & Deployment
简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息
Pat class B common function Usage Summary
Pytorch轻量级可视化工具wandb(local)
Bigvision code
ffmpeg之 一张/多张图片合成视频
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
PAT乙级“1104 天长地久”DFS优化思路
[mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
On the adjacency matrix and adjacency table of graph storage
About HTTP cache control