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
主角们闪亮登场:
![](/attachment/201407/20/23069658_1405870347Df1F.jpg)
It's time time to show :)
![](/attachment/201407/20/23069658_1405870770jltq.jpg)
我们可以看到,女神初到人间时需要用钱,结果没人搭理,去了趟韩国回来之后,被土豪给瞄到了,于是土豪开始大把大把地视金钱如粪土。过了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 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣 ...
随机推荐
- leetcode 206
206. Reverse Linked List Reverse a singly linked list. 翻转一个单链表. 代码如下: /** * Definition for singly-li ...
- arcgis中DEM如何生成等高线
地形图指比例尺大于1∶100万的着重表示地形的普通地图(根据经纬度进行分幅,常用有1:100万,1:50万,1比25万,1:15万,1:10万,1:5万等等).由于制图的区域范围比较小,因此能比较精确 ...
- qsort库函数的用法
qsort 功 能: 使用快速排序例程进行排序 用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *, ...
- EJDK, Raspberry Pi, and NetBeans IDE 8
https://blogs.oracle.com/geertjan/entry/youtube_ejdk_raspberry_pi_and
- delphi判断文件类型
function getFileType(inputFile:string):string;const JPEG_FLAG_BEGIN = $D8FF; JPEG_FLAG_END = $D9FF; ...
- PHP防SQL注入不要再用addslashes和mysql_real_escape_string
PHP防SQL注入不要再用addslashes和mysql_real_escape_string了,有需要的朋友可以参考下. 博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助 ...
- 自动备份SQL数据库 并删除指定日期之前的备份文件
/// <summary> /// 数据备份 /// </summary> /// public bool DataBackup(st ...
- shell 里的变量 总结
对于linux shell的使用者来说, 巧妙的应用变量不仅能够快速的解决问题,同时能够获取非常大的乐趣,因为shell的变量内部可以附加一些运算,使得程序非常简洁明了并且功能强大,以下详细介绍一下: ...
- Two Pointers Day
(1)Reverse String 解题思路简单明了,但是要注意时间复杂度问题!!! 代码如下:(声明一个与字符串等长的char数组,然后倒序区字符串中的字符,放入数组即可.) public clas ...
- 分支合并git checkout adview git merge adview3
分支合并 git checkout adview git merge adview3