Linux中信号处理的两种方法

1、使用signal()函数和sigaction()

signal()函数原型 
       #include <signal.h>
       typedef void (*sighandler_t)(int);
       sighandler_t signal(int signum, sighandler_t handler);
       
       signal() sets the disposition of the signal signum to handler, which is
       either SIG_IGN, SIG_DFL, or the address of a  programmer-defined  function (a "signal handler")
The signals SIGKILL and SIGSTOP cannot be caught or ignored. ——即信号SIGKILL和信号SIGSTOP不能被捕获或者忽略。

sigaction()函数原型
NAME
<pre>       sigaction - examine and change a signal action
SYNOPSIS
       #include <signal.h>
       int sigaction(int signum, const struct sigaction *act,
                     struct sigaction *oldact);
   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
       sigaction(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
       
If act is non-NULL, the new action for signal signum is installed  from
act.  If oldact is non-NULL, the previous action is saved in oldact.
The sigaction structure is defined as something like:
   struct sigaction {
       void     (*sa_handler)(int);
       void     (*sa_sigaction)(int, siginfo_t *, void *);
       sigset_t   sa_mask;
       int        sa_flags;
       void     (*sa_restorer)(void);
   };
On  some  architectures  a  union  is  involved:  do not assign to both
sa_handler and sa_sigaction.
The sa_restorer element is obsolete and should not be used.  POSIX does
not specify a sa_restorer element.

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

/* Define the signal handle function */
void my_func(int sig_no)
{
	switch(sig_no)
	{
		case SIGINT:
			printf("I have get SIGINT\n");
			break;
		case SIGQUIT:
			printf("I have get SIGQUIT\n");
			break;
		default:
			break;
	}
}

int main()
{
	struct sigaction action;
	printf("Waiting for signal SIGINT or SIGQUIT...\n");
	
	/* Initialize the sigaction structure*/
	action.sa_handler = my_func;
	sigemptyset(&action.sa_mask);
	action.sa_flags = 0;
	
	/* Send certain signal and go to handle_fun */
	sigaction(SIGINT, &action, 0);
	sigaction(SIGQUIT, &action, 0);
	
	pause();
	alarm(3);
	
	printf("I'm over!\n");

	exit(0);		
}


2、使用信号集函数组

调用的函数主要包括一下4个功能模块,创建信号集、注册信号处理函数及检测信号。
sigemptyset():将信号集初始化为空
sigfillset():  将信号集初始化为包含所有已定义的信号集。
sigaddset(): 将制定信号加入信号集中。
sigdelset():   将指定信号从信号集中删除。
sigismember():  查询指定信号是否在信号集中。

sigpending()函数允许进程检测“未处理“的信号,并进一步决定对它们做何处理。

说明:
注册信号处理函数主要用于决定进程如何处理信号,注意:信号集中的信号并不是真正可以处理的信号,只有当信号的状态处于非阻塞状态时才会真正起作用。所以一般使用sigprocmask()函数检测并更改信号屏蔽字(信号屏蔽字是指用来指定当前被阻塞的一组信号,它们不会被进程接收),然后使用sigaction()函数来定义进程接收到特定信号的行为。
信号处理的一般流程:

程序实例:
首先把SIGQUIT、SIGINT两个信号加入信号集,然后将该信号集设为阻塞状态,并进入用户输入状态。用户只需按任意键就可以立即将信号集设为非阻塞状态,再对这两个信号分别进行操作,其中SIGQUIT执行默认操作,而SIGINT执行用户自定义函数的操作。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>


/* Define the signal handle function */
void my_func(int sig_no)
{
	printf("If you want to quit, please try SIGQUIT\n");
}

int main()
{
	sigset_t set, pendset;
	struct sigaction action1, action2;
	
	/* step1: Initialize the sigset with Null. */
	if(sigemptyset(&set) < 0)
	{
		perror("sigemptyset");
		exit(1);
	}
	
	/* Add certain signal to sigset */
	if(sigaddset(&set, SIGQUIT) < 0)
	{
		perror("sigaddset");
		exit(1);
	}
	if(sigaddset(&set, SIGINT) < 0)
	{
		perror("sigaddset");
		exit(1);
	}
	
	if(sigismember(&set, SIGINT) < 0)
	{
		sigemptyset(&action1.sa_mask);
		action1.sa_handler = my_func;
		action1.sa_flags = 0;
		sigaction(SIGINT, &action1, NULL);
	}
	
	if(sigismember(&set, SIGQUIT) < 0)
	{
		sigemptyset(&action2.sa_mask);
		action2.sa_handler = my_func;
		action2.sa_flags = 0;
		sigaction(SIGQUIT, &action2, NULL);
	}
	
	/* step2: Set the value of mask word */
	if(sigprocmask(SIG_BLOCK, &set, NULL) < 0)
	{
		perror("sigprocmask");
		exit(1);
	}
	else
	{
		printf("Signal set was blocked, Press any key!\n");
		getchar();
	}

	/* Delete the signal of set */
	if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)
	{
		perror("sigprocmask");
		exit(1);
	}
	else
	{
		printf("Signal set was unblocked, Press any key!\n");
	}
	
	while(1);
	exit(0);
}









郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。