A simple kernel module

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.