[Embedded general review] Linux signal (super super super ~~~ detailed explanation)

There are five communication methods between processes : pipes , semaphores, shared memory, message queues, and sockets

1. What is a signal

  1. Signals are a means of inter-process communication;
  2. Is an event trigger mechanism (similar to the interrupt mechanism);
  3. It is a kind of asynchronous communication (asynchronous notification) (the only asynchronous means in the inter-process communication mechanism), which cannot transmit a large amount of data; [
    Synchronization: It is a process that is executed sequentially [if a certain link If there is no execution, the whole process will be blocked], the two sides of the communication do one thing in real time]
    [Asynchronous: It is an event-triggered process that uses signals or interrupts. If no signals or interrupts are generated, it will not affect the normality of the program. implement. Neither party in the communication will wait [the communication process, will choose to do their own thing]]
  4. A signal can be sent by one process, and captured, blocked and ignored by other processes;
    [Capture: the corresponding action will be executed after receiving the signal] [Blocking:
    the transmission of the signal will be blocked, and the signal will not be delivered until the blockage is unblocked]
    [Ignore: After receiving the signal, do not do any processing]
    (If no action is associated with the signal, the system default action will be executed)

2. Characteristics of the signal

  1. Signals cannot pass large amounts of data
  2. Signals can be communicated between arbitrary processes
  3. Signals are a means of asynchronous communication

3. Signals supported by Linux

3.1 kill command

The kill command is used to send a signal to the specified process
kill -l: list the signals supported in Linux
insert image description here
kill -num pid: send num signals to the process represented by pid

The linux operating system configures default operations for each signal
insert image description here
insert image description here

Signal name meaning default action
SIGHUP This signal is sent when the client connection (normal/abnormal) ends, usually when the control process of the terminal ends, and notifies each process in the same session that it is no longer associated with the control terminal. terminate process
SIGINT This signal is sent when the user enters the INTR string, usually ctrl+c, and the terminal driver sends this signal and sends it to each process in the foreground process. terminate process
FOLLOWS This signal is similar to SIGINT, but is controlled by the QUIT character (usually ctrl+\). terminate process
SIGLL This signal is issued when a process attempts to execute an illegal instruction (the executable file itself has an error, or when it tries to execute the data segment, or the stack overflows). terminate process
SIGFPE This signal is sent when a fatal arithmetic operation error occurs, including not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and division by 0 terminate process
SIGKILL This signal is used to immediately end the running of the process, and cannot be blocked, processed and ignored terminate process
SIGUSR1 Signals left for user definition
SIGUSR2 Signals left for user definition
SIGPIPE signal of a pipe rupture
SIGALARM This signal is emitted when a timer terminate process
SIGSTOP This signal is used to suspend a process and cannot be blocked, handled and ignored suspend process
SIGSTP This signal is used to stop the process interactively. When the user enters the SUSP character, usually ctrl+z sends this signal stop process
SIGCHLD When the child process changes state, the parent process will receive this signal neglect

Note that
SIGKILL and SIGSTOP cannot be ignored and caught by blocking

4. Signal operation

4.1 Sending of signals - how processes send signals

insert image description here
insert image description here

Use the kill function to send a signal to the specified process

  1. kill function
    / header file to be included /
    #include<sys/types.h>
    #include<signal.h>
    /*
  • Function name: kill
  • Function function: send a signal to the specified process
  • Function parameters:
    ————pid_t pid: the pid of the specified process
    ———— int sig: the signal value sent
  • Function return value: int: return 0 for success, -1 for failure
    */
    int kill(pid_t pid,int sig);

4.2 Signal capture (associate a processing function to the signal) - how the process associates the signal and the signal processing function

insert image description here
ANSI American Standardization Organization - signal handling signal processing function
insert image description here

Linux associates a default operation with each signal.
When a signal is received, the process receiving the signal can perform the following operations:

  1. neglect
  2. block
  3. Capture
    --> Default Action
    --> Execute Signal Processing Function

Use the signal function to handle the above process

  1. signal function
    / header file that needs to be included /
    #include<signal.h>
    /*
    * defines a function pointer type: sighandler_t
    * which type of function the function pointer points to: void func(int) - a pointer of this type, pointing to returns void - a class of functions with one parameter of type int;
    */
    typedef void( sighandler_t)(int);
    /
  • Function name: signal
  • Function function: associate a signal with a signal processing function
  • Function parameters:
    ———int signum: the associated signal
    ——— sighandler_t handler: the function pointer (handler) pointer of the signal processing function cannot be NULL
    */
  • sighandler_t signal(int sugnum,sighandler_t handler);
    function return value: sighandler_t: returns the pointer of the previous signal processing function if successful, returns SIG_ERR if failed
  • Special value of handler:
    ——SIG_IGN: Ignore the signal
    ——SIG_DFL: Perform the default operation

5. Examples

5.1 Example 1

insert image description here
insert image description here
insert image description here
insert image description here

insert image description here

insert image description here

#include<stdio.h>
#include<sys/types.h>
#include<signal.h>
#include<stdlib.h>
#include<unistd.h>

void func(int signum)
{
	if(SIGUSR1 == signum)
	{
		puts("recv this signal");
		exit(0);
	}
}

int main()
{
	signal(SIGUSR1,func);
	while(1)
	{
		puts("hello world!");
		sleep(1);
	}
	return 0;
}

5.2 Example 2

insert image description here

insert image description here
insert image description here

#include<sys/types.h>
#include<signal.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>



int main()
{
	char buf[20] = {0};//定义一个数组
	gets(buf);
	pid_t pid = atoi(buf);
	kill(pid,SIGUSR1);
	return 0;
}
	

Guess you like

Origin blog.csdn.net/weixin_51911075/article/details/128022926