linux信号学习02

  1. 未决信号集与阻塞信号集(信号屏蔽字)
    阻塞信号集: 将某些信号加入集合,对他们设置屏蔽,当屏蔽x信号后,再收到该信号,该信号的处理将推后(解除屏蔽后)
    未决信号集:
    a. 信号产生,未决信号集中描述该信号的位立刻翻转为1,表信号处于未决状态。当信号被处理对应位翻转回为0。这一时刻往往非常短暂。
    b. 信号产生后由于某些原因(主要是阻塞)不能抵达。这类信号的集合称之为未决信号集。在屏蔽解除前,信号一直处于未决状态。
    相当于 阻塞信号集在未决信号集前面设置了一堵墙
    在这里插入图片描述

  2. 系统api产生信号
    kill函数

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);

参数介绍:
pid >0,要发送的进程ID
pid =0,代表当前进程组内所有进程
pid =-1, 代表有权限发送的所有进程
pid <-1, 代表 -pid对应组内所有进程
sig 对应的信号

kill函数代码示例

de <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int i=0;
    pid_t pid;
    for(; i<5; ++i) {
        pid = fork();
        if(pid==0){
            break;
        }
    }
    if(i=2) {
        printf("son---%d will kill fatherProgress\n",i);
        sleep(5);
        kill(getppid(), SIGKILL);
        while (1) {
            sleep(1);
        }
    } 
    if(i=5) {
        while(1) {
            printf("father ----\n");
            sleep(1);
        }
    }
    return 0;
}

–分割线–
alarm
man alarm

 #include <unistd.h>
 unsigned int alarm(unsigned int seconds);

alarm() arranges for a SIGALRM signal to be delivered to the calling
process in seconds seconds.
If seconds is zero, any pending alarm is canceled.
In any event any previously set alarm() is canceled.
alarm() 函数 给调用者(自己)发送 一个 SIGALRM 信号,在 seconds秒后。
如果 seconds是0, 取消之前的 设置的 alarm。

return返回值
返回之前设置的 alarm剩余的秒数,如果之前没有设置alarm,就返回0.

alarm代码示例:

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

int main() {
    int ret_1 = alarm(5);
    printf("%d\n", ret_1);
    sleep(2);
    int ret_2 = alarm(4);
    int num = 4;
    printf("%d\n", ret_2);
    while (1) {
        printf(" will ararm fater %d\n", num--);
        sleep(1);
    }
    
    return 0;
}

–分割线–
setitimer函数,周期性发送信号
man setitimer

#include <sys/time.h>
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value,
struct itimerval *old_value);

参数介绍:
which 三种选择

真实时间
 ITIMER_REAL    decrements in real time, and delivers SIGALRM upon expi‐
                ration.

计算进程执行时间
 ITIMER_VIRTUAL decrements  only  when  the  process  is  executing, and
                delivers SIGVTALRM upon expiration.

计算进程执行 + 调度时间
ITIMER_PROF    decrements both when the process executes and  when  the
               system  is  executing on behalf of the process.  Coupled
                with ITIMER_VIRTUAL, this timer is usually used to  pro‐
                file  the time spent by the application in user and ker‐
                nel space.  SIGPROF is delivered upon expiration.
struct itimerval {
    struct timeval it_interval; /* Interval for periodic timer */  周期定时器间隔
    struct timeval it_value;    /* Time until next expiration */ 下次执行时间
};

struct timeval {
    time_t      tv_sec;         /* seconds */ 秒数
    suseconds_t tv_usec;        /* microseconds */ 微秒数
};

new_value 要设置的闹钟时间
old_value 原闹钟时间

return: 成功返回0, 失败-1,并设置errno
setitimer 代码示例:

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

void catch(int sign) {
    if(sign == SIGALRM) {
        printf("catch signal is %d\n", sign);
    }   
}
int main() {
    signal(SIGALRM, catch);
    struct itimerval new_value = {
        {3, 0},
        {1, 0}
    };
    setitimer(ITIMER_REAL, &new_value,  NULL);
    while (1){
        printf(" setitimer test\n");
        sleep(1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值