Signal mechanism (signel) under linux--continuously updated

1. The basic concept of signal

        Processes can send soft interrupt signals to each other through the system call kill. The kernel can also send signals to the process due to internal events, notifying the process that an event has occurred. The process uses the system call signal to specify the processing behavior of the process to a signal.

2. The essence of the signal is an int type number (defined in advance)

        Explanation: The signal is only used to notify a process of what has happened, and does not pass any data (such as text, voice, etc.) to the process. It is an int number defined in advance by the kernel. When the signal function is called, it is used to specify the signal type.

3. Who sent the signal

        (1) The user presses a key such as ctrl+C in the ubuntu terminal (the terminal is a process running in the foreground)

        (2) After the hardware is abnormal, the operating system kernel sends a signal

        (3) The user uses the kill command (which is an API function provided by the kernel), such as: kill - 9 xxx

        (4) Signals will also be sent when certain software conditions are met. For example, when the alarm clock time is up, a SIGALARM signal will be generated, and SIGPIPE will be generated when writing to a pipeline whose read end is nearly closed.

4. Who handles the signal and how to handle it

        (1) To capture the signal, the process can specify a processing function, which is handled by this function.

        (2) Ignore the signal and do nothing to the signal, as if it never happened.

        (3) Default processing, this default operation, the default operation for most signals is to cause the process to terminate.

5. The reason for the occurrence of each int signal value (here are the common ones)

    5.1 The signum.h under /usr/include/i386-linux-gnu/bits under linux defines all the signal values

The reason the signal value processing action is signaled

         SIGHUP 1 A Terminal hangs or the controlling process terminates  SIGINT 2 A Keyboard interrupt (eg break key is pressed)  terminates normally          SIGQUIT 3 C Keyboard exit key is pressed           SIGILL 4 C Illegal instruction   SIGABRT 6 C Issued by abort(3) Exit instruction, aborted SIGIO 23,29,22 A An I/O operation can now be performed (4.2 BSD) SIGKILL 9 AEF The ultimate way to kill a process SIGSEGV 11 C Invalid memory reference  SIGPIPE 13 A Broken pipe: write a no Read the port's pipeline  SIGALRM 14 A Signal SIGTERM issued by alarm(2)  15 A Termination signal  SIGUSR1 30, 10, 16 A User-defined signal 1 Perform inter-process communication for the user, unlike other processing methods that are bound to SIGUSR2 31,12,17 A User-defined signal 2  SIGCHLD 20,17,18 B Child process end signal           SIGCONT 19,18,25 Process continues (the process that was stopped) 
        


        
        
        
        
        
        
        
        
        
        

         SIGSTOP 17,19,23 DEF Terminate process 
         SIGTSTP 18,20,24 D Stop key pressed on the controlling terminal (tty) 
         SIGTTIN 21,21,26 D Background process attempts to read 
         SIGTTOU from the controlling terminal 22,22,27 D Background process attempts write from the controlling terminal       

    5.2 The meanings of the letters in one of the processing actions are as follows 

            A The default action is to terminate the process 
             B The default action is to ignore the signal 
            C The default action is to terminate the process and dump the core image 
            D The default action is to stop the process 
            E The signal cannot be caught 

             F signal cannot be ignored

6. Examples of process signal processing

    6.1, signal system call

            (1) The statement is as follows:
                     typedef void (*sighandler_t) (int); 
             sighandler_t signal( int signum, sighandler_t handler ); 

                            Explanation: sighhandler_t is a function pointer, the function pointed to by this function pointer accepts an int parameter and returns a void type
            (2) In the call, the parameter signum indicates the signal of the processing method to be set. The second parameter handler is a handler function, or 
                      SIG_INT: Capture this signal and process the handler function
                    SIG_IGN: Ignore the signal pointed to by the parameter signum. 
                    SIG_DFL: Restore the processing method of the signal pointed to by the parameter signum to the default value.
            (3) Details:

                    1. After the signal function binds a capture function, the bound capture function will be automatically executed after the signal occurs, and the signal number will be passed as a parameter to the capture function

                    2. The return value of signal is SIG_ERR when there is an error, and the old capture function is returned when the binding is successful

           

    6.2, signal code example

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

typedef void (*sighandler_t)(int); //The following uses the SIG_DFL defined by the kernel with sighandler_t , you need to declare

void func(int sig )
{
if (SIGINT != sig)
            return 0;
printf("func for signal: %d.\n", sig);
}
int main(void)
{
sighandler_t ret = (sighandler_t)(-2); //
/ /signal(SIGINT, func) ;
//signal(SIGINT, SIG_DFL ); // Specify signal SIGINT as default processing
ret = signal(SIGINT, SIG_IGN );         // Specify signal SIGINT as ignore processing
if (SIG_ERR == ret)
{
perror("signal:");
exit(-1);
}

printf("before while(1)\n");
while(1);
printf("after while(1)\n");return 0;


}














Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856599&siteId=291194637