首页 > 其他分享 >函数sigqueue

函数sigqueue

时间:2024-02-02 14:12:55浏览次数:20  
标签:sigqueue 函数 int pid sig act include sigaction

一、函数sigqueue
sigqueue函数原型:

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

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

第一个参数: 指定接收信号的进程id
第二个参数:确定即将发送的信号
第三个参数:是一个联合结构体union sigval,指定了信号传递的参数,即通常所说的4字节值
二、程序清单
1. 测试代码:

发送端程序代码:

#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

void handler(int, siginfo_t *, void*);

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) < 0) {
perror("sigaction error");
exit(0);
}
for(; ;)
pause();
return 0;
}

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);
}
接收端程序代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
if(argc != 2) {
fprintf(stderr, "Usage %s pid\n", argv[0]);
exit(0);
}
pid_t pid = atoi(argv[1]);
union sigval v;
v.sival_int = 100;
sigqueue(pid, SIGINT, v);
sleep(3);
return 0;

}
输出结果

发送端:

 

接收端:

 

 

2. 测试代码:

发送端程序:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
if(argc != 2) {
fprintf(stderr, "Usage %s pid\n", argv[0]);
exit(0);
}
pid_t pid = atoi(argv[1]);
union sigval v;
v.sival_int = 100;
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(3);
kill(pid, SIGUSR1);
return 0;
}
接收端程序:

#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

void handler(int sig);

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 = 0;

sigset_t s;
sigemptyset(&s);
sigaddset(&s, SIGINT);
sigaddset(&s, SIGRTMIN);
sigprocmask(SIG_BLOCK, &s, NULL);
if(sigaction(SIGINT, &act, NULL) < 0) {
perror("sigaction error");
exit(0);
}
if(sigaction(SIGRTMIN, &act, NULL) < 0) {
perror("sigaction error");
exit(0);
}
if(sigaction(SIGUSR1, &act, NULL) < 0) {
perror("sigaction error");
exit(0);
}
for(; ;)
pause();
return 0;
}

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);
}
}
输出结果:

发送端:

 

接收端:

 

 

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/isunbin/article/details/83997138

 

标签:sigqueue,函数,int,pid,sig,act,include,sigaction
From: https://www.cnblogs.com/im18620660608/p/18003072

相关文章

  • Linux——信号处理函数sigaction()
    sigaction()作用:设置某信号的信号处理函数1.sigaction()函数原型#include<signal.h>intsigaction( intsigno, conststructsigaction*restrictact, structsigaction*restrictoldact );12345signo:指定的信号,可以为除SIGKILL及SIGSTOP外的任何信号act:信号处理方式......
  • 无涯教程-substring()函数
    此方法返回String对象的子集。substring()-语法string.substring(indexA,[indexB])indexA   - 小于字符串长度的0到1之间的整数。indexB   - (可选)0到字符串长度之间的整数。substring()-返回值substring方法根据给定的参数返回新的子字符......
  • 无涯教程-substr()函数
    此方法以指定的字符数返回从指定位置开始的字符串中的字符。substr()-语法string.substr(start[,length]);start   -开始提取字符的位置(0到1的整数,小于字符串的长度)。length  -要提取的字符数substr()-返回值substr()方法根据给定的参数返回新的子字符......
  • 无涯教程-split()函数
    此方法通过将字符串对象分成子字符串来将字符串对象拆分为字符串数组。split()-语法string.split([separator][,limit]);separator  - 指定用于分隔字符串的字符。limit      - 整数,用于指定要找到的分割数的限制。split()-返回值split方法返回......
  • 欧拉函数——gcd问题的一大利器
    定义欧拉函数,即\(\varphi(n)\),表示的是\([1,n]\)内与\(n\)互质的数的个数,如\(\varphi(1)=1\)。若\(n\)是质数,显然\(\varphi(n)=n-1\)。性质欧拉函数是积性函数。若\(\gcd(a,b)=1\),则\(\varphi(a\timesb)=\varphi(a)\times\varphi(b)\)。更特殊......
  • 无涯教程-slice()函数
    此方法提取字符串的一部分并返回新的字符串。slice()-语法string.slice(beginslice[,endSlice]);beginSlice  - 从零开始的索引。endSlice   - 终止提取的从零开始的索引。如果省略,则slice提取到字符串的末尾。slice()-返回值如果成功,slice返......
  • C 库函数 - signal()
    C标准库-<signal.h>描述C库函数 void(*signal(intsig,void(*func)(int)))(int) 设置一个函数来处理信号,即带有 sig 参数的信号处理程序。声明下面是signal()函数的声明。void(*signal(intsig,void(*func)(int)))(int)参数sig --在信号处理程序中作为......
  • 代数最值与函数随记
    代数式\(ax^2+bx+c\)的最值是(),()时有最大值,()有最小值。代数解\[ax^2+bx+c\]\[=a(x^2+\frac{b}{a}x)+c\]\[=a[(x^2+\frac{b}{a}x+(\frac{b}{2a})^2]+c-\frac{b^2}{4a^2}·a\]\[=a(x+\frac{b}{2a})^2+\frac{4ac-b^2}{4a}\]\(\therefore\)易得,当\(x=-\fr......
  • 无涯教程-lastIndexOf()函数
    此方法返回最后一次出现的指定值的调用字符对象内的索引,如果没有找到该值,则从fromIndex或-1开始搜索。lastIndexOf()-语法string.lastIndexOf(searchValue[,fromIndex])searchValue  - 表示要搜索的值的字符串。fromIndex   - 调用字符串中开始搜索的位......
  • 设计奖励,奖励函数
        1  1  1 1 1  1  11  111  1  1 1  1 1 1  1 1 1    1 11  1  1 1  11 1 1  11  111  ppo1 1 1 11 1......