Two years ago, the article was attached to the facade.
--------------------
Signal programming under Linux - Unreliable signal
(Author: mikespook | Published: 2002-12-8 | Views: 135)
Keywords: Linux, Signal, Signal () Preface: This article is just a guideline that is just the same as the rookie like me. If you are a master, or is not interested in programming. Please don't waste time here. What is the signal? In fact, this is a very interesting thing. For example, the car is driving on the road, this is the signal; the red light must stop, this is also a signal. Corresponding to the operating system, it is assumed that the process is a car, then the process can send a signal; if the operating system is assumed to be a road system, the operating system can also send a signal to the process. Signals in Linux are unreliable (or unsafe) signals and reliable signals. Behind I will explain why this is said. First, briefly introduce the general signal in the Linux system: SIGHUP terminal lines hang. This means the suspend operation of the modem line when the carrier signal is lost. However, when it is turned off with the Logout function, it will also be applied to either terminal device. The SIGINT terminal has received the interrupt character. The SIGQUIT terminal has received an exit character. SIGUSR1 user defines signal 1. SIGUSR2 user defines signal 2. SIGTERM is being terminated. Sigchld a child process has terminated. Sigpipe semi-closed pipelines occurred. SigalRM ALARM Function Timer is in time. Let's take a look at the function signal (). The definition of the Signal () function is in the header file signal.h, its function protocol is SIG_T SIGNAL (INT SIG, SIG_T FUNC); the first parameter is a signal that wants to register, the second parameter is to point to the signal processing function. Pointer, or by default. The system provides two default operations: SIG_DEL and SIG_IGN, which are default signal operations and ignore signals. I said a lot of boring things, some fainted. Single knife direct, look at a program. Remember the last multi-process program? I have made some modifications. Make it to the WAIT () function without having to wait until the child process is running, but directly transfer to the signal processing function. Oh, it's complicated. Look at yourself! / *--------------------------------------------------- ----------- * // * mikespook * // * EXERCISE FUNCTION SIGNAL () * // * 2002.7.18 * / # include
#include
#include
#include
#DEFINE FAC_N 65535
/ * Sub-process calling function, here I use a loop to simulate a large background operation. * /
Void Big_Loop (INT N);
/ * The function of the parent process call is actually not put in the function, but it is better to look better for the program's structure or put it in the function. * /
Voidinfut_information ();
/ * Signal Processing function, will transfer to the function when the parent process receives the SIGCHLD signal * /
Void handler ();
int main ()
{
/ * Process number * /
PID_T PID;
/ * Want to use the signal? Then, first register the signal processing function, corresponding to it * /
Signal (SIGCHLD, HANDLER);
/ * The program is "bifurcated" here, the new process is created * /
PID = fork ();
/ * Return value by fork () to determine whether the parent process or child processes * / switch (pid) {
/ * Return -1, very unfortunate, create a process failed. Maybe there is not enough memory space, or there may be too many processes. * /
Case -1:
PERROR ("fork / n");
Break;
/ * Return 0, now run in the child process, then call the operator of the child process. * /
Case 0:
/ * A loop running 65535 times, if your machine is too fast, you can't see the effect of both processes simultaneously, then increase the number of cycles. Or use a SLEEP () function * /
BIG_LOOP (FAC_N);
/ * Get the PID of the child process, you can see that the PID of the child process and the parent process is different (the PID of the child process is larger than the parent process, because it is created after the parent process is running). * /
Printf ("PID:% D / N", getPid ());
/ * The child process is executed, and the signal SIGCHLD is transmitted. Since the previous signal is registered, the signal processing operation will be transferred. * /
Break;
/ * Haha, the return is not a mistake, it is not a child process, that is the parent process. * /
DEFAULT:
/ * Here you have entered 4 numbers * /
INPUT_INFORMATION ();
/ * Acquire the PID of the child process. * /
Printf ("PID:% D / N", getPid ());
Break;
}
exit (0);
}
/ * BIG_LOOP: Simple, I will understand, I don't explain it. * /
Void Big_Loop (INT N)
{
INT I;
For (i = 0; i Switch (i% 4) { Case 0: PUTCHAR ('-'); Break; Case 1: Putchar ('/'); Break; Case 2: PUTCHAR ('|'); Break; Case 3: PUTCHAR ('//'); Break; } PUTCHAR ('/ b'); } } / * INPUT_INFORMATION: Simple, I will understand, nor is it. * / Void Input_information () { INT N_TABLE [4], i; For (i = 0; i <4; i ) { Printf ("Number% D: / T", I); Scanf ("% D", & n_table [i]); } Printf ("NUMBER1 / TNUMBER2 / TNUMBER3 / TNUMBER4 / N"); Printf ("% D / T% D / T% D / T% D / N", N_TABLE [0], N_TABLE [1], N_TABLE [2], N_TABLE [3]); } / * Signal Processing function, send SIGCHLD signals to the parent process when the child process ends, when the program will transfer to this function * / void handler () { / * Parent process confirmation, child process exit * / Wait (); } / *--------------------------------------------------- ----------- * / See it, the use of the signal is still very simple. But this is an unreliable signal usage method. why? The reason is very simple. When you run the program, you receive a signal that needs to be processed. Your program will turn to the signal processing function, process the signal, and continue the follow-up program. In this process, if the signal is received when performing a function such as Malloc (), the program does not continue to execute malloc () when the signal is completed, because Malloc () is not a reusable function, the program will not continue to execute malloc (), so space may not Will allocate and cause errors. Therefore, we also need a reliable signal processing, that is, when some of the operations that cannot be interrupted are performed, the signal is shielded. Until these key operations are completed, the signal is processed. I will give you a detailed explanation of the operation of the reliable signal. Since I am a rookie, maybe there is anything wrong. I may also have some details I have not considered. If you know what you want to advice. The younger brother is grateful! !