当前位置:网站首页>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
边栏推荐
- Change and access of median value of listening object
- node 开启服务器
- [mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
- Captura下载安装及在Captura配置FFmpeg
- MySQL MAC download and installation tutorial
- [combinatorics] number of solutions of indefinite equations (number of combinations of multiple sets R | number of non negative integer solutions of indefinite equations | number of integer solutions
- Pytorch轻量级可视化工具wandb(local)
- LVGL使用心得
- 渤、黄海的潮汐特征
- The calculation of stripe, kernel and padding in CNN
猜你喜欢

The calculation of stripe, kernel and padding in CNN

Nce detail of softmax approximation

umi 路由拦截(简单粗暴)

MongoDB安装 & 部署

The idea cannot be loaded, and the market solution can be applied (pro test)

Hi3536c v100r001c02spc040 cross compiler installation

VS code配置虚拟环境

Lvgl usage experience

PHP generates PDF tcpdf

User value is the last word in the competition of mobile phone market
随机推荐
Hi3536c v100r001c02spc040 cross compiler installation
Bigvision code
Compare float with 0
Node start server
Download and install node, NPM and yarn
[Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
MongoDB簡介
Pat class B common function Usage Summary
MySQL practice 45 lecture [row lock]
用Three.js做一个简单的3D场景
小程序获取用户头像和昵称
Mongodb installation & Deployment
Ansible introduction [unfinished (semi-finished products)]
递归:深度优先搜索
C# WebRequest POST模式 ,基于“Basic Auth”口令认证模式,使用multipart/form-data方式上传文件及提交其他数据
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
用Three.js做一個簡單的3D場景
[pyg] understand the messagepassing process, GCN demo details
Pat class B "1104 forever" DFS optimization idea
The idea cannot be loaded, and the market solution can be applied (pro test)