操作系统第二章实验:Linux 内核模块
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/types.h>struct birthday
{
int day;int month;int year;struct list_head list;
};static LIST_HEAD(birthday_list);int simple_init(void)
{
struct birthday *p1,*p2,*p3,*p4,*p5;p1 = kmalloc(sizeof(*p1),GFP_KERNEL);p1->day = 2;p1->month = 8;p1->year = 1995;INIT_LIST_HEAD(&p1->list); p2 = kmalloc(sizeof(*p2),GFP_KERNEL);p2->day = 3;p2->month = 9;p2->year = 1996;INIT_LIST_HEAD(&p2->list);p3 = kmalloc(sizeof(*p3),GFP_KERNEL);p3->day = 4;p3->month = 10;p3->year = 1997;INIT_LIST_HEAD(&p3->list);p4 = kmalloc(sizeof(*p4),GFP_KERNEL);p4->day = 5;p4->month = 11;p4->year = 1998;INIT_LIST_HEAD(&p4->list);p5 = kmalloc(sizeof(*p5),GFP_KERNEL);p5->day = 6;p5->month = 12;p5->year = 1999;INIT_LIST_HEAD(&p5->list);list_add_tail(&p1->list,&birthday_list);list_add_tail(&p2->list,&birthday_list);list_add_tail(&p3->list,&birthday_list);list_add_tail(&p4->list,&birthday_list);list_add_tail(&p5->list,&birthday_list);struct birthday *ptr;list_for_each_entry(ptr,&birthday_list,list){
printk(KERN_INFO "day: %d\tmonth: %d\tyear: %d\n",ptr->day,ptr->month,ptr->year);}printk(KERN_INFO "Loading Module\n");return 0;
}void simple_exit(void)
{
struct birthday *ptr,*next;list_for_each_entry_safe(ptr,next,&birthday_list,list){
list_del(&ptr->list);kfree(ptr);}printk(KERN_INFO "Removing Module\n");
}module_init(simple_init);
module_exit(simple_exit);MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
按照实验要求一步一步来,挺简单的,加油!
Continue to improve…