#include <linux/kernel.h>//Kernel#include <linux/module.h>//kernel Modules#include <linux/init.h>//module initialization and logoff#include <linux/slab.h>//in-house memory allocation management#include <linux/list.h>//kernel linked list//Structural Bodystructperson{Charname[ -];//name intAge//Age structList_head Listhead;//Linked list Header}; structPerson *ptrperson;//Structural BodystructPerson *tmpperson;//Temp VariablestructList_head person_list;//Linked liststructList_head *pos;//point to the current node of the linked list//Initialization of modulesintPerson_init (void){ intI=0; Init_list_head (&person_list);//Initialize the linked table header pointer//apply 10 struct person size continuous physical memory, and specify the head pointer//kmalloc (memory size, type of memory);Ptrperson=kmalloc (sizeof(structperson) *Ten, Gfp_kernel); memset (Ptrperson,0,sizeof(structperson) *Ten);//initializing the contents of a linked list//loop to assign a value to a list for(;i<Ten; i++) {sprintf (Ptrperson[i].name,"[%d]", i+1);//name Teacherptrperson[i].age=i+1;//Age//list_add (linked list pointer of struct, linked list); //List_add (& (Ptrperson[i].listhead), &person_list); //Adding a new linked list to a linked listList_add_tail (& (Ptrperson[i].listhead),&person_list); Insert a new list into the tail of the list}//test loops read data from a linked listList_for_each (pos,&person_list) { //list_entry (null pointer, struct type, linked header in struct);Tmpperson=list_entry (POS,structperson,listhead); PRINTK ("name:%s,age:%d\n",tmpperson->name,tmpperson->Age ); } return 0;} //Module LogoffvoidPerson_exit (void){ intI=0; for(;i<Ten; i++) {List_del (&(Ptrperson[i].listhead)); PRINTK ("list_del[%d]\n", i+1); } kfree (Ptrperson); PRINTK ("person_exit\n");} Module_init (Person_init); Module_exit (person_exit);//Kernel Module DeclarationModule_license ("GPL"); Module_author ("Edison_ren"); Module_description ("list_module_test"); Module_alias ("list_module_test");
Makefile file
Obj-m: = kperson.o #指定内核模块名称KDIR:=/usr/src/linux-headers-3.13. 0-generic #指定编译的内核源文件路径all: Make-C $ (Kdir) m=$ (PWD) modules # Arch=arm cross_compile=arm-linux-clean: rm -F *.ko *.o *.mod *.symvers *.order
A simple kernel module