书接上回,闲话不表。话说,女神无论是在土豪或者屌丝那里都找不到归属感,冥冥之中天上掉下来一个王子(PS:又名高富帅),既可以满足女神的物质需求还可以满足女神的精神需求:

点击(此处)折叠或打开

/*GFS.c*/

#include

#include

#include

#include

#include

#include

#include

#include

MODULE_LICENSE("GPL");

/*

* 注册通知链

*/

extern int register_godness_notifier(struct notifier_block*);

extern int unregister_godness_notifier(struct notifier_block*);

static int sweet_heart_requirments(struct notifier_block *this, unsigned long event, void *ptr)

{

switch(event)

{

case 0:

printk("[GFS]Hi honey,the VISA card is ready for you! \n");

break;

case 1:

printk("[GFS]Hi honey,let me play the piano for you! \n");

break;

default:

break;

}

return 0;

}

static struct notifier_block honey_notifier =

{

.notifier_call = sweet_heart_requirments,

.priority = 2,

};

static int __init GFS_register(void)

{

int err;

printk("[GFS]GFS register honey_requirment response to Godness...");

err = register_godness_notifier(&honey_notifier);

if (err)

{

printk("Refused!\n");

return -1;

}

printk("Accepted!\n");

return err;

}

/*

* 卸载刚刚注册了的通知链

*/

static void __exit GFS_unregister(void)

{

unregister_godness_notifier(&honey_notifier);

printk("[GFS]GFS broke up with Godness!(How sadness)\n");

}

module_init(GFS_register);

module_exit(GFS_unregister);

然后重新上演前面的故事,播放的指令如下:

[root@localhost test]# insmod Goddess.ko;sleep 4;insmod Tuhao.ko; sleep 4; insmod Diors.ko;sleep 4; insmod GFS.ko

[root@localhost test]# rmmod GFS.ko;rmmod Diors.ko;rmmod Tuhao.ko;rmmod Goddess.ko

[root@localhost test]#

播放内容如下:

Jun  2 00:39:41 localhost kernel: [Attention]The Godness coming into the world!

Jun  2 00:39:41 localhost kernel: [Godness]requirements thread starting...

Jun  2 00:39:44 localhost kernel: [Godness]requirment type: 1

Jun  2 00:39:45 localhost kernel: [Tuhao]Tuhao register Cash event  to Godness...Accepted!

Jun  2 00:39:47 localhost kernel: [Godness]requirment type: 0

Jun  2 00:39:47 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:39:49 localhost kernel: [Diors]Diors register music_requirment response to Godness...Accepted!

Jun  2 00:39:50 localhost kernel: [Godness]requirment type: 1

Jun  2 00:39:50 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:39:53 localhost kernel: [Godness]requirment type: 1

Jun  2 00:39:53 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:39:53 localhost kernel: [GFS]GFS register honey_requirment response to Godness...Accepted!

Jun  2 00:39:56 localhost kernel: [Godness]requirment type: 1

Jun  2 00:39:56 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:39:56 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:39:59 localhost kernel: [Godness]requirment type: 0

Jun  2 00:39:59 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:39:59 localhost kernel: [GFS]Hi honey,the VISA card is ready for you!

Jun  2 00:40:02 localhost kernel: [Godness]requirment type: 1

Jun  2 00:40:02 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:40:02 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:40:05 localhost kernel: [Godness]requirment type: 1

Jun  2 00:40:05 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:40:05 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:40:08 localhost kernel: [Godness]requirment type: 0

Jun  2 00:40:08 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:40:08 localhost kernel: [GFS]Hi honey,the VISA card is ready for you!

Jun  2 00:40:11 localhost kernel: [Godness]requirment type: 1

Jun  2 00:40:11 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:40:11 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:40:11 localhost kernel: [Godness]requirements thread ended!

Jun  2 00:40:52 localhost kernel: [GFS]GFS broke up with Godness!(How sadness)

Jun  2 00:40:52 localhost kernel: [Diors]Tuhao is giving up Godness!(What a pity)

Jun  2 00:40:52 localhost kernel: [Tuhao]Tuhao is giving up Godness!(Son of bitch)

Jun  2 00:40:52 localhost kernel: [Attention] The Godness leaving out!

我们可以看到,高富帅向女神需求通知链上注册的回调函数,优先级也是2,我们在测试的时候是先加载土豪、再加载屌丝最后加载高富帅,所以每当女神提出需求时,如果是金钱需求则最先被土豪响应,如果是精神需求则最先被屌丝给响应了。这完全不符合高富帅的特征啊,于是高富帅将自己的优先级提高到5,然后replay一次:

点击(此处)折叠或打开

static struct notifier_block honey_notifier =

{

.notifier_call = sweet_heart_requirments,

.priority = 5,

};

结果如下:

Jun  2 00:50:29 localhost kernel: [Attention]The Godness coming into the world!

Jun  2 00:50:29 localhost kernel: [Godness]requirements thread starting...

Jun  2 00:50:32 localhost kernel: [Godness]requirment type: 0

Jun  2 00:50:33 localhost kernel: [Tuhao]Tuhao register Cash event  to Godness...Accepted!

Jun  2 00:50:35 localhost kernel: [Godness]requirment type: 0

Jun  2 00:50:35 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:50:37 localhost kernel: [Diors]Diors register music_requirment response to Godness...Accepted!

Jun  2 00:50:38 localhost kernel: [Godness]requirment type: 1

Jun  2 00:50:38 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:50:41 localhost kernel: [Godness]requirment type: 0

Jun  2 00:50:41 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:50:41 localhost kernel: [GFS]GFS register honey_requirment response to Godness...Accepted!

Jun  2 00:50:44 localhost kernel: [Godness]requirment type: 0

Jun  2 00:50:44 localhost kernel: [GFS]Hi honey,the VISA card is ready for you!

Jun  2 00:50:44 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:50:47 localhost kernel: [Godness]requirment type: 1

Jun  2 00:50:47 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:50:47 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:50:50 localhost kernel: [Godness]requirment type: 1

Jun  2 00:50:50 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:50:50 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:50:53 localhost kernel: [Godness]requirment type: 0

Jun  2 00:50:53 localhost kernel: [GFS]Hi honey,the VISA card is ready for you!

Jun  2 00:50:53 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:50:56 localhost kernel: [Godness]requirment type: 0

Jun  2 00:50:56 localhost kernel: [GFS]Hi honey,the VISA card is ready for you!

Jun  2 00:50:56 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:50:59 localhost kernel: [Godness]requirment type: 1

Jun  2 00:50:59 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:50:59 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:50:59 localhost kernel: [Godness]requirements thread ended!

Jun  2 00:51:04 localhost kernel: [GFS]GFS broke up with Godness!(How sadness)

Jun  2 00:51:04 localhost kernel: [Diors]Tuhao is giving up Godness!(What a pity)

Jun  2 00:51:04 localhost kernel: [Tuhao]Tuhao is giving up Godness!(Son of bitch)

Jun  2 00:51:04 localhost kernel: [Attention] The Godness leaving out!

这次我们看到,高富帅终于如愿以偿地于第一时间响应到了女神的需求。再考虑一种情况,如果高富帅不想让女神的需求传递到土豪和屌丝那里,怎么办?其实也很简单,高富帅只要在自己的响应函数里向女神返回NOTIFY_STOP值就可以了:

点击(此处)折叠或打开

static int sweet_heart_requirments(struct notifier_block *this, unsigned long event, void *ptr)

{

switch(event)

{

case 0:

printk("[GFS]Hi honey,the VISA card is ready for you! \n");

break;

case 1:

printk("[GFS]Hi honey,let me play the piano for you! \n");

break;

default:

break;

}

return NOTIFY_STOP;

}

然后故事就变成酱紫了:

Jun  2 00:55:56 localhost kernel: [Attention]The Godness coming into the world!

Jun  2 00:55:56 localhost kernel: [Godness]requirements thread starting...

Jun  2 00:55:59 localhost kernel: [Godness]requirment type: 1

Jun  2 00:56:00 localhost kernel: [Tuhao]Tuhao register Cash event  to Godness...Accepted!

Jun  2 00:56:02 localhost kernel: [Godness]requirment type: 0

Jun  2 00:56:02 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:56:04 localhost kernel: [Diors]Diors register music_requirment response to Godness...Accepted!

Jun  2 00:56:05 localhost kernel: [Godness]requirment type: 1

Jun  2 00:56:05 localhost kernel: [Diors]Hi girl,This is a classic Music disk,take it.

Jun  2 00:56:08 localhost kernel: [Godness]requirment type: 0

Jun  2 00:56:08 localhost kernel: [Tuhao]Hi Baby,$$$$$$$$ 么么哒

Jun  2 00:56:08 localhost kernel: [GFS]GFS register honey_requirment response to Godness...Accepted!

Jun  2 00:56:11 localhost kernel: [Godness]requirment type: 1

Jun  2 00:56:11 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:56:14 localhost kernel: [Godness]requirment type: 0

Jun  2 00:56:14 localhost kernel: [GFS]Hi honey,the VISA card is ready for you!

Jun  2 00:56:17 localhost kernel: [Godness]requirment type: 1

Jun  2 00:56:17 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:56:20 localhost kernel: [Godness]requirment type: 1

Jun  2 00:56:20 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:56:23 localhost kernel: [Godness]requirment type: 0

Jun  2 00:56:23 localhost kernel: [GFS]Hi honey,the VISA card is ready for you!

Jun  2 00:56:26 localhost kernel: [Godness]requirment type: 1

Jun  2 00:56:26 localhost kernel: [GFS]Hi honey,let me play the piano for you!

Jun  2 00:56:26 localhost kernel: [Godness]requirements thread ended!

Jun  2 00:56:30 localhost kernel: [GFS]GFS broke up with Godness!(How sadness)

Jun  2 00:56:30 localhost kernel: [Diors]Tuhao is giving up Godness!(What a pity)

Jun  2 00:56:30 localhost kernel: [Tuhao]Tuhao is giving up Godness!(Son of bitch)

Jun  2 00:56:30 localhost kernel: [Attention] The Godness leaving out!

在高富帅还没将自己的响应函数注册到女神之前,女神的物质需求是被土豪给霸占了,精神需求被屌丝给享有了,等到00:56:08秒之后,高富帅的请求被女神接受了,于是接下来就没土豪和屌丝啥事儿了。OK,到这里我们的故事也就讲完了,让我们回头来总结一下Linux内核中通知链的相关知识点(首先非常感谢wwx0715兄弟指出了我【上】篇博文的一个错误,在【上】篇博文里我将通知链的优先级弄反了,是数字越大优先级越高,通知函数越是先会被执行,在此非常感谢。):

1、如果一个子系统A在运行过程中会产生一个实时的事件,而这些事件对其他子系统来说非常重要,那么系统A可以定义一个自己的通知链对象,根据需求可以选择原子通知链、非阻塞通知链或者原始通知链,并向外提供向这个通知链里注册、卸载执行事件的回调函数的接口;

2、如果子系统B对子系统A中的某(些)个事件感兴趣,或者说强依赖,就是说系统B需要根据系统A中某些事件来执行自己特定的操作,那么此时系统就需要实例化一个通知块struct notifier_block XXX{},然后编写通知块里的回调处理函数来响应A系统中响应的事件就可以了;

3、在我们这个示例里用到了struct notifier_block{}的优先级特性,其实在标准内核里每个实例化的通知块都没有用优先级字段。不用优先级字段的结果就是,先注册的通知块里的回调函数在事件发生时会先执行。注意这里所说的后注册指的是模块被动态加载内核的先后顺序,和哪个模块的代码先写完没有关系,注意区分。意思就是说,如果子系统B和C都对子系统A的up事件感兴趣,B和C在向A注册up事件的回调函数时并没有指定函数的优先级。无论是通过insmod手动加载模块B和C,还是系统boot时自动加载B和C,哪个模块先被加载,它的回调函数在A系统的up事件发生时会先被执行;

4、关于通知链的回调函数,正常情况下都需要返回NOTIFY_OK或者NOTIFY_DONE,这样通知链上后面挂载的其他函数可以继续执行。如果返回NOTIFY_STOP,则会使得本通知链上后续挂载的函数无法得到执行,除非你特别想这么做,否则在编写通知链的回调函数时一般最好不要返回这个值;

5、通知链上回调函数的原型int (*notifier_call)(struct notifier_block *, unsigned long, void *),其中第二个参数一般用于指明事件的类型,通常都是一个整数。而第三个参数是一个void类型的内存地址,在不同的子系统中用于表示不同的信息,例如在邻居子系统里,第三个参数可以是一个邻居信息结构体对象的地址struct neighbour *v;而在驱动框架里又可以是一个net_device{}结构体对象的地址等等;我们在设计自己的通知链系统可以用第三个入参实现在通知系统与被通知系统之间实现数据的传递,以便被通知系统的工作可以更加紧凑、高效;

6、如果以后再看到内核中某个子系统在调用通知链的注册函数时,不要心虚、不要怕。做到以下两点就没事儿了:

第一:心里首先要明确,这个注册通知链回调函数的系统一定和提供通知链的系统有某种连续,且本系统需要对那个系统的某些重要事件进行响应;

第二:看本系统注册的通知回调函数的实现,具体看它对那些事件感兴趣,并且是怎么处理的;

第三:看看原提供通知链对象的系统有哪些事件;

最后,心里也就明白了这个系统为什么要用通知链来感知别的系统的变化了,这样一来,对这两个系统从宏观到甚微观的层面就都有一个总体的认识和把握,后续再研究起来就顺风顺水了,而这也正是内核通知链的神奇所在。

完。

linux 内核回调,Linux 内核通知链随笔【下】相关推荐

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

        关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不同子系统之间的通信,通知链只能用于内核不同子系统之间的通信.那么内核通知链到底是怎么工作的?我们如何才能用好 ...

  2. linux区块链环境搭建,区块链--ubuntu下环境搭建和使用

    一.ubuntu设置root登录 通过sudo passwd root给root设置密码 通过su root切换到root用户 进入/usr/share/lightdm/lightdm.conf.d目 ...

  3. linux 内核通知,[Linux] 内核通知链 notifier

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

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

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

  5. Linux 内核通知链和例程代码

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

  6. Linux内核基础--事件通知链(notifier chain)【转】

    转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...

  7. Linux内核通知链(notifier chain)

    1.概述 Linux内核中各个子系统相互依赖,当其中某个子系统状态发生改变时,就必须使用一定的机制告知使用其服务的其他子系统,以便其他子系统采取相应的措施.为满足这样的需求,内核实现了事件通知链机制( ...

  8. Linux内核源码——通知链(notifier chain)

    写在前面的话: 刚入手Linux驱动的时候,各种Linux内核的机制是我们需要搬起的一块大砖块.搬砖的过程中,我发现自己不能很好的理解相关的业务驱动的很大的一个原因就是对内核的基本机制掌握不太准确.刚 ...

  9. linux c 网络事件 通知,深入理解Linux网络技术内幕—通知链

    内核的很多子系统之间具有很强的相互依赖关系,其中一个子系统发现的或产生的事件,其他子系统可能都有兴趣.为了实现这种交互需求,Linux使用了通知链(Notification Chain)机制. 通知链 ...

  10. 深入理解Linux网路技术内幕学习笔记第四章:通知链

    第四章:通知链 内核很多子系统之间具有很强的依赖性,其中一个子系统侦测到的或者产生的事件,其他子系统可能都感兴趣,为了实现这种需求,Linux使用了通知链.通知链只在内核子系统之间使用. 通知链就是一 ...

最新文章

  1. C Primer+Plus(十一)
  2. apache 不解释php,apache-2.2 – Apache不解释.PHP文件
  3. 数据结构(字典,跳跃表)、使用场景(计数器、缓存、查找表、消息队列、会话缓存、分布式锁)、Redis 与 Memcached、 键的过期时间、数据淘汰策略、持久化(RDB、AOF)
  4. CF1654F-Minimal String Xoration【倍增】
  5. struts2值栈,OGNL表达式,interceptor
  6. c++将.cpp编译为.so文件
  7. Qomo OpenProject Field Test 1发布!
  8. AcWing 291. 蒙德里安的梦想(状态压缩DP)
  9. go grpc 异步_dubbogo 3.0:牵手 gRPC 走向云原生时代
  10. Three.js案例分析系列1--webgl_animation_cloth 草坪上漂浮的白布
  11. 【解决】当前操作环境不支持支付宝控件/一直处于“正在安装证书“中
  12. 如何利用ORIGIN拟合曲线
  13. 测试用户名字 陈一王二张三李四钱五赵六钱七张八周九吴十
  14. d3_0330_打造小米商城官网 Html+css+JS练手项目实战
  15. \t\t林荫苗圃 苗木和苗圃 好苗木种植技术是关键 它好我也好
  16. 计算机毕业设计(69)php小程序毕设作品之疫苗预约小程序系统
  17. 如何找到计算机上的画图拦,机子里的画图和计算机没有了
  18. gallery3d源码学习总结(一)——绘制流程drawFocusItems
  19. 店铺降权的原因|盛天海科技
  20. python 求向量模长(一范二范)

热门文章

  1. 日历本(给出年份生成日历)
  2. 阿里资深数据分析师回答那些关于数据分析师的最常见的几个问题
  3. 在rhel7.3中编译和使用log4cxx
  4. 智能abc是什么输入法:win10可用的智能abc输入法免费下载
  5. CentOS Mac 安装zsh,使用oh-my-zsh
  6. RPM 包的构建 - SPEC 基础知识
  7. 矩阵的分解:满秩分解和奇异值分解
  8. 乒乓球侧旋球MATLAB,乒乓球的侧拐球、侧旋球和侧弧圈辨析
  9. 计算机无纸化考试知识点,计算机二级VFP无纸化考试重点和难点
  10. B端硬件产品需求评审