This is the 28th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

1. Signal introduction under Linux

Signal processing is essential for Linux application programming. Signals can be used for multi-process communication. Run the kill -l command to view all signals supported by the current system

[wbyq@wbyq linux_c]$ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX- 14	51) SIGRTMAX- 13	52) SIGRTMAX- 12
53) SIGRTMAX- 11	54) SIGRTMAX- 10	55) SIGRTMAX9 -	56) SIGRTMAX- 8 -	57) SIGRTMAX7 -
58) SIGRTMAX- 6	59) SIGRTMAX- 5	60) SIGRTMAX4 -	61) SIGRTMAX- 3	62) SIGRTMAX2 -
63) SIGRTMAX- 1	64) SIGRTMAX
Copy the code

Some of the more common signals:

  1. SIGINT When the user presses Ctrl+C, the user terminal sends this signal to the current running process. The default action is to terminate the current process.
  2. The SIGQUIT shortcut is

    , which, like SIGINT, terminates the current process by default.
    +>
  3. SIGSEGV is a signal generated by accessing illegal memory, which is often encounteredSegmentation fault
  4. SIGALRM Indicates the timer timeout signal. For example, this signal is generated when the time of the alarm clock expires.
  5. SIGIO data read and write signal, usually generated when the device has data to read when interacting with the driver, notifies the process to read the data.

The signal of Linux application layer is similar to the interrupt of single chip computer program, and the processing function can be set. Interrupt service function. If the process does not want to use the default processing of the signal, it can capture the signal itself and then do related processing.

For example, if the user presses <Ctrl+C> and does not want to terminate the process immediately, it may need to do some cleaning, then it can capture the SIGINT signal itself, handle memory freeing, file closing, and so on before exiting.

The signal acquisition function is as follows:

#include <signal.h>
typedef void (*sighandler_t)(int);   // Function pointer type

sighandler_t signal(int signum, sighandler_t handler); Function function: register the signal to be captured. Function parameters:intSignum Indicates the signal to capture. Kill -lsighandler_tHandler: Function to be executed after a signal is captured.Copy the code

2. Signal capture and transmission cases

2.1 Capture of Ctrl+C signal

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>

/* Signal handler */
void sighandler_func(int sig)
{
    printf("Captured signal :%d\n",sig);
}

int main(int argc,char **argv)
{
    // Register the signal to be captured
    signal(SIGINT,sighandler_func);
    while(1) {}return 0;
}

Copy the code

2.2 Segment error signals were captured

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
/* Signal handler */
void sighandler_func(int sig)
{
    printf("Captured signal :%d\n",sig);
    exit(0); // End the process
}

int main(int argc,char **argv)
{
    // Register the signal to be captured
    signal(SIGSEGV,sighandler_func);

    int *p;
    *p=12345;  // Assign an invalid memory address

    while(1) {}return 0;
}

Copy the code

2.3 Run the kill command to send signals to a specified process

Grammar:

Kill -s < signal name > < process PID number > kill -< signal name > < process PID number >Copy the code

Example:

[wbyq@wbyq linux_c]$ kill -s SIGINT 18365
[wbyq@wbyq linux_c]$ ps
  PID TTY          TIME CMD
 7481 pts/0    00:00:02 bash
18370 pts/0    00:02:49 a.out
18371 pts/0    00:03:28 a.out
18372 pts/0    00:02:46 a.out
18373 pts/0    00:02:36 a.out
18400 pts/0    00:00:00 ps
[1/a.out [wbyq@wbyq linux_c]$kill -s2 18370
[wbyq@wbyq linux_c]$ ps
  PID TTY          TIME CMD
 7481 pts/0    00:00:02 bash
18371 pts/0    00:03:39 a.out
18372 pts/0    00:02:56 a.out
18373 pts/0    00:02:47 a.out
18401 pts/0    00:00:00 ps
[2/a.out [wbyq@wbyq linux_c]$[wbyq@wbyq linux_c]$kill2 - 18371

[wbyq@wbyq linux_c]$ kill 9 - 18372    // Force to kill the specified signal
[wbyq@wbyq linux_c]$ ps
  PID TTY          TIME CMD
 7481 pts/0    00:00:02 bash
18373 pts/0    00:05:31 a.out
18407 pts/0    00:00:00 ps
[4]- Killed./a.outCopy the code

View the PID of the current process

To view all processes running in the background: [wbyq@wbyq linux_c]$ps// View the background process of the current terminal
  PID TTY          TIME CMD
 7481 pts/0    00:00:02 bash
18365 pts/0    00:00:54 a.out
18370 pts/0    00:00:04 a.out
18371 pts/0    00:00:04 a.out
18372 pts/0    00:00:05 a.out
18373 pts/0    00:00:03 a.out
18374 pts/0    00:00:00 ps

[wbyq@wbyq linux_c]$ ps -aux   // View details about all processes in the current system
wbyq     18365 65.1  0.0   1844   280 pts/0    R    09:19   1:33 ./a.out
wbyq     18370 35.9  0.0   1844   280 pts/0    R    09:20   0:34 ./a.out
wbyq     18371 34.9  0.0   1844   276 pts/0    R    09:20   0:32 ./a.out
wbyq     18372 44.7  0.0   1844   276 pts/0    R    09:20   0:41 ./a.out
wbyq     18373 34.4  0.0   1844   280 pts/0    R    09:20   0:32 ./a.out
wbyq     18375  5.0  0.0   6532  1048 pts/0    R+   09:21   0:00 ps -aux

[wbyq@wbyq linux_c]$ top   // Dynamically view the process details of the current systemProgram background run way: [wbyq@wbyq linux_c]$./a.out &Copy the code

2.4 Send a signal to a specified process through code

Functions are used the same as commands.

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
Copy the code