本文转载自:http://bbs.chinaunix.net/thread-2011776-1-1.html

1.通知链表简介
    大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣。为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子系统,Linux内核提供了通知链的机制。通知链表只能够在内核的子系统之间使用,而不能够在内核与用户空间之间进行事件的通知。
    通知链表是一个函数链表,链表上的每一个节点都注册了一个函数。当某个事情发生时,链表上所有节点对应的函数就会被执行。所以对于通知链表来说有一个通知方与一个接收方。在通知这个事件时所运行的函数由被通知方决定,实际上也即是被通知方注册了某个函数,在发生某个事件时这些函数就得到执行。其实和系统调用signal的思想差不多。

2.通知链表数据结构
    通知链表的节点类型为notifier_block,其定义如下:

 struct notifier_block
{
int (*notifier_call)(struct notifier_block *self, unsigned long, void *);
struct notifier_block *next;
int priority;
};

其中最重要的就是notifier_call这个函数指针,表示了这个节点所对应的要运行的那个函数。next指向下一个节点,即当前事件发生时还要继续执行的那些节点。

3.注册通知链
    在通知链注册时,需要有一个链表头,它指向这个通知链表的第一个元素。这样,之后的事件对该链表通知时就会根据这个链表头而找到这个链表中所有的元素。
    注册的函数是:
int notifier_chain_register(struct notifier_block **nl, struct notifier_block *n)
    也即是将新的节点n加入到nl所指向的链表中去。
    卸载的函数是:
int notifier_chain_unregister(strut notifier_block **nl, struct notifier_block *n)
    也即是将节点n从nl所指向的链表中删除。

4.通知链表
    当有事件发生时,就使用notifier_call_chain向某个通知链表发送消息。
int notifier_call_chain(struct notifier_block **nl, unsigned long val, void *v)
    这个函数是按顺序运行nl指向的链表上的所有节点上注册的函数。简单地说,如下所示:

     struct notifier_block *nb = *n;

     while (nb)
{
ret = nb->notifier_call(nb, val, v);
if (ret & NOTIFY_STOP_MASK)
{
return ret;
}
nb = nb->next;
}

5.示例
    在这里,写了一个简单的通知链表的代码。

实际上,整个通知链的编写也就两个过程:
    首先是定义自己的通知链的头节点,并将要执行的函数注册到自己的通知链中。
    其次则是由另外的子系统来通知这个链,让其上面注册的函数运行。

我这里将第一个过程分成了两步来写,第一步是定义了头节点和一些自定义的注册函数(针对该头节点的),第二步则是使用自定义的注册函数注册了一些通知链节点。分别在代码buildchain.c与regchain.c中。
    发送通知信息的代码为notify.c。

代码1 buildchain.c
    它的作用是自定义一个通知链表test_chain,然后再自定义两个函数分别向这个通知链中加入或删除节点,最后再定义一个函数通知这个test_chain链。

 #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>
MODULE_LICENSE("GPL"); /*
* 定义自己的通知链头结点以及注册和卸载通知链的外包函数
*/ /*
* RAW_NOTIFIER_HEAD是定义一个通知链的头部结点,
* 通过这个头部结点可以找到这个链中的其它所有的notifier_block
*/ static RAW_NOTIFIER_HEAD(test_chain); /*
* 自定义的注册函数,将notifier_block节点加到刚刚定义的test_chain这个链表中来
* raw_notifier_chain_register会调用notifier_chain_register
*/ int register_test_notifier(struct notifier_block *nb)
{
return raw_notifier_chain_register(&test_chain, nb);
}
EXPORT_SYMBOL(register_test_notifier); int unregister_test_notifier(struct notifier_block *nb)
{
return raw_notifier_chain_unregister(&test_chain, nb);
}
EXPORT_SYMBOL(unregister_test_notifier); /*
* 自定义的通知链表的函数,即通知test_chain指向的链表中的所有节点执行相应的函数
*/ int test_notifier_call_chain(unsigned long val, void *v)
{
return raw_notifier_call_chain(&test_chain, val, v);
}
EXPORT_SYMBOL(test_notifier_call_chain); /*
* init and exit
*/ static int __init init_notifier(void)
{
printk("init_notifier\n");
return ;
} static void __exit exit_notifier(void)
{
printk("exit_notifier\n");
}
module_init(init_notifier);
module_exit(exit_notifier);

代码2 regchain.c
    该代码的作用是将test_notifier1 test_notifier2 test_notifier3这三个节点加到之前定义的test_chain这个通知链表上,同时每个节点都注册了一个函数。

 #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>
MODULE_LICENSE("GPL"); /*
* 注册通知链
*/ extern int register_test_notifier(struct notifier_block*); extern int unregister_test_notifier(struct notifier_block*); static int test_event1(struct notifier_block *this, unsigned long event, void *ptr)
{
printk("In Event 1: Event Number is %d\n", event);
return ;
} static int test_event2(struct notifier_block *this, unsigned long event, void *ptr)
{
printk("In Event 2: Event Number is %d\n", event);
return ;
} static int test_event3(struct notifier_block *this, unsigned long event, void *ptr)
{
printk("In Event 3: Event Number is %d\n", event);
return ;
} /*
* 事件1,该节点执行的函数为test_event1
*/ static struct notifier_block test_notifier1 =
{
.notifier_call = test_event1,
}; /*
* 事件2,该节点执行的函数为test_event1
*/ static struct notifier_block test_notifier2 =
{
.notifier_call = test_event2,
}; /*
* 事件3,该节点执行的函数为test_event1
*/ static struct notifier_block test_notifier3 =
{
.notifier_call = test_event3,
}; /*
* 对这些事件进行注册
*/ static int __init reg_notifier(void)
{
int err;
printk("Begin to register:\n"); err = register_test_notifier(&test_notifier1);
if (err)
{
printk("register test_notifier1 error\n");
return -;
}
printk("register test_notifier1 completed\n"); err = register_test_notifier(&test_notifier2);
if (err)
{
printk("register test_notifier2 error\n");
return -;
}
printk("register test_notifier2 completed\n"); err = register_test_notifier(&test_notifier3);
if (err)
{
printk("register test_notifier3 error\n");
return -;
}
printk("register test_notifier3 completed\n");
return err;
} /*
* 卸载刚刚注册了的通知链
*/ static void __exit unreg_notifier(void)
{
printk("Begin to unregister\n");
unregister_test_notifier(&test_notifier1);
unregister_test_notifier(&test_notifier2);
unregister_test_notifier(&test_notifier3);
printk("Unregister finished\n");
}
module_init(reg_notifier);
module_exit(unreg_notifier);

代码3 notify.c
    该代码的作用就是向test_chain通知链中发送消息,让链中的函数运行。

 #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>
MODULE_LICENSE("GPL"); extern int test_notifier_call_chain(unsigned long val, void *v); /*
* 向通知链发送消息以触发注册了的函数
*/ static int __init call_notifier(void)
{
int err;
printk("Begin to notify:\n"); /*
* 调用自定义的函数,向test_chain链发送消息
*/ printk("==============================\n");
err = test_notifier_call_chain(, NULL);
printk("==============================\n");
if (err)
printk("notifier_call_chain error\n");
return err;
} static void __exit uncall_notifier(void)
{
printk("End notify\n");
}
module_init(call_notifier);
module_exit(uncall_notifier);

Makefile文件

 obj-m:=buildchain.o regchain.o notify.o

 KERNELDIR:=/lib/modules/$(shell uname -r)/build

 default:
make -C $(KERNELDIR) M=$(shell pwd) modules

运行:

 make

 insmod buildchain.ko
insmod regchain.ko
insmod notify.ko

结果:

 这样就可以看到通知链运行的效果了

 下面是我在自己的机器上面运行得到的结果:
init_notifier
Begin to register:
register test_notifier1 completed
register test_notifier2 completed
register test_notifier3 completed
Begin to notify:
==============================
In Event : Event Number is
In Event : Event Number is
In Event : Event Number is
==============================

callback机制之内核通知链表【转】的更多相关文章

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

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

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

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

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

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

  4. 深入理解Linux网络技术内幕——Notification内核通知表链

    为什么要有内核通知表链:     Linux由多个相互依赖的子系统组成.其中一些子系统可能需要对其他子系统的一些事件感兴趣.这样子系统之间需要一些通信机制来实现这一功能.     在接触Notific ...

  5. Linux内核通知链模块

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

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

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

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

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

  8. Linux内核【链表】整理笔记(1)

    我们都知道Linux内核里的双向链表和学校里教给我们的那种数据结构还是些不一样.Linux采用了一种更通用的设计,将链表以及其相关操作函数从数据本身进行剥离,这样我们在使用链表的时候就不用自己去实现诸 ...

  9. linux 内核的链表操作(好文不得不转)

    以下全部来自于http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 无任何个人意见. 本文详细分析了 2.6.x 内 ...

随机推荐

  1. hdu 5381 The sum of gcd(线段树+gcd)

    题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...

  2. CentOS下配置iptables防火墙 linux NAT(iptables)配置

    CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...

  3. kill -signal

    1. SIGHUP 启动被终止的进程,可让该PID重新读取配置文件,类似于重启服务 对应的数字为1 9.SIGTERM 以正常的结束进程来终止进程 15.SIGSTOP 暂停一个进程相当于crtl+z

  4. 移动端开发者福利-免费收费api收藏

    一 .api 1.https://www.juhe.cn/ 跟百度api集市差不多,超级赞,做好认证就行了,我有20+认证能用的免费api 2.http://apistore.baidu.com/as ...

  5. java查看工具jmap-windows

    Prints shared object memory maps or heap memory details for a process, core file, or remote debug se ...

  6. 应该知道的Linux技巧(转载)

    这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?>—— Linux用户 ...

  7. 【Python】创建和使用类

    面向对象编程是最有效的软件编写方法之一 创建Dog类 class Dog(): '''一次模拟小狗的简单测试''' def __init__(self,name,age): self.name = n ...

  8. http url转义字符,特殊字符

    空格 - %20 " - %22 # - %23 % - %25 & - %26 ( - %28 ) - %29 + - %2B , - %2C / - %2F : - %3A ; ...

  9. 使用sphinx生成美观的文档

    先上效果图 详情 首先,须要知道什么是restructuredtext.能够理解为类似于markdown的一个东西. 然后 安装.pip install sphinx 进入存放文档的文件夹,在命令行, ...

  10. 自己定义ProgressDialog载入图片

    使用系统载入框 mDialog = new ProgressDialog(this); mDialog.setCancelable(true);//能否够被取消 mDialog.setMessage( ...