Cross-platform (Linux) development based on VS2019 C++ (1.4) - Signal

1. Goal

  • Signal concept
  • signal, sigaction function
  • Signal set manipulation functions
  • clock

In this chapter, let's learn about signals without parameters and the kill and signal functions.

Two, the signal

1. Concept

  • A signal is an event generated by the system in response to some condition, and a process takes action when it receives a signal. Can be caused by some error condition, such as a memory segment conflict, a floating-point processor error, or an illegal instruction, etc.
  • Processes can generate signals, catch and respond to signals, or mask signals

2. Purpose

Inter-process communication, as mentioned earlier, data between processes cannot be communicated, so inter-process communication technology is used. This signal is one of them, similar to the signals and slots in Qt (you can switch interfaces when used), (when developing Qt, he actually developed it with reference to linux kernel programming)

3. Review the kill command

kill is not killing but sending: we want to break through a previous misunderstanding, simply think that kill -9 pid is to kill the process, which is actually not the case. It should be understood as sending the ninth signal (SIGKILL), To end the process corresponding to the following pid. Query the signals supported by kill, as shown in the following figure

 From the above picture, you will find that the number 32 and 33 are missing in the middle. This is actually a dividing line. 1-31 are classified as unreliable signals, and 43 and later are classified as reliable signals (will be in the following code in the authenticating). Among them, 10 and 12 are for programmers to develop and use

4, signal name

The name of the signal is defined in the header file signal.h, and some of the names are shown in the figure

5. Process and signal (signal library function)

If a process wants to notify another process to do something, it can use the signal, so the signal library function is involved, as follows

Header file <signal.h>

Function prototype:

void (*signal(int sig, void (*func)(int))) (int);

signal is a function with two parameters, sig and func. The signal to be captured or masked is given by the parameter sig, and the function to be called when the specified signal is received is given by func.

The function func must have a parameter of type int (that is, the received signal code), and its own type is void func can also be the following two special values:         

  • SIG_IGN blocks this signal         
  • SIG_DFL restores default behavior 

View the signal library function in linux as shown below

Where signum is the number of the signal, such as the ninth signal kill (stop the process), sighhandler indicates what the process will do when the signal arrives;

typedef void (*sighhandler_t)(int); There is a * in front of the function name, which means the function pointer, that is, the logic of what function should use the function pointer (a signal corresponds to a function, similar to the connect that binds the signal and the slot together in Qt )

Review int kill(pid_t pid, int sig);

A process can send a signal to another process, including itself, by calling kill. If the program does not have permission to send this signal, the call to kill will fail.

The function of the kill function is to send the signal given by the parameter sig to the process whose identification number is pid. To send a signal, the sender process must have the appropriate permissions. This usually means that both processes must have the same user ID

3. Code and implementation effect

1. Sample code

#include <unistd.h>//
#include <string.h>
#include <sys/types.h>
#include<iostream>
#include <signal.h>

using namespace std;

void signalFunc(int i);
int main(){
	
	int pid = 0;
	//将信号和函数进行绑定,这个signal再开启子进程的时候会给拷贝走(这样子进程才能认识这个信号以及中断处理的函数)
	signal(SIGUSR1, signalFunc);
	signal(SIGUSR2, signalFunc);
	pid = fork();

	if (pid == 0)
	{
		//信号产生进程中断正在执行的业务
		//立刻执行信号对应的函数逻辑
		//执行以后回到原来的业务继续执行

		while (true)
		{
			cout << "子进程运行中 ...pid =" << getpid()<< endl;
			sleep(1);
		}
	}
	else if(pid >0)
	{
		//睡一会,等孩子运行起来的时候再发送信号(创造比子进程晚一点 )
		sleep(5);

		//父亲给孩子发送信号
		kill(pid,SIGUSR1);
		sleep(5);
		kill(pid, SIGUSR2);
		while (true)
		{
			cout << "父进程运行中 ...pid =" << getpid() << endl;
			sleep(1);
		}
	}
	return 0;
}
//信号对应的函数,参数对应的就是信号编码
void signalFunc(int i)
{
	cout << "函数被调用了signalFunc i=  " << i << endl;
}
	

Because each process will have corresponding PCBs, code segments, data segments and stack segments, etc., the child process created by fork () naturally also has code segments. By copying a copy of the parent process code segment (that is, the content of the entire cpp file, naturally fork () The previous signal will also be copied, so that the child process can recognize the signal and the function of interrupt processing), so it can receive and send the signal.

2. Effect

①A signal corresponds to a function

Why does i print out 10? Because the function corresponding to the signal, the parameter corresponds to the signal code, and SIGUSR1 corresponds to 10

② Two signals correspond to a function (signal twice)

 ③The parent process sends the signal 5 times in a row

Two different signals can be bound to the same function, as can multiple signals   

④ Delete sleep , send 5 consecutive signals, and receive once i=10

Without sleep, the signal is sent five times at once, but it is received once. The difference lies in this sleep. There is sleep, which means that a signal is sent to rest for a while, and it can be received five times.

⑤ There is also no sleep situation. At this time, we switch to sending signal No. 34 (reliable signal), as shown below

 No sleep, but received 5 times in a row, which is the difference between a reliable signal and an unreliable signal

Summarize:

A reliable signal must be sent successfully and must be received. Unreliable signals will be lost when sent without interruption (without sleep)

Originality is not easy, please indicate the source when reprinting:

Cross-platform (Linux) development based on VS2019 C++ (1.4) - Signal

Let's enter the study of the second part of the signal:

Cross-platform (Linux) development based on VS2019 C++ (1.4.2) - Signal

Guess you like

Origin blog.csdn.net/hml111666/article/details/123561522