首页 > 代码库 > Linux可靠/不可靠信号编程实践

Linux可靠/不可靠信号编程实践

综合案例

    1) 创建子进程与父进程;

    2) 注册SIGINT非实时信号与SIGRTMIN实时信号,并将这两种信号添加到进程屏蔽信号组中;

    3) 注册用户自定义信号;

    4) 子进程发送5次非实时信号,发5次实时信号;

    5) 然后子进程发送SIGUSR1解除进程对SIGINT,SIGTRMIN信号的阻塞

    6) 观察实时信号与非实时信号的区别


//程序示例
void onSigAction(int signalNumber, siginfo_t *sigInfoStruct, void *)
{
    //获取接收到的数据
    int receiveNumber = sigInfoStruct->si_int;

    //如果收到的是SIGUSR1信号,则解除对SIGINT,SIGRTMIN的屏蔽
    if (signalNumber == SIGUSR1)
    {
        sigset_t unblockSet;
        sigemptyset(&unblockSet);
        sigaddset(&unblockSet,SIGINT);
        sigaddset(&unblockSet,SIGRTMIN);;
        sigprocmask(SIG_UNBLOCK,&unblockSet,NULL);

        //Value值会是乱码!
        //cout << "Receive SIGUSR1, Value = http://www.mamicode.com/" << receiveNumber << endl;>

运行结果:

 

 

其实只是收到了一份非可靠信号SIGINT!

 

附-查看系统限制命令:ulimit

xiaofang@xiaofang:~$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 47131
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 47131
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Linux可靠/不可靠信号编程实践