初步Linux内核模块编程

基本命令

lsmod                    // 查看当前已加载的模块
insmod testmodule.ko     // 加载testmodule模块
rmmod testmodule         // 卸载testmodule模块
dmesg                    // dmesg用来显示内核环缓冲区内容,内核将各种消息存放在这里

简单实例

模块源代码testmodule1.c如下

// 源码来自《操作系统概念-第九版》
// 《operating system concepts, 9th edition》
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* This function is called when the module is loaded. */
int simple init(void)
{
	printk(KERN INFO "Loading Module\n");
	return 0;
}
/* This function is called when the module is removed. */
void simple exit(void)
{
	printk(KERN INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module init(simple init);
module exit(simple exit);
MODULE LICENSE("GPL");
MODULE DESCRIPTION("Simple Module");
MODULE AUTHOR("SGG");

testmodule1.c的Makefile如下

ifneq ($(KERNELRELEASE),)
	obj-m:=testmodule1.o
else
	PWD:=$(shell pwd)
	KDIR:=/lib/modules/$(shell uname -r)/build
all:
	make -C $(KDIR) M=$(PWD) modules
endif

Linux内核链表操作
《operating system concepts,9th edition》p96习题。
源代码testmodule2.c如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/types.h>
#include <linux/slab.h>

/* This function is called when the module is loaded. */
int simple_init(void)
{
    printk(KERN_INFO "Loading Module\n");
	// declare a list_head object by using LIST_HEAD macro
    static LIST_HEAD(birthday_list);         
    struct birthday
    {
        int day;
        int month;
        int year;
        struct list_head list;
    };
    struct birthday *ptr, *next;
    struct birthday *Bob;
    struct birthday *Alice;
    struct birthday *youngseaz;

    Bob = kmalloc(sizeof(*Bob), GFP_KERNEL);
    Bob -> day = 1;
    Bob -> month = 1;
    Bob -> year = 1990;
	// initializes the list member in struct birthday
    INIT_LIST_HEAD(&Bob -> list);

    Alice = kmalloc(sizeof(*Alice), GFP_KERNEL);
    Alice -> day = 2;
    Alice -> month = 2;
    Alice -> year = 1991;
    INIT_LIST_HEAD(&Alice -> list);
    
    youngseaz = kmalloc(sizeof(*youngseaz), GFP_KERNEL);
    youngseaz -> day = 1;
    youngseaz -> month = 1;
    youngseaz -> year = 2000;
    INIT_LIST_HEAD(&youngseaz -> list);

	//add this instance to the end of the linked list using the list add tail() macro
    list_add_tail(&Bob -> list, &birthday_list);
    list_add_tail(&Alice -> list, &birthday_list);
    list_add_tail(&youngseaz -> list, &birthday_list);

    list_for_each_entry(ptr, &birthday_list, list)
    {

        printk(KERN_INFO "%d/%d/%d \n", ptr -> month, ptr -> day,  ptr -> year);
    }
    
    //removing all elements from a linked list is 
    list_for_each_entry_safe(ptr, next, &birthday_list, list)
    {
	/* Removing elements from the list involves using the list_del() macro,
	which is passed a pointer to struct list head */
        list_del(&ptr->list);
        kfree(ptr);
    }

       return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) 
{
    printk(KERN_INFO "Removing Module\n");
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

testmodule2.c的Makefile如下

ifneq ($(KERNELRELEASE),)
	obj-m:=testmodule2.o
else
	PWD:=$(shell pwd)
	KDIR:=/lib/modules/$(shell uname -r)/build
all:
	make -C $(KDIR) M=$(PWD) modules
endif
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值