Linux 内核通知链随笔【中】
关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不同子系统之间的通信,通知链只能用于内核不同子系统之间的通信。那么内核通知链到底是怎么工作的?我们如何才能用好通知链?内核源代码里随处可见的通知链身影,我们到底该如何理解呢?本片博文过后,您的这些疑问和顾虑将统统消除。
以前有个女神,超凡脱俗、出水芙蓉,不过在怎么滴也是人,是人就会有各种各样的需求,女神的所有需求都放在她的需求链表里requirment_chain,比如物质需求,精神需求等等。然后女神首先需要做的事情就是将自己的需求链给实例化了:
点击(此处)折叠或打开
- /* Godness.c */
- /* 我们假设女神需求链的类型是原始通知链(PS:不要和原始需求挂钩理解 -_-||)*/
- static RAW_NOTIFIER_HEAD(requirment_chain);
当需求被定义出来后,还需要向外提供两个接口:一个是别人用于满足她需求的接口,另一个是别人需要和她break out的接口(虽然在现实生活中这种情况比较令人sadness,但女神所在的虚拟世界里这个是必须的)。于是女神提供了别人往其需求链注册响应函数的接口和卸载响应函数的接口:
点击(此处)折叠或打开
- /* Godness.c*/
- int register_godness_notifier(struct notifier_block *nb)
- {
- return raw_notifier_chain_register(&requirment_chain, nb);
- }
- EXPORT_SYMBOL(register_godness_notifier); //注册函数实现了之后必须将其公布出去,不然别人怎么看得到呢
- int unregister_godness_notifier(struct notifier_block *nb)
- {
- return raw_notifier_chain_unregister(&requirment_chain, nb);
- }
- EXPORT_SYMBOL(unregister_godness_notifier); //同上
然后,女神要做的就是提需求,并看看哪些屌丝、土豪或高富帅来追求自己:
点击(此处)折叠或打开
- int call_godness_notifier_chain(unsigned long val, void *v)
- {
- return raw_notifier_call_chain(&requirment_chain, val, v);
- }
- EXPORT_SYMBOL(call_godness_notifier_chain);
为了模拟测试过程,我们需要一个内核线程,模拟女神提需求的过程,然后不断调用上面的需求响应的检测函数。我们姑且认为认为女神的需求有两种:物质需求就是对menoy的需求,精神需求就是音乐的需求。女神每3秒钟提一个需求,一共提10个需求:
点击(此处)折叠或打开
- #define PHY_REQ 0 //物质需求
- #define SPR_REQ 1 //精神需求
- #define REQ_MAX SPR_REQ+1
- static int make_requirment_thread(void *data)
- {
- int i = 10;
- struct completion cmpl;
- unsigned int requirment_type = 0;
- printk("[Godness]requirements thread starting...\n");
- while((i--) > 0){
- init_completion(&cmpl);
- wait_for_completion_timeout(&cmpl, 3 * HZ);
- get_random_bytes(&requirment_type,sizeof(requirment_type)); //生成一个内核随机数
- requirment_type %= REQ_MAX; //需求类型之可能是0或者1
- printk("[Godness]requirment type: %d \n",requirment_type);
- call_godness_notifier_chain(requirment_type,NULL);
- }
- printk("[Godness]requirements thread ended!\n");
- return 0;
- }
女神的最终模型如下:
点击(此处)折叠或打开
- #include asm/uaccess.h>
- #include linux/types.h>
- #include linux/kernel.h>
- #include linux/sched.h>
- #include linux/notifier.h>
- #include linux/init.h>
- #include linux/types.h>
- #include linux/module.h>
- #include linux/kthread.h>
- MODULE_LICENSE("GPL");
- #define PHY_REQ 0 //物质需求
- #define SPR_REQ 1 //精神需求
- #define REQ_MAX SPR_REQ+1
- extern void get_random_bytes(void* buf,int nbytes);
- static struct task_struct *requirments_thread = NULL;
- /*
- * 女神所有的需求都会列在她的需求链里。这里我们定义了一个原始通知链,暂时没考虑锁的问题。
- */
- static RAW_NOTIFIER_HEAD(requirment_chain);
- /*
- * 如果谁想追求本女王,就来献殷勤吧
- */
- int register_godness_notifier(struct notifier_block *nb)
- {
- return raw_notifier_chain_register(&requirment_chain, nb);
- }
- EXPORT_SYMBOL(register_godness_notifier);
- /*
- * 伺候不起的,赶紧Get out as soon as
- */
- int unregister_godness_notifier(struct notifier_block *nb)
- {
- return raw_notifier_chain_unregister(&requirment_chain, nb);
- }
- EXPORT_SYMBOL(unregister_godness_notifier);
- /*
- * 本女王开始提需求了,看看谁能才是真心的。
- */
- int call_godness_notifier_chain(unsigned long val, void *v)
- {
- return raw_notifier_call_chain(&requirment_chain, val, v);
- }
- EXPORT_SYMBOL(call_godness_notifier_chain);
- static int make_requirment_thread(void *data)
- {
- int i = 10;
- struct completion cmpl;
- unsigned int requirment_type = 0;
- printk("[Godness]requirements thread starting...\n");
- while((i--) > 0){
- init_completion(&cmpl);
- wait_for_completion_timeout(&cmpl, 3 * HZ);
- get_random_bytes(&requirment_type,sizeof(requirment_type)); //生成一个内核随机数
- requirment_type %= REQ_MAX; //需求类型之可能是0或者1
- printk("[Godness]requirment type: %d \n",requirment_type);
- call_godness_notifier_chain(requirment_type,NULL);
- }
- printk("[Godness]requirements thread ended!\n");
- return 0;
- }
- static int __init godness_init_notifier(void)
- {
- printk("[Attention]The Godness coming into the world!\n");
- requirments_thread = kthread_run(make_requirment_thread,NULL,"Godness_requirments_thread");
- return 0;
- }
- static void __exit godness_exit_notifier(void)
- {
- printk("[Attention]The Godness leaving out!\n");
- }
- module_init(godness_init_notifier);
- module_exit(godness_exit_notifier);
这个时候有个叫土豪的家伙,突然于茫茫人海中发现了女神,并且知道了女神有金钱需求的欲望,于是土豪向女神的需求链里注册了一个金钱的响应函数,这样一旦女神需要用钱的时候他第一时间就能收到通知,然后以迅雷下载不及掩耳盗铃之势加以满足:
点击(此处)折叠或打开
- /*Tuhao.c*/
- extern int register_godness_notifier(struct notifier_block*);
- extern int unregister_godness_notifier(struct notifier_block*);
- static int baby_need_money(struct notifier_block *this, unsigned long event, void *ptr)
- {
- if(event != 0) //不是金钱需求关我鸟事
- {
- return NOTIFY_DONE; //Don't care
- }
- printk("[Tuhao]Hi Baby,$$$$$$$$ 么么哒 \n");
- return NOTIFY_OK;
- }
- static struct notifier_block cash_notifier =
- {
- .notifier_call = baby_need_money,
- .priority = 2,
- };
- static int __init tuhao_register(void)
- {
- int err;
- printk("[Tuhao]Tuhao register cash_requirment response to Godness...");
- err = register_godness_notifier(&cash_notifier);
- if (err)
- {
- printk("Refused!\n");
- return -1;
- }
- printk("Accepted!\n");
- return err;
- }
- static void __exit tuhao_unregister(void)
- {
- unregister_godness_notifier(&cash_notifier);
- printk("[Tuhao]Tuhao is giving up Godness!(Son of bitch)\n");
- }
- module_init(tuhao_register);
- module_exit(tuhao_unregister);
这时,有一个屌丝,也于茫茫人海中发现了女神,他发现女神喜欢音乐,于是他开始响应女神的精神需求:
点击(此处)折叠或打开
- /*Diors.c*/
- extern int register_godness_notifier(struct notifier_block*);
- extern int unregister_godness_notifier(struct notifier_block*);
- static int godness_need_music(struct notifier_block *this, unsigned long event, void *ptr)
- {
- if(event != 1) //我又没钱,给不了你大房子、气派的车子...
- {
- return NOTIFY_DONE; //Don't care
- }
- printk("[Diors]Hi girl,This is a classic Music disk,take it. \n");
- return NOTIFY_OK;
- }
- static struct notifier_block music_notifier =
- {
- .notifier_call = godness_need_music,
- .priority = 2,
- };
- static int __init diors_register(void)
- {
- int err;
- printk("[Diors]Diors register music_requirment response to Godness...");
- err = register_godness_notifier(&music_notifier);
- if (err)
- {
- printk("Refused!\n");
- return -1;
- }
- printk("Accepted!\n");
- return err;
- }
- static void __exit diors_unregister(void)
- {
- unregister_godness_notifier(&music_notifier);
- printk("[Diors]Tuhao is giving up Godness!(What a pity)\n");
- }
- module_init(diors_register);
- module_exit(diors_unregister);
好的,到此为止,一切就绪,好戏正式开始:
点击(此处)折叠或打开
- #Makefile for fun
- obj-m:=Goddess.o Tuhao.o Diors.o
- CURRENT_PATH := $(shell pwd)
- KERNEL_VERSION := $(shell uname -r)
- KERNEL_HEADER_DIR := /usr/src/kernels/$(LINUX_KERNELKERNEL_VERSION)
- all:
- make -C $(KERNEL_HEADER_DIR) M=$(CURRENT_PATH) modules
- clean:
- make -C $(KERNEL_HEADER_DIR) M=$(CURRENT_PATH) clean
主角们闪亮登场:

It's time time to show :)

我们可以看到,女神初到人间时需要用钱,结果没人搭理,去了趟韩国回来之后,被土豪给瞄到了,于是土豪开始大把大把地视金钱如粪土。过了8秒钟,屌丝男也发现了女神,当女神想听歌时屌丝男就屁颠屁颠地把自己珍藏了多年的古典乐光盘送给了女神。最后剧中,谢幕。
OK,让我们总结一下Linux内核通知链的应用场景。如果一个子系统需要向外通告事件时,它需要首先定义自己的通知链对象,然后向内核里其他子系统提供一个向自己的通知链注册消息响应函数的接口,当然也必须提供一个用于从自己从自己的通知链上卸载响应函数的接口。接下来,我们这个子系统要做的事情就是根据自己的实际运行情况,定期地产生一些消息,并调用自己通知链里别的系统已经注册好了消息响应函数,这样别的子系统就可以根据我们这个系统的的消息类型进行一些处理动作。
那么多个子系统对我们的同一种消息都挂有响应函数时该怎么处理?鉴于时间关系,下次再叙。
未完,待续...
Linux 内核通知链随笔【中】的更多相关文章
- Linux 内核通知链随笔【中】【转】
转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...
- Linux内核通知链模块
通知链描写叙述 大多数内核子系统都是相互独立的,因此某个子系统可能对其他子系统产生的事件感兴趣. 为了满足这个需求,也即是让某个子系统在发生某个事件时通知其他的子系统.Linux内核提供了通知链的机制 ...
- [Linux] 内核通知链 notifier
Linux 内核中每个模块之间都是独立的,如果模块需要感知其他模块的事件,就需要用到内核通知链. 最典型的通知链应用就是 LCD 和 TP 之间,TP 需要根据 LCD 的亮灭来控制是否打开关闭触摸功 ...
- Linux 内核通知链机制的原理及实现
一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...
- Linux内核通知链机制的原理及实现【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/11/11/2396781.html 一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其 ...
- Linux内核调试方法总结之内核通知链
Linux内核通知链notifier 1.内核通知链表简介(引用网络资料) 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在 ...
- 从基本理解到深入探究 Linux kernel 通知链(notifier chain)【转】
转自:https://blog.csdn.net/u014134180/article/details/86563754 版权声明:本文为博主原创文章,未经博主允许不得转载.——Wu_Being ht ...
- Linux内核通知链分析【转】
转自:http://www.cnblogs.com/jason-lu/articles/2807758.html Linux内核通知链分析 1. 引言 Linux是单内核架构(monolithic k ...
- notifier chain — 内核通知链【转】
转自:http://blog.csdn.net/g_salamander/article/details/8081724 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣 ...
随机推荐
- thinkphp 配合mongodb
首先在config.php里面添加配合mongodb的数据库连接 'DB_TYPE'=>'mongo',// 数据库类型 'DB_HOST'=>'localhost',// 服务器地址 ' ...
- java类型占用字节数&类型转换
1.整型类型 存储需求 bit数 取值范围 备注int 4字节 4*8 short ...
- python2.6.6安装Image模块
python2.6.6安装Image模块1.下载Image模块源码地址:http://www.pythonware.com/products/pil/index.htm2.加压文件#tar zxvf ...
- Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
A few days ago, I've been facing a strange behavior with Visual Studio 2013. No matter what solu ...
- Xamarin Android自学和实践步骤
一.入门(已完成) 1.学习Xamarin Android项目的基本结构 2.学习界面布局的基本方式 3.学习基本编码规则 4.学习页面跳转和传值 5.学习对话框和提示信息显示方法 6.学习使用系统剪 ...
- 【原】无规矩,不方圆——说一说正则里的exec()和test()
今天一大早遇就遇到一件诡异的事儿,可能是思绪还没有澄静下来,一下子没反应过来.事情是这样的: 模板: <input class="name" type="text& ...
- spring的helloworld
前两天接到了支付宝的电话面试,问了我好多java中的知识,知识姐不看java好多年,这猛一问知道的东西还真是不少啊.趁着现在精力还算是比较旺盛,再把spring重新理一理,为下一次面试做准备. 学习每 ...
- git工作中的常用操作
上班开始,打开电脑,git pull:拉取git上最新的代码: 编辑代码,准备提交时,git stash:将自己编辑的代码暂存起来,防止git pull时与库中的代码起冲突,否则自己的代码就白敲了: ...
- java规范(二)
常量命名 不允许使用任何魔法值(未定义的常量)直接出现在代码中 反例: String key="Id#taobao_"+tradeId: cache.put(key, value) ...
- poj1753改
#include<iostream> char data[16]; int a[16]; int d[5]={0,-4,1,4,-1}; char b[16]; int flag; int ...