Signals in Linux

Source: Internet
Author: User

Signal is a mechanism used in Linux to communicate with each other or operate on processes. Signal is a widely used topic. Here, we only discuss several of the most important signals and techniques for using signal control processes.
A signal is a special message sent to a process. The signal mechanism is asynchronous. When a process receives a signal, it immediately processes the signal without waiting for the current function or even a line of code to end running. There are dozens of signals, which represent different meanings. Signals are differentiated by their values, but the names of signals are usually used in programs to represent a signal. In Linux, these signals and constants named after them are defined in the/usr/include/bits/signum. h file. (Usually the program does not need to directly include this header file, but should contain <signal. h> .)
When a process receives signals based on different dispositions, the process may execute one of several different operations. Each signal has a default processing method (default disposition). When a process does not specify its own processing method for a signal, the default processing method is used to respond to the corresponding signal. For most types of signals, the program can freely specify a processing method-the program can choose to ignore this signal, or call a specific signal processing function. If a signal processing function is specified, the current program suspends the current execution process and starts to execute the signal processing function. When the signal processing function returns, it continues to execute the function from the paused position.
When the Linux system encounters special conditions, it also sends a signal to the process. For example, when a process executes an illegal operation, it may receive signals such as SIGBUS (Main Line Error), SIGSEGV (segment overflow error), and SIGFPE (floating point exception. The default processing method for these signals is to terminate the program and generate a core file ).
In addition to the system signal, a process can send signals to other processes. One of the most common applications of this mechanism is to end other processes by sending SIGTERM or SIGKILL signals. 3 #3 In addition, it is also common in sending commands to running processes. Two "user-defined" signals, SIGUSR1 and SIGUSR2, are specifically used for this purpose. The SIGHUP signal is sometimes used for this purpose-usually used to wake up a waiting process or make the process re-read the configuration file.
The system calls sigaction to specify the signal processing method. The first parameter of the function is the signal value. The next two parameters are two pointers to the sigaction structure. The first one is the processing method to be set, and the second is used to save the previous processing method. The most important two sigaction structures are the sa_handler domain. It can be the following three values:
 
<Span style = "font-size: 18px;"> · SIG _ DFL, specifying the default Signal Processing Method
· SIG _ IGN: specifies that the signal will be ignored.
· A pointer to the signal processing function. This function should take the signal value as a unique parameter and has no return value.
</Span>
 
Because signal processing is asynchronous, when the signal processing function is called, the main program may be in a very fragile state, and this state will remain until the end of the signal processing function. Therefore, do not use the input and output functions, most library functions, and system calls in signal processing functions.
The signal processing function should do as little work as possible to respond to the arrival of the signal, and then return to the main program to continue running (or end the process ). In most cases, only the arrival of the signal is recorded. The main program regularly checks whether there is any signal arriving, and makes corresponding processing for the current situation.
The signal processing function may also be interrupted by the arrival of other signals. Although this situation sounds very rare, once it appears, it will be very difficult for the program to identify the problem and debug it. It is even unsafe to assign values to global variables, because one assignment operation may be completed by two or more machine commands, and a second signal may arrive during the execution of these commands, the modified global variables are in incomplete state. If you need to set a global flag from the signal processing function to record the arrival of the signal, this flag must be a special type of sig_atomic_t instance. Linux ensures that only one machine command is required for assigning values to this type of variable, so you don't have to worry about being interrupted in the middle. In Linux, sig_atomic_t is the basic int type. In fact, operations on int or smaller Integer Variables and pointer assignment are atomic operations. However, if you want the program to be written to any standard UNIX system, set all global variables to the sig_atomic_t type.
In the simple program in code list 3.5, we use the signal processing function to count the number of times the program receives the SIGUSR1 signal at runtime. The SIGUSR1 signal is a signal reserved for the application.
Code List 3.5 (sigusr1.c) using signal processing functions
 
<Span style = "font-size: 18px;" >#include <signal. h>
# Include <stdio. h>
# Include <string. h>
# Include <sys/types. h>
# Include <unistd. h>
Sig_atomic_t sigusr1_count = 0;
Void handle (int signal_number)
{
++ Sigusr0000count;
}
Int main ()
{
Struct sigaction sa;
Memset (& sa, 0, sizeof (sa ));
Sa. sa_handler = & handler;
Sigaction (SIGUSR1, & sa, NULL );
/* You can execute some work for a long time here. */
/*...*/
Printf ("SIGUSR1 was raised % d times \ n", sigusr0000count );
Return 0;
} </Span>


From the w397090770 Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.