linux驱动学习笔记(四)信号

前言

信号作为异步通知的一种手段,类似与硬件的中断功能,算是在软件层次上对中断的一种模拟,驱动可以通过主动向应用层发送信号来报告自己可以被应用层访问了,应用层捕获到信号后就可以从驱动设备中读写数据。

Linux所支持的信号类型

#define SIGHUP 1 /* 终端挂起或控制进程终止 */
#define SIGINT 2 /* 终端中断(Ctrl+C 组合键) */
#define SIGQUIT 3 /* 终端退出(Ctrl+\组合键) */
#define SIGILL 4 /* 非法指令 */
#define SIGTRAP 5 /* debug 使用,有断点指令产生 */
#define SIGABRT 6 /* 由 abort(3)发出的退出指令 */
#define SIGIOT 6 /* IOT 指令 */
#define SIGBUS 7 /* 总线错误 */
#define SIGFPE 8 /* 浮点运算错误 */
#define SIGKILL 9 /* 杀死、终止进程 */
#define SIGUSR1 10 /* 用户自定义信号 1 */
#define SIGSEGV 11 /* 段违例(无效的内存段) */
#define SIGUSR2 12 /* 用户自定义信号 2 */
#define SIGPIPE 13 /* 向非读管道写入数据 */
#define SIGALRM 14 /* 闹钟 */
#define SIGTERM 15 /* 软件终止 */
#define SIGSTKFLT 16 /* 栈异常 */
#define SIGCHLD 17 /* 子进程结束 */
#define SIGCONT 18 /* 进程继续 */
#define SIGSTOP 19 /* 停止进程的执行,只是暂停 */
#define SIGTSTP 20 /* 停止进程的运行(Ctrl+Z 组合键) */
#define SIGTTIN 21 /* 后台进程需要从终端读取数据 */
#define SIGTTOU 22 /* 后台进程需要向终端写数据 */
#define SIGURG 23 /* 有紧急数据 */
#define SIGXCPU 24 /* 超过 CPU 资源限制 */
#define SIGXFSZ 25 /* 文件大小超额 */
#define SIGVTALRM 26 /* 虚拟时钟信号 */
#define SIGPROF 27 /* 时钟信号描述 */
#define SIGWINCH 28 /* 窗口大小改变 */
#define SIGIO 29 /* 可以进行输入/输出操作 */
#define SIGPOLL SIGIO
/* #define SIGLOS 29 */
#define SIGPWR 30 /* 断电重启 */
#define SIGSYS 31 /* 非法的系统调用 */
#define SIGUNUSED 31 /* 未使用信号 */

应用层对信号的接收与处理

void (*signal(int signum, void (*handler))(int)))(int);//第一个参数为信号,第二个参数为信号处理函数的函数指针

//demo.c

void sigterm_handler(int signo)
{
printf("Have caught sig N.O. %d\n", signo);
exit(0);
}
int main(void)
{
	signal(SIGINT, sigterm_handler);
	signal(SIGQUIT, sigterm_handler);
	while(1);
	return 0;
}

驱动中信号的释放

驱动中涉及三样工作
1.支持F_SETOWN命令, 能在这个控制命令处理中设置filp->f_owner为对应进程ID。 不过此项工作已由内核完成, 设备驱动无须处理。
2.驱动中应该实现file_operations中的fasync函数。
3.在设备资源可获得时, 调用kill_fasync函数激发相应的信号。

struct fasync_struct *async_queue; /* 异步相关结构体 */
static int xxx_fasync(int fd, struct file *filp, int on){
	if (fasync_helper(fd, filp, on, &async_queue) < 0)
		return -EIO;
	return 0;
}
static ssize_t xxx_write(struct file *filp, const char __user *buf, size_t count,loff_t *f_pos){
	/* 产生异步读信号 */
	if (async_queue)
		kill_fasync(&async_queue, SIGIO, POLL_IN);
}
static ssize_t xxx_read(struct file *filp, const char __user *buf, size_t count,loff_t *f_pos){
	/* 产生异步读信号 */
	if (async_queue)
		kill_fasync(&async_queue, SIGIO, POLL_OUT);
}
static int xxx_release(struct inode *inode, struct file *filp){
	return xxx_fasync(-1, filp, 0); /* 删除异步通知 */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值