信号(五)

一、sigaction

(1)包含头文件<signal.h>

(2)功能:sigaction函数用于改变进程接收到特定信号后的行为。

原型:int  sigaction(int signum,const struct sigaction *act,const struct sigaction *old);

参数:

该函数的第一个参数为信号的值,可以为除SIGKILLSIGSTOP外的任何一 个特定有效的信号(为这两个信号定义自己的处理函数,将导致信号安装错误)

第二个参数是指向结构sigaction的一个实例的指针,在结构 sigaction的实例中,指定了对特定信号的处理,可以为空,进程会以缺省方式对信号处理

第三个参数oldact指向的对象用来保存原来对相应信号的处理,可指定oldactNULL

返回值:函数成功返回0,失败返回-1

例子:

最简单的使用:处理信号

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

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


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)

void handler(int sig);

int main(int argc, char *argv[])
{
	struct sigaction act;
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;

	if (sigaction(SIGINT, &act, NULL) < 0)
		ERR_EXIT("sigaction error\n");

	for (;;)
		pause();
	return 0;
}

void handler(int sig)
{
	printf("recv a sig=%d\n", sig);
}

用sigaction实现signal函数

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

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


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)

void handler(int sig);
__sighandler_t my_signal(int sig, __sighandler_t handler);

int main(int argc, char *argv[])
{
/*
	struct sigaction act;
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;

	if (sigaction(SIGINT, &act, NULL) < 0)
		ERR_EXIT("sigaction error\n");
*/
	my_signal(SIGINT, handler);
	for (;;)
		pause();
	return 0;
}

__sighandler_t my_signal(int sig, __sighandler_t handler)
{
	struct sigaction act;
	struct sigaction oldact;
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;

	if (sigaction(sig, &act, &oldact) < 0)
		return SIG_ERR;

	return oldact.sa_handler;
}

void handler(int sig)
{
	printf("recv a sig=%d\n", sig);
}

二、sigaction结构体

第二个参数最为重要,其中包含了对指定信号的处理、信号所传递的信息、信号处理函数执行过程中应屏蔽掉哪些函数等等

struct sigaction {
	void (*sa_handler)(int);
	void (*sa_sigaction)(int, siginfo_t *, void *);
	sigset_t sa_mask;
	int sa_flags;
	void (*sa_restorer)(void);
};

在处理SIGINT信号的时候,收到SIGQUIT信号,SIGQUIT的信号屏蔽字为1,所以SIGQUIT信号不能到信号未决态。当SIGINT信号处理函数执行完毕,SIGQUIT信号屏蔽字变为0,信号未决字也变为0,进入信号递达态,处理SIGQUIT信号处理函数

(1)表示当SIGINT信号发生时,SIGQUIT信号也发生了。SIGQUIT信号被阻塞,直到SIGINT处理函数执行完毕,SIGQUIT信号才被递达

(2)在信号处理的函数中,加入到sa_mask掩码的信号被阻塞

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

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


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)

void handler(int sig);

int main(int argc, char *argv[])
{
	struct sigaction act;
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	sigaddset(&act.sa_mask, SIGQUIT);
	act.sa_flags = 0;

	if (sigaction(SIGINT, &act, NULL) < 0)
		ERR_EXIT("sigaction error");

	for (;;)
		pause();
	return 0;
}

void handler(int sig)
{
	printf("recv a sig=%d\n", sig);
	sleep(5);
}

三、sigaction中与sigprocmask的信号屏蔽的区别

(1)在信号处理的函数中,加入到sigaction.sa_mask掩码的信号被阻塞。直到sa_handler返回,被阻塞的信号才能备被递达

(2)sigpromask表示将信号添加到进程的信号屏蔽字中,即使这些信号发生也不能被递达

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>

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


#define ERR_EXIT(m) \
	do \
	{ \
		perror(m); \
		exit(EXIT_FAILURE); \
	} while(0)

void handler(int sig);

int main(int argc, char *argv[])
{
	struct sigaction act;
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;

	sigset_t s;
	sigemptyset(&s);
	sigaddset(&s, SIGINT);
	sigprocmask(SIG_BLOCK, &s, NULL);

	if (sigaction(SIGINT, &act, NULL) < 0)
		ERR_EXIT("sigaction error");

	for (;;)
		pause();
	return 0;
}

void handler(int sig)
{
	printf("recv a sig=%d\n", sig);
	sleep(5);
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值