8) SIGFPE
This signal is caused by floating-point exception. In fact, it will be caused when the divisor is 0. No matter what type of dividend is, this signal will be sent as long as the divisor is 0. The test code is as follows:
1 /** 2 * filename: signal_8.c 3 * author: Suzkfly 4 * date: 2021-02-16 5 * platform: Ubuntu 6 * Operation steps: 7 * Run the executable program. 8 */ 9 #include <stdio.h> 10 #include <signal.h> 11 #include <float.h> 12 #include <stdlib.h> 13 14 /* Custom signal processing function */ 15 void func(int sig) 16 { 17 switch (sig) { 18 case SIGFPE : 19 printf("SIGFPE signal captured. value = %d\n", sig); 20 exit(0); /* It cannot be removed here, otherwise it will go all the way in */ 21 break; 22 } 23 } 24 25 int main(int argc,char *argv[]) 26 { 27 int i = 0; 28 float f = 0; 29 30 signal(SIGFPE, func); 31 32 f = FLT_MAX * 2; /* floating point overflow */ 33 printf("f = %f\n", f); 34 i = 1/0; 35 //f = 1/0; 36 printf("f = %f\n", f); 37 38 return 0; 39 }
Test results:
It can be seen from the test result that sigpe does not send an overflow signal when the division of sigpe is 0. It should be noted that when you define the signal processing mode, you should also let the program exit, otherwise the signal will be triggered all the time.
9) SIGKILL
The signal can be sent by using the kill command at the terminal. The specific method is to input: kill -9 + process pid
The test procedure is as follows:
1 /** 2 * filename: signal_9.c 3 * author: Suzkfly 4 * date: 2021-02-16 5 * platform: Ubuntu 6 * Operation steps: 7 * Run the executable program and find that the return value of signal function is SIG_ERR, and kill it with the kill -9 command 8 */ 9 #include <stdio.h> 10 #include <signal.h> 11 #include <unistd.h> 12 #include <sys/types.h> 13 14 /* Custom signal processing function */ 15 void func(int sig) 16 { 17 switch (sig) { 18 case SIGKILL : 19 printf("SIGKILL signal captured. value = %d\n", sig); 20 break; 21 } 22 } 23 24 int main(int argc,char *argv[]) 25 { 26 void(*p)(int) = NULL; 27 28 p = signal(SIGKILL, func); 29 if (p == SIG_ERR) { 30 printf("signal failed\n"); 31 } 32 printf("pid = %d\n", getpid()); 33 34 while (1) { 35 sleep(1); 36 } 37 38 return 0; 39 }
Test results:
The call of signal signal failed because SIGKILL signal cannot customize the processing method, and another terminal can be opened to input kill -9 6884, which can also kill the process normally. The reason for leaving such a signal is to ensure that there can be no undead process.
10) SIGUSR1
The signal used by the user is naturally sent by the user, which can generally be realized by two functions:
10.1 raise
Function prototype | int raise(int sig); |
Header file | signal.h |
function | Send a signal to yourself |
parameter | [in]: sig: signal to be sent |
return | 0 is returned for success and non-0 value is returned for failure |
10.2 kill
Function prototype | int kill(pid_t pid, int sig); |
Header file | signal.h |
function | Signal the process of the specified PID |
parameter |
[in]: PID: Specifies the PID of the process [in]: sig: signal to be sent |
return | 0 is returned for success and - 1 is returned for failure |
Although the name of the function kill is kill, its real meaning is only to send a specified signal to the process with a specified PID. The test procedure is as follows:
1 /** 2 * filename: signal_10.c 3 * author: Suzkfly 4 * date: 2021-02-16 5 * platform: Ubuntu 6 * Operation steps: 7 * Run the executable program and observe the printed information. 8 */ 9 #include <stdio.h> 10 #include <signal.h> 11 #include <unistd.h> 12 #include <sys/types.h> 13 14 /* Custom signal processing function */ 15 void func(int sig) 16 { 17 switch (sig) { 18 case SIGUSR1 : 19 printf("SIGUSR1 signal captured. value = %d\n", sig); 20 break; 21 } 22 } 23 24 int main(int argc,char *argv[]) 25 { 26 int pid = 0; 27 void(*p)(int) = NULL; 28 29 pid = fork(); 30 if (pid > 0) { /* Parent process */ 31 p = signal(SIGUSR1, func); 32 if (p == SIG_ERR) { 33 printf("signal failed\n"); 34 } 35 raise(SIGUSR1); /* Send SIGUSR1 signal to yourself */ 36 while (1) { 37 sleep(1); 38 } 39 } else if (pid == 0) { /* Subprocess */ 40 sleep(1); /* Wait for parent process registration signal */ 41 kill(getppid(), SIGUSR1); /* Send SIGUSR1 signal to parent process */ 42 } 43 44 return 0; 45 }
Operation results:
Result analysis:
After the program runs, it prints "SIGUSR1 signal captured. value = 10" twice. The first time is sent to itself by calling raise, and the second time is sent by the sub process through the kill function.