linux内核基础--通知链



/******************************init.c**********************/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/notifier.h>


//初始化通知链头
RAW_NOTIFIER_HEAD(hellochain); 


//用此函数来通知事件
int hello_raw_notifiy_call(unsigned long val, void *v)
{
    return raw_notifier_call_chain(&hellochain,val,v);
}
EXPORT_SYMBOL(hello_raw_notifiy_call);//导出函数


//注册接收事件的函数
int hello_register_notfiy_raw(struct notifier_block *n)
{
    return raw_notifier_chain_register(&hellochain,n);
}
EXPORT_SYMBOL(hello_register_notfiy_raw);//导出函数


//取消注册的接收事件函数
int hello_raw_notifier_chain_unregister(struct notifier_block *n)
{
    return raw_notifier_chain_unregister(&hellochain,n);
}
EXPORT_SYMBOL(hello_raw_notifier_chain_unregister);//导出函数


static int __init  hello_init(void)
{
   printk("init hello chain\n");
   return 0;
}


static void __exit hello_exit(void)
{
   printk("remove hello chain\n");
   return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");




/****************************************************notify1.c****************************/
//用来接收事件的模块
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/notifier.h>


#define HELLO_EVENT 0x62U
extern int hello_register_notfiy_raw(struct notifier_block *n);
extern int hello_raw_notifiy_call(unsigned long val, void *v);
extern int hello_raw_notifier_chain_unregister(struct notifier_block *n);


//对接收到的消息进行处理
static int hello_receive_call(struct notifier_block *nb, unsigned long event, void *v)
{
    switch(event)
    {
        case HELLO_EVENT:
             printk("notify1 receive a event\n");
             break;
        default:
             break;
    }
    return 0;
}




//接收消息的函数
static struct notifier_block nb={
    .notifier_call = hello_receive_call
};


//模块初始化,挂载一个消息接收体
static int __init notify1_init(void)
{
     hello_register_notfiy_raw(&nb);


     return 0;
}




static void __exit notify1_exit(void)
{
    hello_raw_notifier_chain_unregister(&nb);
    return;
}
module_init(notify1_init);
module_exit(notify1_exit);


MODULE_LICENSE("GPL");




/******************************************notify2.c**************************/
//用来发送事件的模块
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/notifier.h>


#define HELLO_EVENT 0x62U
extern int hello_register_notfiy_raw(struct notifier_block *n);
extern int hello_raw_notifiy_call(unsigned long val, void *v);




static int __init notify2_init(void)
{
     //向其他模块发送消息
     hello_raw_notifiy_call(TESTCHAIN_EVENT,"not_use");
     return 0;
}


static void __exit notify2_exit(void)
{
    return;
}


module_init(notify2_init);
module_exit(notify2_exit);


MODULE_LICENSE("GPL");




/**********************************************************Makefile************************/

PWD = $(shell pwd)
VERSION = $(shell uname -r)
KPATH = /lib/modules/$(VERSION)/build
obj-m := init.o notify1.o notify2.o

default:
        make -C  $(KPATH) SUBDIRS=$(PWD) modules                
clean:
        make -C  $(KPATH) SUBDIRS=$(PWD) clean


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值