Linux内核的异步通知

来源:互联网 发布:域名赎回期是多久 编辑:程序博客网 时间:2024/05/29 14:27

Linux内核的异步通知


异步通知类似于中断,主要用于实现驱动通过发送信号通知应用程序。

应用层:

void my_signal_fun(int signum)
{
..............................
}


int main(int argc, char **argv)
{
int fd;
int Oflags;

signal(SIGIO, my_signal_fun);

fd = open("/dev/xxx", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return -1;
}

fcntl(fd, F_SETOWN, getpid());   // 告诉驱动要发信号给本应用程序。

Oflags = fcntl(fd, F_GETFL);   //  获取flags

fcntl(fd, F_SETFL, Oflags | FASYNC);           //   设置flags支持异步通知   ,最终调用到驱动的fasync函数进行初始化。

while (1);

return 0;
}


驱动层:

static struct fasync_struct *my_async;

static int drv_fasync (int fd, struct file *filp, int on)
{
return fasync_helper (fd, filp, on, &my_async);
}

static struct file_operations drv_fops = {
         .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
 .fasync =  drv_fasync,
};
static int drv_init(void)
{
register_chrdev(major  , "drv", &drv_fops);
}
static void drv_exit(void)
{
unregister_chrdev(major, "drv");
}

kill_fasync (&my_async, SIGIO, POLL_IN);   //向应用程序发送SIGIO信号


module_init(drv_init);
module_exit(drv_exit);
MODULE_LICENSE("GPL");

原创粉丝点击