linux 内核异步通知

1、异步通知简介

1)中断是处理器提供的一种异步机制,配置好中断以后就可以让处理器去处理其他的事情了,当中断发生以后会触发我们事先设置好的中断服务函数,在中断服务函数中做具体的处理。

2)通过阻塞方式访问的话应用程序会处于休眠态,等待驱动设备可以使用,非阻塞方式的话会通过 poll 函数来不断的轮询,查看驱动设备文件是否可以使用。这两种方式都需要应用程序主动的去查询设备的使用情况。

3)信号
驱动可以通过主动向应用程序发送信号的方式来报告自己可以访问了,应用程序获取到信号以后就可以从驱动设备中读取或者写入数据了。整个过程就相当于应用程序收到了驱动发送过来了的一个中断,然后应用程序去响应这个中断。

异步通知信号定义在arch/xtensa/include/uapi/asm/signal.h 文件中有很多的

#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 /* 未使用信号 */

除了 SIGKILL(9)和 SIGSTOP(19)这两个信号不能被忽略外,其他的信号都可以忽略。
这些信号就相当于中断号,不同的中断号代表了不同的中断,不同的中断所做的处理不同,因此,驱动程序可以通过向应用程序发送不同的信号来实现不同的功能。

2、驱动中的信号处理

fasync_struct 结构体

struct fasync_struct {
	spinlock_t fa_lock;
	int magic;
	int fa_fd;
	struct fasync_struct *fa_next;
	struct file *fa_file;
	struct rcu_head fa_rcu;
};

在设备结构体中添加,fasync_struct 指针变量

struct xxx_dev {
	struct fasync_struct *async_queue; /* 异步相关结构体 */
};

异步通知,需要在设备驱动中实现 file_operations 操作集中的 fasync 函数

int (*fasync) (int fd, struct file *filp, int on)

fasync 函数里面一般通过调用 fasync_helper 函数来初始化前面定义的 fasync_struct 结构体
指针

int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)

内核使用示例

struct xxx_dev {
	......
	struct fasync_struct *async_queue; /* 异步相关结构体 */
};

static int xxx_fasync(int fd, struct file *filp, int on)
{
	struct xxx_dev *dev = (xxx_dev)filp->private_data;
	if (fasync_helper(fd, filp, on, &dev->async_queue) < 0)
	return -EIO;
	 return 0;
 }

static struct file_operations xxx_ops = {
	 ......
	.fasync = xxx_fasync,
	.release = xxx_release,
	 ......
 };
static int xxx_release(struct inode *inode, struct file *filp)
{
	return xxx_fasync(-1, filp, 0); /* 删除异步通知 */
}

kill_fasync 函数
当设备可以访问的时候,驱动程序需要向应用程序发出信号。 kill_fasync函数负责发送指定的信号, kill_fasync 函数原型如下所示:

void kill_fasync(struct fasync_struct **fp, int sig, int band)

fp:要操作的 fasync_struct。sig: 要发送的信号。band: 可读时设置为 POLL_IN,可写时设置为 POLL_OUT

2、应用程序处理流程

1)注册信号处理函数
signal 函数原型如下所示:

sighandler_t signal(int signum, sighandler_t handler)

signum:要设置处理函数的信号。
handler: 信号的处理函数。
2)将本应用程序的进程号告诉内核
使用 fcntl(fd, F_SETOWN, getpid())将本应用程序的进程号告诉给内核。
3)开启异步通知

flags = fcntl(fd, F_GETFL); /* 获取当前的进程状态 */
fcntl(fd, F_SETFL, flags | FASYNC); /* 开启当前进程异步通知功能 */

通过 fcntl 函数设置进程状态为 FASYNC,经过这一步,驱动程序中的 fasync 函数就会执行。

应用程序使用示例


static void sigio_signal_func(int signum)
{
	int err = 0;
	unsigned int keyvalue = 0;
	
	err = read(fd, &keyvalue, sizeof(keyvalue));
	if(err < 0) {

	 } else {
		printf("sigio signal! key value=%d\r\n", keyvalue);
	 }
}

void main()
{
	...
	fd = open(filename, O_RDWR);

	signal(SIGIO, sigio_signal_func);
	fcntl(fd, F_SETOWN, getpid()); /* 将当前进程的进程号告诉给内核 */
	flags = fcntl(fd, F_GETFD); /* 获取当前的进程状态 */
	fcntl(fd, F_SETFL, flags | FASYNC);/* 设置进程启用异步通知功能 */
	
	while(1) {
		sleep(2);
	}
	
	close(fd);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为了维护世界和平_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值