sigaction和sigqueue
sigaction函数相对于siganl函数控制信号的发送要更加精确一些,其函数原型为:
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
实验代码:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> /* 信号处理函数:信号量,发送者传递的额外信息,参数 */
void func_handler(int signum, siginfo_t *info, void *arg)
{
printf("/nget the signal. pid : %d, uid : %d\n", info->si_pid, info->si_uid);
printf("sival_int : %d\n", (int)arg);
/* printf("si_signo : %d, si_code : %d, si_errno : %d\n", info->si_signo, info->si_code, info->si_errno); */
printf("si_status : %d\n", info->si_status);
/* printf("si_utime : %d, si_stime : %d\n", info->si_utime, info->si_stime); */
printf("signal from address : 0x%x\n", info->si_addr);
printf("--------++++++++--------\n");
} int main(int argc, char *argv[])
{
struct sigaction signal_action;
/* initializes the signal set given by set to empty, with all signals excluded from the set */
/* 初始化sigaction */
sigemptyset(&signal_action.sa_mask); signal_action.sa_sigaction = func_handler; /* 设置信号处理函数 */
/* 设置标志位 */
/* SA_SIGINFO:信号发送者会提供额外的信息(siginfo_t),信号处理函数应该使用三参数(int,siginfo_t *,void *) */
/* SA_RESTART:如果系统调用被信号中断,则不返回错误,而是自动重启系统调用(重入机制) */
signal_action.sa_flags |= SA_SIGINFO | SA_RESTART; /* The sigaction(int, const struct sigaction *new, struct sigaction *old) system call is used to change the action taken by a process on receipt of a specific signal */
/* 修改信号的处理函数 */
if(sigaction(SIGINT, &signal_action, NULL) == -1)
{
printf("sigaction failed, app exit. error : %s!\n", strerror(errno));
exit(1);
}
while(1)
{
/* 暂停进程,或注释掉该行代码 */
pause();
}
printf("done!\n"); return 0;
}
运行程序后,使用命令:ps -au查看该程序的进程编号

使用sigqueue函数向指定进程发送指定信号,实验代码:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h> int main(int argc, char *argv[])
{
union sigval arg;
arg.sival_int = 2017; /* sigqueue指定的整型参数 */
int pid = atoi(argv[1]); /* 指定一个进程编号 */
int sig = atoi(argv[2]); /* 指定一个信号 */ printf("pid : %d, sig : %d\n", pid, sig); /* 检测该进程是否存在 */
if(sigqueue(pid, 0, arg) != 0)
{
printf("no process's pid : %d, app exit!\n", pid);
exit(-1);
}
printf("check process succeed!\n"); int times = 0;
for(;times<3;times++)
{
printf("sending a signal : [%d] to process : [%d].\n", sig, pid);
if(sigqueue(pid, sig, arg) == -1)
{
printf("sigqueue failed, app exit. err : %s!\n", strerror(errno));
exit(-1);
}
printf("times : %d, sigqueue success, sleep 2 second!\n", times + 1);
sleep(2);
} return 0;
}
使用命令./sigqueue 32499 2, 意思是对进程32499进程发送SIGINT(2)信号

sigqueue进程每隔2秒发送一个SIGINT信号,sigaction进程就显示了信号发送者的额外信息.
当然,也可以通过kill命令向指定的进程发送指定的信号,比如

sigaction和sigqueue的更多相关文章
- Linux 改进捕捉信号机制(sigaction,sigqueue)
sigaction函数 sigaction函数的功能是用于改变进程接收到特定信号后的行为. int sigaction(int signum, const struct sigaction *act, ...
- linux中高级信号函数sigaction和sigqueue实例
/************************************************************************* > File Name: sigquque. ...
- Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号
Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...
- linux系统编程之信号(六):信号发送函数sigqueue和信号安装函数sigaction
一,sigaction() #include <signal.h> int sigaction(int signum,const struct sigaction *act,struct ...
- linux系统编程之信号:信号发送函数sigqueue和信号安装函数sigaction
信号发送函数sigqueue和信号安装函数sigaction sigaction函数用于改变进程接收到特定信号后的行为. sigqueue()是比较新的发送信号系统调用,主要是针对实时信号提出的(当然 ...
- sigaction和实时信号sigqueue
sigaction函数sigaction函数的功能是用于改变进程接收到特定信号后的行为.int sigaction(int signum, const struct sigaction *act,st ...
- 信号发送接收函数:sigqueue/sigaction
信号是一种古老的进程间通信方式,下面的例子利用sigqueue发送信号并附带数据:sigaction函数接受信号并且处理时接受数据. 1.sigqueue: 新的信号发送函数,比kill()函数传递了 ...
- linux下的struct sigaction
工作中使用案例: struct sigaction act; act.sa_sigaction = handleSignal; act.sa_flags = SA_SIGINFO; sigemptys ...
- signal函数、sigaction函数及信号集(sigemptyset,sigaddset)操作函数
信号是与一定的进程相联系的.也就是说,一个进程可以决定在进程中对哪些信号进行什 么样的处理.例如,一个进程可以忽略某些信号而只处理其他一些信号:另外,一个进程还可以选择如何处理信号.总之,这些总与特定 ...
随机推荐
- python获取时间
获取当前时间,和当前UTC时间 #!/usr/bin/env python #_*_ encoding:utf-8_*_ import datetime import time utctime = d ...
- AVSampleBufferDisplayLayer----转
http://blog.csdn.net/fernandowei/article/details/52179631 目前大多数iOS端的视频渲染都使用OpenGLES,但如果仅仅为了渲染而不做其他的例 ...
- #define宏定义形式的"函数"导致的bug
定义了一个宏定义形式的"函数": #define SUM8(YY)\ {\ int Y = YY>>2;\ ...\ } 然后使用的时候,传入了一个同名的变量Y: i ...
- mysql 5.6.34 二进制
安装方法 http://dev.mysql.com/doc/refman/5.6/en/binary-installation.html shell> groupadd mysqlshell&g ...
- C#获取CSV文件内容对逗号和引号分隔的处理
我们知道,使用excel工具保存成csv文件时有几个规则: 1.每一行的单元格内容之间用逗号分隔. 2.如果单元格的内容本身有逗号,这个单元格的内容将会用引号包含. 3.如果单元格的内容本身有引号 1 ...
- [第一个自己做的C小程序]丧失求生文字小游戏
丧失求生文字小游戏 编写原因: 我编写这个小程序是为了结合下我学习的知识并且做一个小游戏来看看我自己的能力,目前我已经学完了C语言的编程基础.马上就要学到指针,这个就是我的基础总结项目,希望大家可以都 ...
- OPENGL半透明图像产生黑色光环
OPENGL提供了多种多样的混合方法,我们很容易就能实现诸如 叠加.变亮等图像混合. 我们知道一般带透明度的图像是RGBA四个通道来存储,最常的glBlendFunc是 glBlendFunc(GL_ ...
- 【翻译】configuration changes与handler.post
原文地址 http://corner.squareup.com/2013/12/android-main-thread-2.html 在前一部分里面previous part ,我们深入挖掘了 loo ...
- phpmyadmin 长时间登陆不过期
一个小技巧: 在项目开发过程中,经常使用phpmyadmin,默认情况下,一段时间不操作,就需要重新登陆,如果要长时间使用,操作如下: 修改config.inc.php中的$cfg['Serv ...
- GOLDENGATE 配置文档,各类参数--转发
1 GoldenGate简要说明 GoldenGate现在是业内成熟的数据容灾与复制产品,经过多年的发展与完善,现在已经成为业内事实上的标准之一. GoldenGate软件是一种基于日志的 ...