关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不同子系统之间的通信,通知链只能用于内核不同子系统之间的通信。那么内核通知链到底是怎么工作的?我们如何才能用好通知链?内核源代码里随处可见的通知链身影,我们到底该如何理解呢?本片博文过后,您的这些疑问和顾虑将统统消除。

以前有个女神,超凡脱俗、出水芙蓉,不过在怎么滴也是人,是人就会有各种各样的需求,女神的所有需求都放在她的需求链表里requirment_chain,比如物质需求,精神需求等等。然后女神首先需要做的事情就是将自己的需求链给实例化了:

点击(此处)折叠或打开

  1. /* Godness.c */
  2. /* 我们假设女神需求链的类型是原始通知链(PS:不要和原始需求挂钩理解 -_-||)*/
  3. static RAW_NOTIFIER_HEAD(requirment_chain);

当需求被定义出来后,还需要向外提供两个接口:一个是别人用于满足她需求的接口,另一个是别人需要和她break out的接口(虽然在现实生活中这种情况比较令人sadness,但女神所在的虚拟世界里这个是必须的)。于是女神提供了别人往其需求链注册响应函数的接口和卸载响应函数的接口:

点击(此处)折叠或打开

  1. /* Godness.c*/
  2. int register_godness_notifier(struct notifier_block *nb)
  3. {
  4. return raw_notifier_chain_register(&requirment_chain, nb);
  5. }
  6. EXPORT_SYMBOL(register_godness_notifier); //注册函数实现了之后必须将其公布出去,不然别人怎么看得到呢
  7. int unregister_godness_notifier(struct notifier_block *nb)
  8. {
  9. return raw_notifier_chain_unregister(&requirment_chain, nb);
  10. }
  11. EXPORT_SYMBOL(unregister_godness_notifier); //同上

然后,女神要做的就是提需求,并看看哪些屌丝、土豪或高富帅来追求自己:

点击(此处)折叠或打开

  1. int call_godness_notifier_chain(unsigned long val, void *v)
  2. {
  3. return raw_notifier_call_chain(&requirment_chain, val, v);
  4. }
  5. EXPORT_SYMBOL(call_godness_notifier_chain);

为了模拟测试过程,我们需要一个内核线程,模拟女神提需求的过程,然后不断调用上面的需求响应的检测函数。我们姑且认为认为女神的需求有两种:物质需求就是对menoy的需求,精神需求就是音乐的需求。女神每3秒钟提一个需求,一共提10个需求:

点击(此处)折叠或打开

  1. #define PHY_REQ 0 //物质需求
  2. #define SPR_REQ 1 //精神需求
  3. #define REQ_MAX SPR_REQ+1
  4. static int make_requirment_thread(void *data)
  5. {
  6. int i = 10;
  7. struct completion cmpl;
  8. unsigned int requirment_type = 0;
  9. printk("[Godness]requirements thread starting...\n");
  10. while((i--) > 0){
  11. init_completion(&cmpl);
  12. wait_for_completion_timeout(&cmpl, 3 * HZ);
  13. get_random_bytes(&requirment_type,sizeof(requirment_type));  //生成一个内核随机数
  14. requirment_type %= REQ_MAX;  //需求类型之可能是0或者1
  15. printk("[Godness]requirment type: %d \n",requirment_type);
  16. call_godness_notifier_chain(requirment_type,NULL);
  17. }
  18. printk("[Godness]requirements thread ended!\n");
  19. return 0;
  20. }

女神的最终模型如下:

点击(此处)折叠或打开

  1. #include asm/uaccess.h>
  2. #include linux/types.h>
  3. #include linux/kernel.h>
  4. #include linux/sched.h>
  5. #include linux/notifier.h>
  6. #include linux/init.h>
  7. #include linux/types.h>
  8. #include linux/module.h>
  9. #include linux/kthread.h>
  10. MODULE_LICENSE("GPL");
  11. #define PHY_REQ 0 //物质需求
  12. #define SPR_REQ 1 //精神需求
  13. #define REQ_MAX SPR_REQ+1
  14. extern void get_random_bytes(void* buf,int nbytes);
  15. static struct task_struct *requirments_thread = NULL;
  16. /*
  17. * 女神所有的需求都会列在她的需求链里。这里我们定义了一个原始通知链,暂时没考虑锁的问题。
  18. */
  19. static RAW_NOTIFIER_HEAD(requirment_chain);
  20. /*
  21. * 如果谁想追求本女王,就来献殷勤吧
  22. */
  23. int register_godness_notifier(struct notifier_block *nb)
  24. {
  25. return raw_notifier_chain_register(&requirment_chain, nb);
  26. }
  27. EXPORT_SYMBOL(register_godness_notifier);
  28. /*
  29. * 伺候不起的,赶紧Get out as soon as
  30. */
  31. int unregister_godness_notifier(struct notifier_block *nb)
  32. {
  33. return raw_notifier_chain_unregister(&requirment_chain, nb);
  34. }
  35. EXPORT_SYMBOL(unregister_godness_notifier);
  36. /*
  37. * 本女王开始提需求了,看看谁能才是真心的。
  38. */
  39. int call_godness_notifier_chain(unsigned long val, void *v)
  40. {
  41. return raw_notifier_call_chain(&requirment_chain, val, v);
  42. }
  43. EXPORT_SYMBOL(call_godness_notifier_chain);
  44. static int make_requirment_thread(void *data)
  45. {
  46. int i = 10;
  47. struct completion cmpl;
  48. unsigned int requirment_type = 0;
  49. printk("[Godness]requirements thread starting...\n");
  50. while((i--) > 0){
  51. init_completion(&cmpl);
  52. wait_for_completion_timeout(&cmpl, 3 * HZ);
  53. get_random_bytes(&requirment_type,sizeof(requirment_type));  //生成一个内核随机数
  54. requirment_type %= REQ_MAX;  //需求类型之可能是0或者1
  55. printk("[Godness]requirment type: %d \n",requirment_type);
  56. call_godness_notifier_chain(requirment_type,NULL);
  57. }
  58. printk("[Godness]requirements thread ended!\n");
  59. return 0;
  60. }
  61. static int __init godness_init_notifier(void)
  62. {
  63. printk("[Attention]The Godness coming into the world!\n");
  64. requirments_thread = kthread_run(make_requirment_thread,NULL,"Godness_requirments_thread");
  65. return 0;
  66. }
  67. static void __exit godness_exit_notifier(void)
  68. {
  69. printk("[Attention]The Godness leaving out!\n");
  70. }
  71. module_init(godness_init_notifier);
  72. module_exit(godness_exit_notifier);

这个时候有个叫土豪的家伙,突然于茫茫人海中发现了女神,并且知道了女神有金钱需求的欲望,于是土豪向女神的需求链里注册了一个金钱的响应函数,这样一旦女神需要用钱的时候他第一时间就能收到通知,然后以迅雷下载不及掩耳盗铃之势加以满足:

点击(此处)折叠或打开

  1. /*Tuhao.c*/
  2. extern int register_godness_notifier(struct notifier_block*);
  3. extern int unregister_godness_notifier(struct notifier_block*);
  4. static int baby_need_money(struct notifier_block *this, unsigned long event, void *ptr)
  5. {
  6. if(event != 0)  //不是金钱需求关我鸟事
  7. {
  8. return NOTIFY_DONE; //Don't care
  9. }
  10. printk("[Tuhao]Hi Baby,$$$$$$$$ 么么哒 \n");
  11. return NOTIFY_OK;
  12. }
  13. static struct notifier_block cash_notifier =
  14. {
  15. .notifier_call = baby_need_money,
  16. .priority = 2,
  17. };
  18. static int __init tuhao_register(void)
  19. {
  20. int err;
  21. printk("[Tuhao]Tuhao register cash_requirment response to Godness...");
  22. err = register_godness_notifier(&cash_notifier);
  23. if (err)
  24. {
  25. printk("Refused!\n");
  26. return -1;
  27. }
  28. printk("Accepted!\n");
  29. return err;
  30. }
  31. static void __exit tuhao_unregister(void)
  32. {
  33. unregister_godness_notifier(&cash_notifier);
  34. printk("[Tuhao]Tuhao is giving up Godness!(Son of bitch)\n");
  35. }
  36. module_init(tuhao_register);
  37. module_exit(tuhao_unregister);

这时,有一个屌丝,也于茫茫人海中发现了女神,他发现女神喜欢音乐,于是他开始响应女神的精神需求:

点击(此处)折叠或打开

  1. /*Diors.c*/
  2. extern int register_godness_notifier(struct notifier_block*);
  3. extern int unregister_godness_notifier(struct notifier_block*);
  4. static int godness_need_music(struct notifier_block *this, unsigned long event, void *ptr)
  5. {
  6. if(event != 1) //我又没钱,给不了你大房子、气派的车子...
  7. {
  8. return NOTIFY_DONE; //Don't care
  9. }
  10. printk("[Diors]Hi girl,This is a classic Music disk,take it. \n");
  11. return NOTIFY_OK;
  12. }
  13. static struct notifier_block music_notifier =
  14. {
  15. .notifier_call = godness_need_music,
  16. .priority = 2,
  17. };
  18. static int __init diors_register(void)
  19. {
  20. int err;
  21. printk("[Diors]Diors register music_requirment response to Godness...");
  22. err = register_godness_notifier(&music_notifier);
  23. if (err)
  24. {
  25. printk("Refused!\n");
  26. return -1;
  27. }
  28. printk("Accepted!\n");
  29. return err;
  30. }
  31. static void __exit diors_unregister(void)
  32. {
  33. unregister_godness_notifier(&music_notifier);
  34. printk("[Diors]Tuhao is giving up Godness!(What a pity)\n");
  35. }
  36. module_init(diors_register);
  37. module_exit(diors_unregister);

好的,到此为止,一切就绪,好戏正式开始:

点击(此处)折叠或打开

  1. #Makefile for fun
  2. obj-m:=Goddess.o Tuhao.o Diors.o
  3. CURRENT_PATH := $(shell pwd)
  4. KERNEL_VERSION := $(shell uname -r)
  5. KERNEL_HEADER_DIR := /usr/src/kernels/$(LINUX_KERNELKERNEL_VERSION)
  6. all:
  7. make -C $(KERNEL_HEADER_DIR) M=$(CURRENT_PATH) modules
  8. clean:
  9. make -C $(KERNEL_HEADER_DIR) M=$(CURRENT_PATH) clean

主角们闪亮登场:

It's time time to show :)

我们可以看到,女神初到人间时需要用钱,结果没人搭理,去了趟韩国回来之后,被土豪给瞄到了,于是土豪开始大把大把地视金钱如粪土。过了8秒钟,屌丝男也发现了女神,当女神想听歌时屌丝男就屁颠屁颠地把自己珍藏了多年的古典乐光盘送给了女神。最后剧中,谢幕。

OK,让我们总结一下Linux内核通知链的应用场景。如果一个子系统需要向外通告事件时,它需要首先定义自己的通知链对象,然后向内核里其他子系统提供一个向自己的通知链注册消息响应函数的接口,当然也必须提供一个用于从自己从自己的通知链上卸载响应函数的接口。接下来,我们这个子系统要做的事情就是根据自己的实际运行情况,定期地产生一些消息,并调用自己通知链里别的系统已经注册好了消息响应函数,这样别的子系统就可以根据我们这个系统的的消息类型进行一些处理动作。
   那么多个子系统对我们的同一种消息都挂有响应函数时该怎么处理?鉴于时间关系,下次再叙。
   未完,待续...

Linux 内核通知链随笔【中】的更多相关文章

  1. Linux 内核通知链随笔【中】【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...

  2. Linux内核通知链模块

    通知链描写叙述 大多数内核子系统都是相互独立的,因此某个子系统可能对其他子系统产生的事件感兴趣. 为了满足这个需求,也即是让某个子系统在发生某个事件时通知其他的子系统.Linux内核提供了通知链的机制 ...

  3. [Linux] 内核通知链 notifier

    Linux 内核中每个模块之间都是独立的,如果模块需要感知其他模块的事件,就需要用到内核通知链. 最典型的通知链应用就是 LCD 和 TP 之间,TP 需要根据 LCD 的亮灭来控制是否打开关闭触摸功 ...

  4. Linux 内核通知链机制的原理及实现

    一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...

  5. Linux内核通知链机制的原理及实现【转】

    转自:http://www.cnblogs.com/armlinux/archive/2011/11/11/2396781.html 一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其 ...

  6. Linux内核调试方法总结之内核通知链

    Linux内核通知链notifier 1.内核通知链表简介(引用网络资料)    大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在 ...

  7. 从基本理解到深入探究 Linux kernel 通知链(notifier chain)【转】

    转自:https://blog.csdn.net/u014134180/article/details/86563754 版权声明:本文为博主原创文章,未经博主允许不得转载.——Wu_Being ht ...

  8. Linux内核通知链分析【转】

    转自:http://www.cnblogs.com/jason-lu/articles/2807758.html Linux内核通知链分析 1. 引言 Linux是单内核架构(monolithic k ...

  9. notifier chain — 内核通知链【转】

    转自:http://blog.csdn.net/g_salamander/article/details/8081724 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣 ...

随机推荐

  1. leetcode 206

    206. Reverse Linked List Reverse a singly linked list. 翻转一个单链表. 代码如下: /** * Definition for singly-li ...

  2. arcgis中DEM如何生成等高线

    地形图指比例尺大于1∶100万的着重表示地形的普通地图(根据经纬度进行分幅,常用有1:100万,1:50万,1比25万,1:15万,1:10万,1:5万等等).由于制图的区域范围比较小,因此能比较精确 ...

  3. qsort库函数的用法

    qsort 功 能: 使用快速排序例程进行排序  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *, ...

  4. EJDK, Raspberry Pi, and NetBeans IDE 8

    https://blogs.oracle.com/geertjan/entry/youtube_ejdk_raspberry_pi_and

  5. delphi判断文件类型

    function getFileType(inputFile:string):string;const JPEG_FLAG_BEGIN = $D8FF; JPEG_FLAG_END = $D9FF; ...

  6. PHP防SQL注入不要再用addslashes和mysql_real_escape_string

    PHP防SQL注入不要再用addslashes和mysql_real_escape_string了,有需要的朋友可以参考下. 博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助 ...

  7. 自动备份SQL数据库 并删除指定日期之前的备份文件

    /// <summary>        /// 数据备份        /// </summary>        /// public bool DataBackup(st ...

  8. shell 里的变量 总结

    对于linux shell的使用者来说, 巧妙的应用变量不仅能够快速的解决问题,同时能够获取非常大的乐趣,因为shell的变量内部可以附加一些运算,使得程序非常简洁明了并且功能强大,以下详细介绍一下: ...

  9. Two Pointers Day

    (1)Reverse String 解题思路简单明了,但是要注意时间复杂度问题!!! 代码如下:(声明一个与字符串等长的char数组,然后倒序区字符串中的字符,放入数组即可.) public clas ...

  10. 分支合并git checkout adview git merge adview3

    分支合并 git checkout adview git merge adview3