Signal capture and processing Linux-

Overview of signal capture process

Introduced in front of the basic information and send signals of signals, summarized here about Linux in the process of capture and signal processing, process the received signal will generally set up a special signal processing function to perform the operation after receiving the signal, similar to the same interrupt .
In the signal processing function may be performed depending on the respective operation signals can be read the information carried by the signal.
Linux system for processing signals from the main signal()function and sigaction()to hold a function, and sometimes use the pause()functions below these functions introduced one by one. (SIGKILL and SIGSTOP signals can not be caught or ignored)

signal () function

signal () function is used to set the process receives an operation signal input at the shell man signalmay get its function prototype:

#include <signal.h>
typedef void ( *sighandler_t ) ( int );
sighandler_t signal( int signum, sighandler_t handler );
  • signum is a signal of a specified number
  • handler is a signal processing function corresponding to the pointer, it points to the function type sighandler_t, and points to a function of a parameter of type int, void return value
  • signal () returns a pointer to the signal processing function when the function succeeds, the return SIG_ERR (i.e., -1) error

The following piece of code is achieved by signal()capturing and processing of the signals:

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

/* 信号处理函数 */
void handler_sigint( int signo )
{
	printf("recv SIGINT\n");
}

int main( void )
{
	/* 安装信号处理函数 */
	signal( SIGINT, handler_sigint );
	while( 1 )
		;
	return 0;
}

In the shell can press <Ctrl + C> to send the SIGINT signal to the process, the process received signals performs the signal processing functions immediately, that is printed on the screen: recv SIGINT
want to end the process when pressing <Ctrl +> that is, can (send a SIGSTOP signal), you can also use the kill command to end the process

Program Description:
The program first use the signal()function installed SIGINT signal processing function, and then enters an infinite loop, when the received signal SIGIINT jumps immediately to perform signal processing functions, and to print out the information, and then returns to the main function of an infinite loop continues.

sigaction () function

the sigaction () function call can also capture and signal processing functions on the signal input at the shell man sigactionmay get its function prototype:

#include <signal.h>

int sigaction( int signum, const struct sigaction *act, struct sigaction *oldact );
  • signum is a signal of a specified number
  • act point to the new signal processing function
  • oldact will store the old signal handler
  • Struct sigaction structure is defined as follows:
struct sigaction
{
	void ( *sa_handler )( int );
	void ( *sa_sigaction )( int, siginfo_t, void* );
	sigset_t sa_mask;
	int sa_flags;
	void ( *sa_restorer )( void );
}

The members of the structure will be described below:

  • Before the two members are defined as a union, two parameters i.e. the same time only one is valid.
  • may be a constant or sa_handler SIG_DFL SIG_IGN, it may be the name of a function, signal processing function point
  • sa_sigaction also used to refer to the signal processing function, but it has three parameters, the first parameter is the number signal, the second parameter is a pointer pointing to siginfo_t, this parameter can be used to pass dataThe third argument is generally not
  • sa_mask declares a set of signals
  • Some members of the correlating process operation sa_flags for explaining signal processing

By the following piece of code to familiarize yourself with:

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

/* 定义全局变量 */
int temp = 0;

/* 信号处理函数 */
void handler_sigint( int signo )
{
	printf("recv SIGINT\n");
	sleep( 5 );
	temp++;
	printf("the value of temp is :%d\n", temp);
	printf("in handler_sigint, after sleep\n");
}

int main( void )
{
	struct sigaction act;
	/* 配置act结构体 */
	act.sa_handler = handler_sigint;
	/* 表示支持信号的嵌套处理 */
	act.sa_flags = SA_NOMASK;
	/* 安装信号处理函数,act中就包含了信号处理函数的信息 */
	sigaction( SIGINT, &act, NULL );
	while( 1 )
		;
	return 0;
}

Results of the implementation is as follows:
Here Insert Picture Description
when the act.sa_flags = SA_NOMASK;results of the implementation remove the comment as follows:
Here Insert Picture Description
After removal, sigaction blocking signals are lining up in accordance with the default, because SIGINT signal is not reliable, unreliable signal does not support line, resulting in a loss of signal

pause () function

pause () function allows the calling process is suspended until a signal is captured, in the input shell man pauseavailable Prototype:

#include <unistd.h>

int pause( void )

pause () function will make the current process to sleep temporarily, until it is interrupted signals, only the function returns -1.

Published 62 original articles · won praise 188 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43743762/article/details/101314744