Chinaunix首页 | 论坛 | 博客
  • 博客访问: 793808
  • 博文数量: 118
  • 博客积分: 2067
  • 博客等级: 大尉
  • 技术积分: 1751
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-17 14:27
文章存档

2016年(1)

2013年(1)

2012年(3)

2011年(26)

2010年(47)

2009年(40)

分类: LINUX

2010-01-04 16:46:18

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

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

 
CODE:
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指向的链表上的所有节点上注册的函数。简单地说,如下所示:

 
CODE:
    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链。

 
CODE:
#include
#include
#include
#include
#include
#include
#include
#include
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 0;
}

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这个通知链表上,同时每个节点都注册了一个函数。

 
CODE:
#include
#include
#include
#include
#include
#include
#include
#include
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 0; 
}

static int test_event2(struct notifier_block *this, unsigned long event, void *ptr)
{
        printk("In Event 2: Event Number is %d\n", event);
        return 0; 
}

static int test_event3(struct notifier_block *this, unsigned long event, void *ptr)
{
        printk("In Event 3: Event Number is %d\n", event);
        return 0; 
}

/*
* 事件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 -1; 
        }
        printk("register test_notifier1 completed\n");

        err = register_test_notifier(&test_notifier2);
        if (err)
        {
                printk("register test_notifier2 error\n");
                return -1; 
        }
        printk("register test_notifier2 completed\n");

        err = register_test_notifier(&test_notifier3);
        if (err)
        {
                printk("register test_notifier3 error\n");
                return -1; 
        }
        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通知链中发送消息,让链中的函数运行。

 
CODE:
#include
#include
#include
#include
#include
#include
#include
#include
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(1, 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文件

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

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

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

运行:

 
CODE:
make

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

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

下面是我在自己的机器上面运行得到的结果:

QUOTE:
init_notifier
Begin to register:
register test_notifier1 completed
register test_notifier2 completed
register test_notifier3 completed
Begin to notify:
==============================
In Event 1: Event Number is 1
In Event 2: Event Number is 1
In Event 3: Event Number is 1
==============================

 在 Linux 内核中,各个子系统之间有很强的相互关系,一些被一个子系统生成或者被探测到的事件,很可能是另一个或者多个子系统感兴趣的,也就是说这个事件的获取者必 须能够通知所有对该事件感兴趣的子系统,并且还需要这种通知机制具有一定的通用性。基于这些, Linux 内核引入了“通知链”技术。

1 数据结构:

通知链有四种类型,

  1. 原子通知链( Atomic notifier chains ):通知链元素的回调函数(当事件发生时要执行的函数)只能在中断上下文中运行,不允许阻塞
  2. 可阻塞通知链( Blocking notifier chains ):通知链元素的回调函数在进程上下文中运行,允许阻塞
  3. 原始通知链( Raw notifier chains ):对通知链元素的回调函数没有任何限制,所有锁和保护机制都由调用者维护
  4. SRCU 通知链( SRCU notifier chains ):可阻塞通知链的一种变体

所以对应了四种通知链头结构:

  • struct atomic_notifier_head :原子通知链的链头
  • struct blocking_notifier_head :可阻塞通知链的链头
  • struct raw_notifier_head :原始通知链的链头
  • struct srcu_notifier_head : SRCU 通知链的链头

通知链元素的类型:

  • struct notifier_block :通知链中的元素,记录了当发出通知时,应该执行的操作(即回调函数)

链头中保存着指向元素链表的指针。通知链元素结构则保存着回调函数的类型以及优先级,参见 notifier.h 文件。

2 运作机制

通知链的运作机制包括两个角色:

  1. 被通知者:对某一事件感兴趣一方。定义了当事件发生时,相应的处理函数,即回调函数。但需要事先将其注册到通知链中(被通知者注册的动作就是在通知链中增加一项)。
  2. 通知者:事件的通知者。当检测到某事件,或者本身产生事件时,通知所有对该事件感兴趣的一方事件发生。他定义了一个通知链,其中保存了每一个被通知者对事件的处理函数(回调函数)。通知这个过程实际上就是遍历通知链中的每一项,然后调用相应的事件处理函数。

包括以下过程:

  1. 通知者定义通知链
  2. 被通知者向通知链中注册回调函数
  3. 当事件发生时,通知者发出通知(执行通知链中所有元素的回调函数)

被通知者调用 notifier_chain_register 函数注册回调函数,该函数按照优先级将回调函数加入到通知链中 。注销回调函数则使用 notifier_chain_unregister 函数,即将回调函数从通知链中删除。2.2.1节讲述的4种通知链各有相应的注册和注销函数,但是他们最终都是调用上述两个函数完成注册和注销功能的。有 兴趣的读者可以自行查阅内核代码。

通知者调用 notifier_call_chain 函数通知事件的到达,这个函数会遍历通知链中所有的元素,然后依次调用每一个的回调函数(即完成通知动作)。2.2.1节讲述的4种通知链也都有其对应的 通知函数,这些函数也都是最终调用 notifier_call_chain 函数完成事件的通知。

由以上的叙述,“通知链”技术可以概括为:事件的被通知者将事件发生时应该执行的操作通过函数指针方式保存在链表(通知链)中,然后当事件发生时通知者依次执行链表中每一个元素的回调函数完成通知。

阅读(899) | 评论(0) | 转发(0) |
0

上一篇:qemu调试linux内核!

下一篇:深入理解cache

给主人留下些什么吧!~~