异步通知: 当设备可以被进行访问时,由驱动程序主动通知应用程序进行访问,使得使用无阻塞I/O的应用程序无需轮询设备是否可访问,而阻塞访问也可被异步通知取代。
1. 异步通知是设备通知自身可被访问
2. 在用户空间对信号的处理方式: 捕获忽略信号,捕获处理信号,系统默认处理。SIGKILL 和 SIGSTOP 不能被捕获处理或捕获忽略。
3. 在用户空间处理一个设备释放的信号所需完成的工作:
(1)为了能让设备驱动发出的信号能被本进程接受到,需要通过 F_SETOWN 控制命令设置设备文件的拥有者为本进程。
(2)使设备支持异步通知,通过 F_SETFL 命令设置设备文件的 FASYNC 标志。
(3)通过signal()给信号注册处理函数。
4. 内核空间应完成的工作:
(1)支持 F_SETOWN 命令,通过对该命令的处理执行设置filp->f_owner 为该进程的ID。
(2)支持 F_SETFL 命令,驱动中应实现 fasync() 函数,FASYNC标志改变时,驱动中的 fasync() 函数将执行。
(3)通过 kill_fasnc() 发出所需信号。
5. 用户空间的编程
实现监视标准的输入文件 STDIN_FILENO 的输入信号。
实现内容:
(1) 主程序。
(2) 信号处理函数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <fcntl.h> int flag=0; void sighandler(int signo) { if (signo==SIGIO) flag++; printf("in sighandler "); return; } char buffer[4096]; int main(int argc, char **argv) { int count; struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_handler = sighandler; action.sa_flags = 0; sigaction(SIGIO, &action, NULL); fcntl(STDIN_FILENO, F_SETOWN, getpid()); /*STDIN_FILENO is a standard input file descriptor*/ fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | FASYNC); /* join in asynchronous notification list table*/ while(1) { /* this only returns if a signal arrives */ printf("into sleep "); sleep(86400); /* one day */ printf("after sleep "); if (!flag) continue; count=read(0, buffer, 4096); /*read form standard input file 0*/ /* buggy: if avail data is more than 4kbytes... */ write(1,buffer,count); flag=0; } }
分析:sleep()系统调用 makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which is not ignored
6. 驱动编程
实现内容:
(1) 实现 fasync 函数,对应 fcntl(F_SETFL.....) 系统调用。
(2) 发送信号。
代码以ldd3实例代码为例。