一、进程与信号不可靠问题

linux信号不可靠问题:

  1,系统层面信号是可靠的

  2,用户层面信号是可靠的

    将依赖于信号而执行的代码放在信号处理函数中执行,否则这些代码将不被执行

进程在处理过程中是否还可以接收处理信号,相同信号/不同信号

范列

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

void set_signal(int signo)
{
    if(signo == SIGINT)
    {
        printf("%d catch SIGINT\n",getpid());
        sleep(5);
        printf("process the sigint finished\n");
    }
    if(signo==SIGTSTP)
    {
        printf("%d catch SIGTSTP\n",getpid());
        sleep(5);
        printf("process the sigtstp finished\n");
    }
}

int main()
{
    if(signal(SIGINT,set_signal)==SIG_ERR)
    {
        printf("signal error\n");
        return 1;
    }
    if(signal(SIGTSTP,set_signal)==SIG_ERR)
    {
        printf("signal error\n");
        return 1;
    }
    
    //暂停等待信号
    while(1) pause(); 
}

编译执行

进程处理中中发送相同信号,先发送ctrl+c 在发送ctrl+c
^C3267 catch SIGINT
^Cprocess the sigint finished
3267 catch SIGINT
process the sigint finished

进程处理中发送不同信号,先发送ctrl+c 在发送ctrl+z
^C3267 catch SIGINT
^Z3267 catch SIGTSTP
process the sigtstp finished
process the sigint finished

结论

进程处理中发送相同信号/不同信号仍然会处理,但是超过2次进程就会屏蔽

信号处理中:信号会创建2个数据结构

  信号屏蔽字(mask)
  信号未决字(pending)

 用户层面信号可靠性

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int signal_init=0;

void set_signal(int signo)
{
    printf("catch signal %d\n",signo);
    signal_init =1;
}

int main()
{
    if(signal(SIGINT,set_signal)==SIG_ERR)
    {
        printf("set signal error");
    }
    
    while(signal_init == 0)
    {
        sleep(5);
        pause();
    }
    
    printf("process run success\n");
    
    return 0;
}

 

转载于:https://www.cnblogs.com/peixiguang/p/5862042.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值