linux硬件驱动通知应用程序,Linux设备驱动中的异步通知

异步通知的概念:

一旦设备就绪,主动通知应用程序。这样应用程序根本不需要查询设备状态。

阻塞,非阻塞和异步:

-阻塞IO是等待设备可访问后再访问

-非阻塞IO是查询设备是否可以访问

-异步通知是设备通知自身可以访问

为了使支持异步通知机制,驱动程序应该涉及以下3项工作。

支持F_SETOWN命令,能在这个控制命令处理。

处理FASYNC标志更变的函数

Int fasync_helper(int fd, struct file*filp, int mode, struct fasync_struct **fa);

释放信号用的函数

Void kill_fasync(struct fasync_struct **fa,int sig, int band);

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

{

/*First a quick test without locking: usually

* the list is empty.

*/

if(*fp) {

read_lock(&fasync_lock);

/*reread *fp after obtaining the lock */

__kill_fasync(*fp,sig, band);

read_unlock(&fasync_lock);

}

}

void __kill_fasync(struct fasync_struct*fa, int sig, int band)

{

while(fa) {

structfown_struct * fown;

if(fa->magic != FASYNC_MAGIC) {

printk(KERN_ERR"kill_fasync: bad magic number in "

"fasync_struct!\n");

return;

}

fown= &fa->fa_file->f_owner;

/*Don't send SIGURG to processes which have not set a

queued signum: SIGURG has its own defaultsignalling

mechanism. */

if(!(sig == SIGURG && fown->signum == 0))

send_sigio(fown,fa->fa_fd, band);

fa= fa->fa_next;

}

}

void send_sigio(struct fown_struct *fown,int fd, int band)

{

structtask_struct *p;

enumpid_type type;

structpid *pid;

intgroup = 1;

read_lock(&fown->lock);

type= fown->pid_type;

if(type == PIDTYPE_MAX) {

group= 0;

type= PIDTYPE_PID;

}

pid= fown->pid;

if(!pid)

gotoout_unlock_fown;

read_lock(&tasklist_lock);

do_each_pid_task(pid, type, p) {

send_sigio_to_task(p,fown, fd, band, group);

} while_each_pid_task(pid, type, p);

read_unlock(&tasklist_lock);

out_unlock_fown:

read_unlock(&fown->lock);

}

通过内核,给所有的进程发送信号。

用户对应进程可以接受该信号,并作处理。

用户进程要做的是:

1.绑定信号和信号处理函数

Signal(SIGIO,input_handler)

2.设定本进程为某个标准输入文件的拥有者。这样内核就知道信号发送给那个进程。

Fcntl(STDIN_FILENO,F_SETNO,getpid());

3.对输入文件设定异步标志位。

Fcntl(STDIN_FILENO,F_SETFL,oflags | FASYNC);0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值