Interpretation of soft interrupt communication and signal()

Table of contents

 soft interrupt communication

signal()

overview

signal() similar functions

SIGINT of signal()

signal()之SIGHT TERM 

SIGALRM of signal() 

SIGQUIT of signal() 

 SIG_IGN uses


 soft interrupt communication

There are many ways to communicate between processes, among which soft interrupt communication is a common way, which is based on the signal mechanism and can communicate between different processes. The implementation of soft interrupt communication is that one process sends a specific signal to another process, and the signal is captured by the receiving process and processed accordingly.

The steps of soft interrupt communication are as follows:
1) The sending process sends a signal to the receiving process through the kill() function, and the signal type can be specified by itself.
2) The receiving process can register the processing function of the corresponding signal through the signal() function, or other similar functions can specify the processing method of the corresponding signal.
3) When the receiving process receives the signal, the operating system will suspend the normal execution process of the receiving process, and then execute the corresponding signal processing function or processing method.
4) After processing the signal, the operating system will resume the normal execution flow of the receiving process.

It should be noted that soft interrupt communication is an asynchronous communication method, that is, there is no direct synchronization mechanism between the sending process and the receiving process. The sending process just sends a signal to the receiving process, and the receiving process does not know when the signal will be received, so it needs to register the signal processing function or processing method to realize the processing of the signal. Therefore, when designing a system using soft interrupt communication, it is necessary to fully consider the impact of this asynchronous mechanism.

signal()

overview

The signal() function is a system call in UNIX/Linux to register a signal handler. Signal is a notification mechanism in the operating system, which is used to notify the process that certain events have occurred, such as receiving an external interrupt, receiving a signal sent by a process, and so on. When the system receives a signal, it interrupts the normal execution flow of the process and executes the signal handler instead.

The signal() function is used to register a signal handler. When the process receives the specified signal, the registered signal handler is executed. handler can be a function pointer, or SIG_IGN or SIG_DFL. If handler is a function pointer, it is registered as a signal handler. If the handler is SIG_IGN, it means ignore the signal without any processing. If the handler is SIG_DFL, it means adopt the system default signal processing method.

void (*signal(int signum, void (*handler)(int)))(int);

Among them, signum is the number of the signal, and handler is a function pointer pointing to the signal handler. The return value of the function is the original signal handler. 

signal() similar functions

In addition to the signal function, there are some similar functions, which are used to
process signals in the process:
1) sigaction function: Unlike the signal function, the sigaction function can set the signal processing method, including capturing signals and ignoring signals , restore the default processing method, etc.
2) sigprocmask function: used to set the signal mask of the process and control which signals can be received by the current process.
3) sigsuspend function: used to temporarily suspend the current process, waiting to receive a specified signal. When this signal is received, the process resumes execution.
4) sigqueue function: used to send a specified signal to the specified process, with an integer value as additional data.
5) raise function: used to send a specified signal to the current process, which is equivalent to using the kill function to send a signal to itself.
These functions have different characteristics and uses in signal processing, and developers can choose suitable functions to use according to their own needs.

SIGINT of signal()

SIGINT is a signal generated by the keyboard, which means to interrupt the process. When the user presses the Ctrl + C key combination on the controlling terminal, a SIGINT signal is sent to all processes in the foreground process group.

//简单的SIGINT示例
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void sigint_handler(int signum) {
    printf("Received SIGINT signal. Terminating.\n");
    exit(0);
}
int main() {
    signal(SIGINT, sigint_handler);
    printf("Press Ctrl+C to terminate the program.\n");
    while (1) {
        // Do nothing
    }
    return 0;
}

signal()之SIGHT TERM 

SIGTERM is a termination signal sent to a process when the process terminates. It allows processes to do some cleanup and terminate gracefully, because a process that receives a SIGTERM signal can catch that signal and perform some cleanup before terminating the process. If the process does not handle the SIGTERM signal, the operating system will terminate the process by default.
Unlike the SIGKILL signal, the SIGTERM signal can be caught and handled by the process, and the behavior of the signal is configurable. Therefore, it is generally recommended to first try sending the SIGTERM signal when a process needs to be stopped, so that the process has a chance to clean itself up and terminate gracefully. For example, the SIGTERM signal can be handled by registering a signal handler function in the process.

//简单的SIGTERM示例
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void sigterm_handler(int signum) {
    printf("Received SIGTERM signal, terminating process...\n");
    exit(0);
}
int main() {
    // 注册SIGTERM信号处理函数
    signal(SIGTERM, sigterm_handler);
    printf("Process is running...\n");
    while(1) {
    // 进程执行某些操作
        sleep(1);
    }
    return 0;
}

SIGALRM of signal() 

SIGALRM is a timer signal, when the timer set by alarm() or setitimer() expires, the SIGALRM signal will be sent. When using the signal() function to capture the SIGALRM signal, you need to pay attention to the following points:
1) The processing function of the SIGALRM signal should be as simple as possible, because it is usually processed when the timer expires, which may affect the running speed of the program .
2) Concurrency needs to be considered when processing SIGALRM signals, because there may be multiple SIGALRM signals arriving at the same time, and appropriate concurrency control measures need to be adopted.
3) Use alarm() or setitimer() to set the timer. If you want to continue receiving SIGALRM signal after processing the SIGALRM signal, you need to reset the timer.

//简单的SIGTERM示例
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sigalrm_handler(int signo) {
    printf("Caught SIGALRM signal.\n");
    // reset timer
    //alarm(5);
    exit(0);
}
int main() {
    // set timer
    alarm(5);
    // register signal handler
    signal(SIGALRM, sigalrm_handler);
    // wait for signal
    while(1) {
        sleep(1);
    }
    return 0;
}

SIGQUIT of signal() 

The SIGQUIT signal is a process-terminating signal that can be sent by pressing Ctrl+\ (CTRL + backslash) in the terminal. When a process receives a SIGQUIT signal, it exits forcefully and generates a core dump before exiting. In C language, you can use the signal function to register a handler for the SIGQUIT signal.

//简单的SIGQUIT示例
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void quit_handler(int signum) {
    printf("Received SIGQUIT signal (%d), terminating.\n", signum);
    exit(0);
}
int main() {
    // 注册SIGQUIT信号处理函数
    signal(SIGQUIT, quit_handler);
    printf("Press Ctrl+\\ to terminate the program.\n");
    while (1) {
        // 进程执行的操作
    }
    return 0;
}

 SIG_IGN uses

Use the SIG_IGN parameter to ignore the signal, and the program will not be affected by the signal. For example:

signal(SIGINT, SIG_IGN); indicates that when receiving SIGINT (that is, an interrupt signal, generally triggered by the user pressing Ctrl-C), ignore it, that is, do not process it. This is usually set to ignore in order to prevent the user from accidentally pressing Ctrl-C and causing the program to exit abnormally.

signal (SIGQUIT, SIG_IGN); This code uses the signal() function to change the behavior of the program when it receives the SIGQUIT signal (usually generated by the user typing "Ctrl+\" on the terminal). Specifically, the SIG_IGN parameter is used, indicating that the signal is ignored, and the program will not be affected by the signal.

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/131480219