Note: The Context Environment in this article is the Unix/Linux operating system, G ++ compiling environment, and GDB debugging environment.
Signal is a good combination of blocking modes. Some programs that require low-speed calls, such as socket and Oracle connections, will consume a lot of CPU overhead if they work in non-blocking mode, the blocking mode makes the program unable to process the operations that require real-time response in a timely manner. Signal can wake up the program from the blocking state to handle emergencies and then do other things.
Chapter 10 of unix_program_advance describes the signal capture functions provided by UNIX systems in detail.
Note the following during development:
1. determine whether to use blocking or non-blocking. In OCI and socket, both are blocked by default.
In socket, you can use fcntl () to setO_nonblock,O_ndelay.
If (fcntl (FD, f_setfl, orig_flags | o_nonblock) <0)
{
}
In OCI, you can use the oci_attr_nonblocking_mode attribute value of the ociattrset () function to set the non-blocking mode for OCI operations. For detailed usage, see nonblocking mode in OCI in Oracle call interface programmer's guide B10779-01. Chapter 2.
When the socket is in blocking mode, you can use setsockopt to set the Recv timeout time.
1. Add an alarm ()
In Oracle, if the network is disconnected when you call Oracle. The function is always blocked. Therefore, it is necessary to add an alarm () before functions that may be blocked to wake up after timeout.
The following example shows how to use the signal. After the function has been called for more than 3 seconds, the system jumps out of the function and reconnects to the database to perform this operation. If the operation fails three times, the operation is abandoned.
Do
{
Alarm (3 );
If (table. Query () = false & errno = eintr)
{
Conndb ();
}
Else
{
Break;
}
} While (retrytimes <3)
Alarm (0 );
2. Use sigaction () instead of signal () to set the signal hook. Signal () has poor cross-platform performance. In Linux, it runs a very robust program and performs very poorly on HP-Unix or IBM-Aix, while signal on Solaris performs differently. Compared with signal, sigaction defines whether or not the interrupted low-speed call is re-called (not aroused by default); sigactionl () also has a blocking function, this allows the signal processing function to focus on the current signal.
3. Call the previously defined signal processing function before the current signal processing function ends.
Sigaction (signo, null, oldhandle );
4. You can set to capture all signals to avoid unexpected exit when the program receives the signal. The boundary of the signal number is 1 and the highest boundary is sigmax. You can use for () loop to set the hook.
5. The child process inherits the signal hook of the parent process, but cannot inherit the signal (for example, the alarm signal) that has been set in the parent process ).
6. If you want to jump out of the current context after receiving the signal, use throw ().
Related information:
1. <The design of the UNIX operationg system> from the perspective of operating system implementation, describes in detail the signal classification, how the signal is awakened, and under what circumstances the signal will be lost or cause an accident. (Note: The content discussed in the book is the original version of signal (). In fact, sigaction has solved the problem of signal loss and abnormal signal .)
2. <advanced programming in the Unix environment> Chapter 4 describes the signal Implementation of unreliable signal and reliable signal sigaction in posix.1, svr4, and 10th + BSD. The source code for signal processing is provided.
Complete example of signal usage:
A very useful use of the signal is to wake up the process from the blocking state.
Signals have certain limitations. A signal cannot transmit a message structure like a message, but can only transmit a simple signal.
The signal will destroy the process of the program. Because we don't know when the signal will happen.
A reasonable solution is:
An exception is thrown when a process receives a message of concern. In this way, the program can be stopped as needed.
Example: