In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how Linux achieves signal capture, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
##signal function
Function prototype:
sighandler_t signal(int signum, sighandler_t handler);
where the definition of sigandler is: typedef void (* sigandler_t)(int);
Function role: register a signal capture function, that is, received a signal, it executes the registered callback function.
Function parameters:
signum: signal number, try to write it in macro instead of number, which is more suitable for cross-platform;
handler: registered callback function;
Function defects:
Due to historical reasons, this function may have different effects in different versions of Unix and Linux systems, so cross-platform is not good, try to avoid using it, and use the sigaction function with better versatility instead.
#include #include void func() { printf("SIGQUIT catched!\ n"); } int main() { signal(SIGQUIT, func); while(1); }
##sigaction
Function prototype:
int sigaction(int signum, const struct sigaction act, struct sigaction oldact);
Function role: similar to the signal function, used to register a signal capture function;
Return value:
Success: 0; Failure: -1, and set errno;
Parameters:
signum: signal number, try to write it in macro instead of number, which is more suitable for cross-platform;
act: passed parameter, new signal capture method;
oldact: outgoing parameter, old signal capture method
Here, pay special attention to the struct sigaction structure in the parameter, which is also the difficulty of this function. The following is a detailed explanation:
struct sigaction
Prototype:
struct sigaction { void (*sa_handler)(int); void (sa_sigaction)(int, siginfo_t , void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); };
This struct has a lot of members, and many of them are in the form of callback functions, which is daunting. But in reality, there are only three that need to be mastered.
First of all, sa_restorer and sa_sigaction are two members, one of which is deprecated and the other is rarely used, so let's leave them alone and focus on the remaining three.
(1)sa_handler: Specify the processing function after signal capture, i.e. register callback function. This member can also be assigned SIG_IGN, indicating that the signal is ignored, or registered as SIG_DFL, indicating that the default action of the signal is performed.
(2)sa_mask: temporary blocking signal set (or signal mask word) Let's first look at a scenario like this:
A signal has a callback function registered. When the kernel passes this signal, it will first pass through a blocking signal set and block some signals first. Then execute the corresponding callback function. The following diagram shows:
If, say, this callback function callback execution time is relatively long, such as 2 seconds, in these 2 seconds, there are other signals coming, then the process is to suspend the current callback function, to respond to the new signal, or regardless of the new signal, first the current callback function to finish processing again?
The correct approach is to use sa_mask to temporarily replace the blocking signal set of the process during the execution of the callback function, so as to ensure that the callback function is safely executed, and then remove the replacement. Note: This process only occurs during callback execution and is a temporary setting.
(3)sa_flags: Usually set to 0, meaning default attributes are used.
Let's look at another scenario:
For example, a process registers a callback function for SIGQUIT, and when the callback function is executed, the SIGQUIT function comes again. At this time, does the process respond or not respond to the signal? This is one of sa_flags 'functions. When it is set to 0, it means that the default property is used, that is, the signal is not responded to first, but the callback function is executed and then the signal is processed.
In addition, blocking regular signals do not support queuing, that is, thousands of the same signal during the execution of the callback function, the system records only once. The next 32 real-time signals support queuing.
#include #include #include void func(int signal) { printf("SIGQUIT catched!\ n"); sleep(2); //used to simulate callback function execution for a long time printf("func finished!\ n"); } int main() { struct sigaction act; act.sa_handler = func; sigemptyset(&act.sa_mask); //clear the temporary blocking signal set first sigaddset(&act.sa_mask, SIGINT); //Mask SIGINT during callback function execution act.sa_flags = 0; sigaction(SIGQUIT, &act, NULL); //Register callback function while(1); return 0; } Thank you for reading this article carefully. I hope that the article "How to achieve signal capture in Linux" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.