调用内核hlist编写的测试代码
2013-04-18 15:09
211 查看
最近在做linux内核编程,最深的一点感触是:算法的巧妙,犹如看一场高明杂技
真正的程序员那是键盘上灵动的舞者。
写面写的是一个调用内核的hlist测试代码。
/***how to program use the hlist in kernel**/#include<linux/module.h>#include<linux/init.h> /* kind of macros */#include<linux/kernel.h>#include<linux/types.h>#include<linux/list.h>#include<linux/slab.h>unsigned int len_hash = 100;struct hlist_head* hash[100];void get_random_bytes(void *buf,int nbytes);/* define the data strc */typedef struct _data_node{unsigned int data;struct hlist_node node;} data_node_strc;/* init the hash-chain */void init_hash(void){unsigned int i=0;for(i=0;i<len_hash;i++){hash[i] = (struct hlist_head*)kmalloc(sizeof(struct hlist_head),0);memset(hash[i],0,sizeof(struct hlist_head));}}/* get a random numer */unsigned int rand(unsigned int lower,unsigned int up){unsigned int rand = 0;get_random_bytes(&rand,sizeof(unsigned int ));return (rand%(up-lower)+lower);}/* alloc the memory */data_node_strc* kalloc_node(void){data_node_strc* tmp = (data_node_strc*)kmalloc(sizeof(data_node_strc),0);memset(tmp,0,sizeof(data_node_strc));return tmp;}/* full the hash-chain randomly */void full_hash(void){int i = 0;for(i=0;i<100;i++){unsigned int key = rand(0,100);data_node_strc* p_node = kalloc_node();p_node->data = key;hlist_add_head(&(p_node->node),hash[key]);}}void print_hash(void){unsigned int i = 0;for(i=0;i<len_hash;i++){if(!hlist_empty(hash[i])){data_node_strc * tmp = NULL;struct hlist_node *tmp_node = NULL;int j=0;hlist_for_each_entry(tmp,tmp_node,hash[i],node){printk("(key=%d,node=%d)->data=%d\n",i,j,tmp->data);j++;}}}}void free_hash(void){int i = 0;for(i=0;i<len_hash;i++){if(!hlist_empty(hash[i])){data_node_strc * tmp = NULL;struct hlist_node * tmp_node = NULL ;hlist_for_each_entry(tmp,tmp_node,hash[i],node){tmp->node.next = NULL;tmp->node.pprev = NULL;kfree(tmp);}hash[i]->first = NULL;}kfree(hash[i]);hash[i] = NULL;}}/* init the module */static int __init hlist_init(void){init_hash();full_hash();print_hash();return 0;}/* exit the module */static void __exit hlist_exit(void){free_hash();}/* call the macro module_init() to init the module*/module_init(hlist_init);/* call the macro module_exit() to init the module*/module_exit(hlist_exit);/* obey the GPL poc */MODULE_LICENSE("GPL");MODULE_AUTHOR("dead_angel");MODULE_DESCRIPTION("timer_test");
相关文章推荐
- 动画内核核心与调用接口(代码编写)
- 【百度分享】基于内核模块的测试代码编写(一)
- 【百度分享】基于内核模块的测试代码编写(二)
- 基于内核模块的测试代码编写
- 【百度分享】基于内核模块的测试代码编写(三)
- 开始编写Cocosd-x对Java进行调用的测试代码
- js代码调用c++代码的测试
- 编写Android工程里测试代码的步骤
- 编写S-Function,创建Simulink模型并生成C++代码,编译生成可调用的动态库dll(From Simulink model to DLL A tutorial)
- 02_JNI中Java代码调用C代码,Android中使用log库打印日志,javah命令的使用,Android.mk文件的编写,交叉编译
- Ubuntu14.10编译安装3.17.4内核及系统调用测试
- Linux编写新的系统调用(内核版本3.2.18)
- 编写存储过程,并且使用java代码调用存储过程的简单例子
- I2C的内核驱动测试代码
- 一起谈.NET技术,VS2010 测试功能之旅:编码的UI测试(4)-通过编写测试代码的方式建立UI测试(上)
- 意外作出了一个javascript的服务器,可以通过js调用并执行任何java(包括 所有java 内核基本库)及C#类库,并最终由 C# 执行你提交的javascript代码! 不敢藏私,特与大家分
- 测试代码的编写
- 在Jmeter中使用自定义编写的Java测试代码
- Linux内核分析:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- 使用eclipse调用天气预报的测试代码WSDL