• 函数sigsuspend


    sigqueue函数原型:

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

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

    分析:

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

    程序清单

    1. 测试代码:

    • 发送端
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <signal.h>
     5 #include <unistd.h>
     6  
     7 int main(int argc, char *argv[])
     8 {
     9     if(argc != 2) 
    10     {
    11         fprintf(stderr, "Usage %s pid
    ", argv[0]);
    12         exit(0);
    13     }
    14     pid_t pid = atoi(argv[1]);
    15     union sigval v;
    16     v.sival_int = 100;
    17     sigqueue(pid, SIGINT, v);
    18     sleep(3);
    19     return 0;  
    20 }
    • 接收端
     1 #include <string.h>
     2 #include <signal.h>
     3 #include <unistd.h>
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6  
     7 void handler(int sig, siginfo_t *info, void *ctx)
     8 {
     9     printf("recv a sig = %d data = %d data = %d
    ", sig, info->si_value.sival_int, info->si_int);
    10 }
    11 
    12 int main(int argc, char *argv[])
    13 {
    14     printf("I'm %d
    ", getpid());
    15     struct sigaction act;
    16     act.sa_sigaction = handler;
    17     sigemptyset(&act.sa_mask);
    18     act.sa_flags = SA_SIGINFO;
    19     
    20     if(sigaction(SIGINT, &act, NULL) < 0) 
    21     {
    22         perror("sigaction error");
    23         exit(0);
    24     }
    25     for(; ;)
    26       pause();
    27     return 0;
    28 }

    输出结果:

    •  发送端

    • 接收端

    2. 测试代码

    • 发送端
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <signal.h>
     5 #include <unistd.h>
     6  
     7 int main(int argc, char *argv[])
     8 {
     9     if(argc != 2) 
    10     {
    11         fprintf(stderr, "Usage %s pid
    ", argv[0]);
    12         exit(0);
    13     }
    14     pid_t pid = atoi(argv[1]);
    15     union sigval v;
    16     v.sival_int = 100;
    17     sigqueue(pid, SIGINT, v);
    18     sigqueue(pid, SIGINT, v);
    19     sigqueue(pid, SIGINT, v);
    20     sigqueue(pid, SIGRTMIN, v);
    21     sigqueue(pid, SIGRTMIN, v);
    22     sigqueue(pid, SIGRTMIN, v);
    23     sleep(3);
    24     kill(pid, SIGUSR1);
    25     return 0;    
    26 }
    • 接收端
     1 #include <string.h>
     2 #include <signal.h>
     3 #include <unistd.h>
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6  
     7 void handler(int sig)
     8 {
     9     if(sig == SIGINT || sig == SIGRTMIN) 
    10     printf("recv a sig = %d
    ", sig);
    11     else if(sig == SIGUSR1)
    12     {
    13         sigset_t s;
    14         sigemptyset(&s);
    15         sigaddset(&s, SIGINT);
    16         sigaddset(&s, SIGRTMIN);
    17         sigprocmask(SIG_UNBLOCK, &s, NULL);        
    18     }
    19 }
    20  
    21 int main(int argc, char *argv[])
    22 {
    23     printf("I'm %d
    ", getpid());
    24     struct sigaction act;
    25     act.sa_sigaction = handler;
    26     sigemptyset(&act.sa_mask);
    27     act.sa_flags = 0;
    28     
    29     sigset_t s;
    30     sigemptyset(&s);
    31     sigaddset(&s, SIGINT);
    32     sigaddset(&s, SIGRTMIN);
    33     sigprocmask(SIG_BLOCK, &s, NULL);
    34     if(sigaction(SIGINT, &act, NULL) < 0) 
    35     {
    36         perror("sigaction error");
    37         exit(0);
    38     }
    39     if(sigaction(SIGRTMIN, &act, NULL) < 0) 
    40     {
    41         perror("sigaction error");
    42         exit(0);
    43     }
    44     if(sigaction(SIGUSR1, &act, NULL) < 0) 
    45     {
    46         perror("sigaction error");
    47         exit(0);
    48     }
    49     for(; ;)
    50       pause();
    51     return 0;
    52 }

     输出结果

    • 发送端

    • 接收端

  • 相关阅读:
    golang闭包,传统斐波那契
    ubuntu 软件桌面图标创建
    Mysql系列-性能优化神器EXPLAIN使用介绍及分析
    Sklearn-GridSearchCV网格搜索
    sklearn逻辑回归(Logistic Regression)类库总结
    scikit-learn模块学习笔记(数据预处理模块preprocessing)
    Python中的高级数据结构
    Python进阶之“属性(property)”详解
    python模块之itertools
    python list有关remove的问题
  • 原文地址:https://www.cnblogs.com/sunbines/p/10268902.html
Copyright © 2020-2023  润新知