实时信号阻塞队列大小测试
1 #include <assert.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <signal.h> 6 #include <unistd.h> 7 /* 测试性能 */ 8 void my_sigaction(int signum, siginfo_t *info, void *dat) 9 { 10 int ret = 0; 11 if (SIGUSR1 == signum) 12 { 13 sigset_t set; 14 ret = sigemptyset(&set); 15 ret = sigaddset(&set, SIGINT); 16 ret = sigaddset(&set, SIGRTMIN); 17 ret = sigprocmask(SIG_UNBLOCK, &set, NULL); 18 printf("解除阻塞 recv sig num:%d\n", signum); 19 } 20 else if (SIGINT == signum || signum == SIGRTMIN) 21 { 22 printf("收到的数据是: %d\n", info->si_value.sival_int); 23 } 24 else 25 { 26 printf("其他recv sig num:%d\n", signum); 27 printf("收到的数据是 %d\n", info->si_value.sival_int); 28 } 29 } 30 int main(void) 31 { 32 pid_t pid; 33 int ret = 0; 34 struct sigaction act; 35 36 37 // 设置回调函数 38 act.sa_sigaction = my_sigaction; 39 act.sa_flags = SA_SIGINFO; // 千万别忘记了 40 // 注册非实时信号的处理函数 41 ret = sigaction(SIGINT, &act, NULL); 42 assert(!(-1 == ret)); 43 // 注册实时信号的处理函数 44 ret = sigaction(SIGRTMIN, &act, NULL); 45 assert(!(-1 == ret)); 46 // 注册用户自定义信号 47 ret = sigaction(SIGUSR1, &act, NULL); 48 assert(!(-1 == ret)); 49 50 51 52 // 把 SIGINT SIGRTMIN 都添加到阻塞状态字中 53 sigset_t set; 54 ret = sigemptyset(&set); 55 ret = sigaddset(&set, SIGINT); 56 ret = sigaddset(&set, SIGRTMIN); 57 ret = sigprocmask(SIG_BLOCK, &set, NULL); 58 59 60 61 pid = fork(); 62 assert(!(-1 == ret)); 63 if (0 == pid) 64 { 65 union sigval value; 66 int i = 0; 67 value.sival_int = 0; 68 // Linux 内核 实时信号压力测试 69 for (i = 0; i < 9*1024; i++) 70 { 71 // 发送信号 72 value.sival_int++; 73 ret = sigqueue(getppid(), SIGRTMIN, value); 74 //assert(!(-1 == ret)); 75 //printf("发送可靠信号ok i = %d\n", i); 76 } 77 printf("signal max id is = %d\n", i); 78 // 向父进程发送 SIGUSR1 解除阻塞 79 ret = kill(getppid(), SIGUSR1); 80 //assert(!(-1 == ret)); 81 } 82 while (1) 83 { 84 sleep(1); 85 } 86 return 0; 87 }
可以用 ulimit -a 查看 linux 系统pending signal 大小