您的位置:首页 > 运维架构 > Linux

Linux内核的异步通知

2013-10-07 22:28 351 查看

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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: