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的更多相关文章

  1. Linux 改进捕捉信号机制(sigaction,sigqueue)

    sigaction函数 sigaction函数的功能是用于改变进程接收到特定信号后的行为. int sigaction(int signum, const struct sigaction *act, ...

  2. linux中高级信号函数sigaction和sigqueue实例

    /************************************************************************* > File Name: sigquque. ...

  3. Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号

    Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...

  4. linux系统编程之信号(六):信号发送函数sigqueue和信号安装函数sigaction

    一,sigaction() #include <signal.h> int sigaction(int signum,const struct sigaction *act,struct ...

  5. linux系统编程之信号:信号发送函数sigqueue和信号安装函数sigaction

    信号发送函数sigqueue和信号安装函数sigaction sigaction函数用于改变进程接收到特定信号后的行为. sigqueue()是比较新的发送信号系统调用,主要是针对实时信号提出的(当然 ...

  6. sigaction和实时信号sigqueue

    sigaction函数sigaction函数的功能是用于改变进程接收到特定信号后的行为.int sigaction(int signum, const struct sigaction *act,st ...

  7. 信号发送接收函数:sigqueue/sigaction

    信号是一种古老的进程间通信方式,下面的例子利用sigqueue发送信号并附带数据:sigaction函数接受信号并且处理时接受数据. 1.sigqueue: 新的信号发送函数,比kill()函数传递了 ...

  8. linux下的struct sigaction

    工作中使用案例: struct sigaction act; act.sa_sigaction = handleSignal; act.sa_flags = SA_SIGINFO; sigemptys ...

  9. signal函数、sigaction函数及信号集(sigemptyset,sigaddset)操作函数

    信号是与一定的进程相联系的.也就是说,一个进程可以决定在进程中对哪些信号进行什 么样的处理.例如,一个进程可以忽略某些信号而只处理其他一些信号:另外,一个进程还可以选择如何处理信号.总之,这些总与特定 ...

随机推荐

  1. 对 OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks 一文的理解

    一点最重要的学习方法:  当你读一篇论文读不懂时,如果又读了两遍还是懵懵懂懂时怎么办???方法就是别自己死磕了,去百度一下,如果是很好的论文,大多数肯定已经有人读过并作为笔记了的,比如我现在就把我读过 ...

  2. RANSAC 剔除错误匹配 估计模型

    随机抽样一致,这个算法,我以前一直都没有理解透彻.只知道可以用来直线拟合,网上大多数中文博客也都是写直线拟合的,但是用来匹配二维特征的时候,总还是没弄明白. 基本概念参考 http://www.cnb ...

  3. linux学习网站分享

    http://www.zhihu.com/question/19895288 http://linux.vbird.org/ 两个linux学习的网页存起来,以后学习.

  4. springmvc的foward和redirect跳转简单解析

    Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.假设逻辑视图名为 hello,通过配置,我们 配置某个 ViewRe ...

  5. Sql Server R2还有备份数据库错误

    错误信息描述  该数据库是运行版本10.50.1600的服务器上备份的.该版本与此服务器(运行版本10.00.1600)不兼容.请在支持该被份的服务器上还原该数据,  或者使用与此服务器兼容的备份(M ...

  6. cocos2d-x 图片性能测试

    本文是原创文章,如需转载,请注明文章出处 本次测试使用的cocos2d-x版本是3.9,测试环境是XCode7自带的iphone5 一.JPG格式与PVR.CCZ格式对比 1.占用空间对比 a)不透明 ...

  7. poj 2732 Countdown(East Central North America 2005)

    题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...

  8. table中的td内容超出隐藏

    <table style="table-layout: fixed;width: XXX px"> <tr> <td style="whit ...

  9. VPS/云主机 如何试用远程连接登录主机服务器_

    1.windows主机如何远程登录 点本地电脑开始>运行(或者按"window+R")>输入mstsc点确定 弹出远程连接的框输入IP连接,  如果是VPS,直接输入I ...

  10. JAVA 多线程随笔 (一) 可见性和volatile关键字

    // 先上代码 1 public class NoVisibility { private static boolean ready; private static int number; priva ...