In Linux, a signal is also called a soft interrupt. The process processes the signal after receiving the signal. It can be said that it is an interrupt process. This article will give you a brief explanation of the Linux signaling mechanism.
1, the signal processing function is installed
on the system programming level most directly related to the relationship between the process signals and has two functions, they are used to install the signal processing function: < Br>
sighandler_t signal(int signum, sighandler_t handler);
int sigaction(int signum, const struct sigaction *act,,struct sigaction *oldact);
first function Signal is relatively simple, sighandler_t is an alias, its prototype is typedef void (*sighandler_t) (int), which is a function pointer, accepts a parameter of type int (signal number), returns void. For example, to process the SIGUSR1 signal:
void handler(int sig)
//strsiganl function is to convert the number of the signal to the string of the signal description < Br>
printf(“Rcv a signal:%s”,strsignal(sig));
}
int main()
{
signal(SIGUSR1,handler);
while(1)
;
}
(This program is actually problematic, As I will see later, this program is originally an infinite loop, but sending a SIGUSR1 signal to him, the program will go from the while in the "interruption" to execute the code in the handler. Use the kill command in the shell to send the signal SIGUSR1 and the program promises a message like this: Rcv a signal: User defined signal 1. The usage of signal() is almost as simple as that. However, for portability reasons, the following function should be used when participating in project development.
There are two structures in the parameters of the sigaction() function. The man page prototype is as follows:
struct sigaction {
void (*sa_handler)(int); Br>
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)( Void);
};
As far as I know, sa_handler and sa_sigaction are actually in a union, they are pointers to signal handlers.
sa_mask is the signal to be masked, and sa_flags has several options. (More on these two points later). From the sigaction() prototype, we can find that there are two struct sigaction parameters in the parameter, where act is the signal processing to be installed, and oldact is used to bring back the original processing method so that we can recover after processing the signal. If you don't need to get back the previous signal processing method, you can set the third parameter to NULL. If you only want to get the previous processing method instead of installing a new signal processing, you can set the second parameter to NULL. Signal() is not available. Rewriting the above example with sigaction() is like this:
1 void handler(int sig)
2 {
3 printf(“Rcv a signal:%s&rdquo ;,strsignal(sig));
4 }
5
6 int main()
7 {
8 struct Sigaction act;
9 sigemptyset(&act.sa_mask);
10 act.sa_handler = handler;
11 act.sa_flags = 0;
12 sigaction(SIGUSR1,&act,NULL);
13 while(1)
14 ;
15 } Previous12Next page Total 2 pages
In Linux system operation, in addition to using commands to monitor system performa
Ubuntu and Fedora are the most famous in many Linux distributions. Ubuntu is an open source, ultra-f
Samba is a tool that can implement SMB protocol. You can share Linux system software through Samba s
JITwatch is a code analysis tool for Linux systems. To use JITwatch, you must first
What should I do if there is a mount:Structure needs cleaning error in Linux?
Summary of the usage of the Linux command less command
Ubuntu when copying files prompts how to resolve Permission denied?
Linux changes the way Docker runs the root directory
How Ubuntu System Uses Script to Upgrade Kernel
How to identify Linux system rpm and yum tools
How to sort the connector ld link under Linux
Linux mint how to solve the problem of no sound in the root login
Linux shutdown selinux security subsystem skills
How to turn off recovery mode in Ubuntu 12.04 system
Group Policy Tips: Protection of Computer Shared Directory Security Tips
Windows10 Xiaona can't connect to the network how to do
Win8.1 view system hidden account skills
The best way to get 2 systems together into the C drive
Win10 official version 10240 received three patches to update
Win8 start interface to create a one-button shutdown step
Windows XP System Optimization Acceleration Tips
Running with the Win7 hypervisor
How to make Windows 7 reproduce XP Classic Skip account settings