Further understanding of the signal

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

// 信号处理函数
void sig_usr(int signo)
{
    
    
    if (signo == SIGUSR1)
    {
    
    
        printf("收到了SIGUSR1信号,我休息15秒......!\n");
        sleep(15);
        printf("收到了SIGUSR1信号,我休息15秒完毕,苏醒了......!\n");
    }
    else if (signo == SIGUSR2)
    {
    
    
        printf("收到了SIGUSR2信号,我休息5秒......!\n");
        sleep(5);
        printf("收到了SIGUSR2信号,我休息5秒完毕,苏醒了......!\n");
    }
    else
    {
    
    
        printf("收到了未捕捉的信号%d!\n", signo);
    }
}

int main(int argc, char* const* argv)
{
    
    
    // 系统函数,第一个参数是个信号,第二个参数是个函数指针,代表一个针对该信号的捕捉处理函数
    if (signal(SIGUSR1, sig_usr) == SIG_ERR)
    {
    
    
        printf("无法捕捉SIGUSR1信号!\n");
    }

    if (signal(SIGUSR2, sig_usr) == SIG_ERR)
    {
    
    
        printf("无法捕捉SIGUSR2信号!\n");
    }

    for (;;)
    {
    
    
        sleep(1);
        printf("休息1秒~~~~!\n");
    }

    printf("再见!\n");

    return 0;
}

(1) As shown in the figure below, use kill to send the USR1 signal to the process, and the execution of the signal processing function is stuck for 15 seconds. At this time, because the process cannot return to main(), the statements in main() cannot be executed.

insert image description here

(2) Use kill to send the SIGUSR1 signal to the process. During the period when the SIGUSR1 signal is triggered and sleep for 15 seconds, even if the SIGUSR1 signal is triggered multiple times, the signal processing function corresponding to the SIGUSR1 signal will not be re-executed, but will wait for the previous one. After the SIGUSR1 signal processing function is executed, the SIGUSR1 signal processing function is executed for the second time. That is, when a signal handler is called, the new signal mask (sigprocmask()) created by the operating system automatically includes the signal being delivered, thus ensuring that when processing a given signal, if the signal happens again, then it will block until the processing of the previous signal is completed.

(3) No matter how many times the SIGUSR1 signal is sent with kill, during the execution of the signal processing function, all subsequent SIGUSR1 signals will be reduced to 1 11 time. For example, the processing program of the SIGUSR1 signal is currently being executed but the execution has not been completed. At this time, 5 5is sent again.5 SIGUSR1 signals, then when the SIGUSR1 signal handler finishes executing (unblocking), the SIGUSR1 signal handler will only be called1 11 time instead of calling5 5The handler for the 5 SIGUSR1 signals .

insert image description here

(4) When the USR1 signal processing program is executed but the execution is not completed, it can continue to enter the USR2 signal processing program to execute. When sending USR1 and USR2 again, there will be no response.

(5) Since the USR2 signal comes during the execution of the USR1 signal processing program, the USR2 signal processing program is executed again, which means that the USR1 signal processing program will not return to the USR1 signal processing program until the USR2 signal processing program is executed; only After the execution of the USR1 signal processing program is completed, it will finally return to the main flow of the main function to continue execution.

insert image description here

Guess you like

Origin blog.csdn.net/qq_42815188/article/details/129410599