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. Mac环境brew安装

    curl -L https://get.rvm.io | bash -s stable //RVM安装 source ~/.rvm/scripts/rvm rvm -v //版本检测 rvm list ...

  2. Redis教程(二):String数据类型

    一.概述: 字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这便意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等.在Redis中字符串类型 ...

  3. Python_Day4_函数

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 定义: 函数是指将一组语句的集合通过一个名字( ...

  4. 【腾讯云的1001种玩法】腾讯云搭建DiscuzX论坛

    版权声明:本文由艾可德原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/506828001481866457 来源:腾云阁 h ...

  5. i.Windows下APR安装过程

    1.下载安装native Windows下的APR安装和配置比较简单,可以直接下载Tomcat-native的二进制的版本包: 下载之后的目录结构为: tcnative-1.dll这个库已经包含了AP ...

  6. python3 文件增删查

    #Author by Andy#_*_ coding:utf-8 _*_import sysimport timePath="E:\my python study\\files\haprox ...

  7. markdown小记

    一直听说用markdown写文档比较符合程序员的逼格,没事就倒腾了下,附上近期整理的精华,留自己以方便查阅. 1.常用指令 单个回车 视为空格. 连续回车 才能分段. 行尾加两个空格,这里-> ...

  8. Android 自定义View

    Android 自定义View流程中的几个方法解析: onFinishInflate():从布局文件.xml加载完组件后回调 onMeasure() :调用该方法负责测量组件大小 onSizeChan ...

  9. hexo 本地local4000打不开解决方法

    错误:Cannot GET /spadesq.github.io/ (注:spadesq.github.io是原来放hexo文件夹的名字) 由于我后来把hexo文件夹搬迁到别处,但我发现打开本地,地址 ...

  10. "Couldn't communicate with a helper application" in Xcode 7

    解决方案 xcrun git config --global user.email you@yourdomain.com xcrun git config --global user.name &qu ...