Signal set operation function

Control principle: The kernel determines whether a signal should be processed by reading the set of pending signals. The signal mask word mask can affect the set of pending signals. And we can customize the set in the application to change the mask. The purpose of blocking the specified signal has been achieved.

For more specific  man *** methods of using functions, see the manual. 

Analysis of the usage principle of signal set operation function https://www.bilibili.com/video/BV1KE411q7ee/?p=134&spm_id_from=pageDriver&vd_source=d239c7cf48aa4f74eccfa736c3122e65

 Signal set settings

sigset_t  set;                   

                                Custom signal set // typedef unsigned long sigset_t;

int sigemptyset(sigset_t *set);         

                                Clear a certain signal set to 0. Success: 0; Failure: -1

int sigfillset(sigset_t *set);               

                                 Set a signal set to 1 Success: 0; Failure: -1

int sigaddset(sigset_t *set, int sign);         

                                Add a signal to the signal set Success: 0; Failure: -1

int sigdelset(sigset_t *set, int sign);           

                                Clear a signal from the signal set. Success: 0; Failure: -1

int sigismember(const sigset_t *set, int sign);

                                Determine whether a signal is in the signal set 

                                Return value: In set: 1; Not in set: 0; Error: -1 

The essence of the sigset_t type is a bitmap.

                However, bit operations should not be used directly, but the above functions should be used to ensure that cross-system operations are valid.

Set the signal masking word and remove the signal masking word

This function is also used to shield signals and unblock them . Its essence is to read or modify the signal mask word of the process (in PCB)

int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

        Success: 0; Failure: -1, set errno

        parameter:

                set: custom set

                         The incoming parameter is a bitmap. Which bit in the set is 1 indicates which signal is blocked by the current process.

                oldset: old mask

                        Pass out parameters and save the old signal mask set.

                how:

                        SIG_BLOCK: Set blocking

                        SIG_UNBLOCK: Unblock

                        SIG_SETMASK: replace mask with custom set

 View the set of pending signals:

        int sigpending(sigset_t *set); 

                set: Outgoing pending signal set

                Return value: Success: 0; Failure: -1, set errno

Case 1: Block the ctrl+c signal and print the pending signal set

It can be clearly seen from the results that when we press ctrl+c, the pending signal machine No. 2 position changes to 1 and remains there. This is because we set up a sigprocmaskblocking signal set using · and the signal cannot be delivered.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<pthread.h>
#include<signal.h>

void sys_err(const char *str)
{
    perror(str);
    exit(1);
}

void print_set(sigset_t *set) //打印操作 
{
    int i;
    for(i = 1; i < 32; i++)
    {
        if(sigismember(set, i)){ //利用成员判断函数
            putchar('1');
        } else {
            putchar('0');
        }
    }
    printf("\n");
}


int main(int argc, char *argv[])
{
    sigset_t set, oldset, pedset;

    int ret = 0;

    sigemptyset(&set);         // 清空
    sigaddset(&set, SIGINT);   //屏蔽 ctrl + c
    sigaddset(&set, SIGQUIT);  //屏蔽 ctrl + \
    sigaddset(&set, SIGBUS);   //屏蔽 总线
    sigaddset(&set, SIGKILL);  //屏蔽 kill   9号信号无法屏蔽

    //SIG_BLOCK 表示添加阻塞信号 set表示要覆盖的信号集 oldset表示旧的信号集
    ret = sigprocmask(SIG_BLOCK, &set, &oldset); //设置阻塞
    if(ret == -1){
        sys_err("sigprocmask error");
    }
    
    while(1)
    {
        ret = sigpending(&pedset);  //获取未决信号集 传出
        if(ret == -1){
            sys_err("sigpending error");
        }

         print_set(&pedset);  //打印 自定义的函数
         sleep(1);
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43200943/article/details/129837178