一、简介
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 SCIGFPE 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 /* 未使用信号 */
二、驱动中的信号处理
1. 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;
};
/* 初始化 */
int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
/* 前3个参数就是file_operation中的fasync函数的参数,这两个函数一般一起使用。*/
2.fasync函数
要使用异步通知,需要在设备驱动中实现file_operations操作集中的fasync函数。当应用程序通过"fcntl(fd, F_SETFL,flag | FASYNC)"改变fasync标记时候,该函数就会执行。
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,
......
};
static int xxx_release(struct inode *inode, struct file *filp)
{
return xxx_fasync(-1, filp, 0); /* 删除异步通知 */
}
static struct file_operations xxx_ops = {
......
.release = xxx_release,
};
3. kill_fasync函数
/* 当设备可以访问的时候,驱动程序需要向应用程序发出信号,相当于产生“中断”。 */
void kill_fasync(struct fasync_struct **fp, int sig, int band)
/*
* fp:要操作的 fasync_struct。
* sig: 要发送的信号。
* band: 可读时设置为 POLL_IN,可写时设置为 POLL_OUT。
* 返回值: 无。
*/
三、应用程序对异步通知的处理
1. 注册信号处理函数
sighandler_t signal(int signum, sighandler_t handler)
/* signum: 要处理函数的信号
* handler: 信号的处理函数
* 返回值: 设置成功的话返回信号的前一个处理函数, 设置失败的话返回SIG_ERR。
*/
2. 将本应用程序的进程号告诉给内核
fcntl(fd, F_SETOWN, getpid())
3. 开启异步通知
flags = fcntl(fd, F_GETFL); /* 获取当前的进程状态 */
fcntl(fd, F_SETFL, flags | FASYNC); /* 开启当前进程异步通知功能 */
四、实例
- 驱动
struct xxx_dev{
...
struct fasync_struct *async_queue; /* 异步相关的结构体指针 */
}
struct xxx_dev dev
void xxx_func() /* 释放信号的函数 */
{
...
if(condition) /* 可读 */
if(dev->async_queue)
kill_fasync(&dev->async_queue, SIGIO, POLL_IN); /* 释放可进行IO操作得信号 */
}
static int xxx_fasync(int fd, struct file *filp, int on)
{
struct xxx_dev *dev = (struct imx6uirq_dev *)filp->private_data;
return fasync_helper(fd, filp, on, &dev->async_queue);
}
static int xxx_release(struct inode *inode, struct file *filp)
{
return xxx_fasync(-1, filp, 0);
}
static struct file_operations xxx_fops = {
...
.fasync = xxx_fasync,
.release = xxx_release,
};
- 应用程序
staitic int fd = 0;
static void sigio_signal_func(int signum)
{
/* 信号触发函数 */
}
int main(int argc, char *argv[])
{
int flags;
...
signal(SIGIO, sigio_signal_func); /* 设置处理函数 */
fcntl(fd, F_SETOWN, getpid()); /* 将当前进程的进程号告诉内核 */
flags = fcntl(fd, F_GETFD); /* 获取当前的进程状态 */
fcntl(fd, F_SETFL, flags | FASYNC); /* 设置进程启用异步通知功能 */
...
}