sigqueue函数原型:

函数作用:新的发送信号系统调用,主要是针对实时信号提出的支持信号带有参数,与函数sigaction()配合使用

int sigqueue(pid_t pid, int signo, const union sigval value);

分析:

  • 第一个参数: 指定接收信号的进程id
  • 第二个参数:确定即将发送的信号
  • 第三个参数:是一个联合结构体union sigval,指定了信号传递的参数,即通常所说的4字节值

程序清单

1. 测试代码:

  • 发送端
 #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h> int main(int argc, char *argv[])
{
if(argc != )
{
fprintf(stderr, "Usage %s pid\n", argv[]);
exit();
}
pid_t pid = atoi(argv[]);
union sigval v;
v.sival_int = ;
sigqueue(pid, SIGINT, v);
sleep();
return ;
}
  • 接收端
 #include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> void handler(int sig, siginfo_t *info, void *ctx)
{
printf("recv a sig = %d data = %d data = %d\n", sig, info->si_value.sival_int, info->si_int);
} int main(int argc, char *argv[])
{
printf("I'm %d\n", getpid());
struct sigaction act;
act.sa_sigaction = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO; if(sigaction(SIGINT, &act, NULL) < )
{
perror("sigaction error");
exit();
}
for(; ;)
pause();
return ;
}

输出结果:

  • 发送端

  • 接收端

2. 测试代码

  • 发送端
 #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h> int main(int argc, char *argv[])
{
if(argc != )
{
fprintf(stderr, "Usage %s pid\n", argv[]);
exit();
}
pid_t pid = atoi(argv[]);
union sigval v;
v.sival_int = ;
sigqueue(pid, SIGINT, v);
sigqueue(pid, SIGINT, v);
sigqueue(pid, SIGINT, v);
sigqueue(pid, SIGRTMIN, v);
sigqueue(pid, SIGRTMIN, v);
sigqueue(pid, SIGRTMIN, v);
sleep();
kill(pid, SIGUSR1);
return ;
}
  • 接收端
 #include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> void handler(int sig)
{
if(sig == SIGINT || sig == SIGRTMIN)
printf("recv a sig = %d\n", sig);
else if(sig == SIGUSR1)
{
sigset_t s;
sigemptyset(&s);
sigaddset(&s, SIGINT);
sigaddset(&s, SIGRTMIN);
sigprocmask(SIG_UNBLOCK, &s, NULL);
}
} int main(int argc, char *argv[])
{
printf("I'm %d\n", getpid());
struct sigaction act;
act.sa_sigaction = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = ; sigset_t s;
sigemptyset(&s);
sigaddset(&s, SIGINT);
sigaddset(&s, SIGRTMIN);
sigprocmask(SIG_BLOCK, &s, NULL);
if(sigaction(SIGINT, &act, NULL) < )
{
perror("sigaction error");
exit();
}
if(sigaction(SIGRTMIN, &act, NULL) < )
{
perror("sigaction error");
exit();
}
if(sigaction(SIGUSR1, &act, NULL) < )
{
perror("sigaction error");
exit();
}
for(; ;)
pause();
return ;
}

输出结果

  • 发送端

  • 接收端

最新文章

  1. const
  2. iptables常规使用
  3. iOS播放动态GIF图片
  4. 【实习记】2014-09-24万事达卡bin查询项目总结
  5. java 优化
  6. vim的漫漫长征路
  7. java之JVM学习--基本机构
  8. Linux的chattr与lsattr命令详解
  9. Day6_time模块
  10. 前端导出excel表
  11. 20145215《网络对抗》shellcode注入&amp;Return-to-libc攻击深入
  12. [LeetCode] 733. Flood Fill_Easy tag: BFS
  13. [转载]JS浏览器兼容性问题
  14. 1002 A+B for Polynomials (25 分)
  15. windchill 跑物料变更流程后无法发送物料到SAP
  16. java 命令--备忘
  17. ZXing.Net.Mobile无法识别较大的条码
  18. React+Three.js——PerspectiveCamera透视相机camera参数以及属性值探索
  19. html中块元素的居中。及兼容性
  20. Leveldb源码解析之Bloom Filter

热门文章

  1. Nginx负载均衡实例
  2. git与eclipse集成之更新特性分支代码到个人特性分支
  3. 修改docker image存放位置
  4. Fiddler模拟低速网络
  5. vue 不常见操作
  6. java压缩图片质量
  7. oracle 11.2.0.4 rac 打补丁
  8. SQL Server 之 内部连接
  9. 5分钟搞定Nginx安装
  10. [转]MySQL常用Json函数和MySQL常用字符串函数