内核通知链例子
来源:互联网 发布:sql having用法 编辑:程序博客网 时间:2024/05/19 20:45
如果模块A要发信息给模块B,可采用内核通知链的方式,这里提供最简单的例子。
notifier_chain.c
#include <linux/module.h>#include <linux/notifier.h>#include <linux/export.h>static BLOCKING_NOTIFIER_HEAD(test_chain_head);int register_test_notifier(struct notifier_block *nb){ return blocking_notifier_chain_register(&test_chain_head, nb);}EXPORT_SYMBOL(register_test_notifier);int unregister_test_notifier(struct notifier_block *nb){ return blocking_notifier_chain_unregister(&test_chain_head, nb);}EXPORT_SYMBOL(unregister_test_notifier);int test_notifier_call_chain(unsigned long val){ int ret; ret = blocking_notifier_call_chain(&test_chain_head, val, NULL); return notifier_to_errno(ret);}EXPORT_SYMBOL(test_notifier_call_chain);MODULE_LICENSE("Dual BSD/GPL");
notifier_chain.h
#ifndef _NOTIFIER_CHAIN_H_#define _NOTIFIER_CHAIN_H_extern int register_test_notifier(struct notifier_block *nb);extern int unregister_test_notifier(struct notifier_block *nb);extern int test_notifier_call_chain(unsigned long val);#endif
发信息producer.c
#include <linux/module.h>#include <linux/delay.h>#include "notifier_chain.h"static int producer_init(void) { printk(KERN_INFO "%s\n",__FUNCTION__);test_notifier_call_chain(1);msleep(1000);test_notifier_call_chain(1000);return 0; } static void producer_exit(void) { printk(KERN_INFO "%s\n",__FUNCTION__);} module_init(producer_init); module_exit(producer_exit); MODULE_LICENSE("Dual BSD/GPL");
接收信息consumer.c
#include <linux/module.h>#include "notifier_chain.h"static int test_notify(struct notifier_block *self, unsigned long action, void *dev){switch (action) {case 1:printk("action=%ld\n",action);break;case 1000:printk("action=%ld\n",action);break;}return NOTIFY_OK;}static struct notifier_block test_nb = {.notifier_call = test_notify,};static int consumer_init(void) { printk(KERN_INFO "%s\n",__FUNCTION__);register_test_notifier(&test_nb);return 0;}static void consumer_exit(void) { printk(KERN_INFO "%s\n",__FUNCTION__);unregister_test_notifier(&test_nb);} module_init(consumer_init); module_exit(consumer_exit); MODULE_LICENSE("Dual BSD/GPL");
相应的Makefile为
ifeq ($(KERNELRELEASE),)KERNELDIR_OUT ?= /home/w/M8974AAAAANLYD4275/out/target/product/msm8974/obj/KERNEL_OBJDBG_CROSS_COMPILE ?= /home/w/M8974AAAAANLYD4275/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-PWD :=$(shell pwd)modules:$(MAKE) -C $(KERNELDIR_OUT) M=$(PWD) ARCH=arm CROSS_COMPILE=$(DBG_CROSS_COMPILE) modulesmodules_install:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order Module.symvers app.PHONY: modules modules_install cleanelseobj-m+= notifier_chain.oobj-m+= consumer.oobj-m+= producer.oendif
先insmod notifier_chain.ko,consumer.ko,再insmod producer.ko,就可从打印信息中看到producer发给consumer的信息了。
0 0
- 内核通知链例子
- 内核异步通知的例子
- linux内核事件通知链文章链接(概述+例子)
- 内核通知链
- 内核通知链
- 内核中的通知链
- 内核通知链
- 内核通知链
- linux 内核通知链
- 内核通知链分析
- 内核通知链
- Linux内核通知链
- 内核通知链
- 内核通知链-notifier_block
- 内核通知链 学习笔记
- 内核通知链 学习笔记
- Linux内核通知链notifier
- 内核通知链 学习笔记
- Camera 预览之SurfaceView、TextureView、GLSurfaceView(一)
- 某天想做某事于是...
- 获取手机IP地址以及获取手机号码+运营商
- require import 区别
- 一个功能齐全的DataGrid分页例子
- 内核通知链例子
- web开发 java如何连接数据库并取得数据,实现 增,删,改,查
- 帖子内容页分类信息字段模板万能调用标记$_G['forum_option']
- XTU算法专题个人赛3 (DP专题)解题报告
- C++ 基本语法
- 把图象文件转换成XML格式文件
- 第一部分、十道海量数据处理面试题
- 数字证书文件格式(cer和pfx)的区别
- 某天想做某事于是...