操作系统:Linux Kernel Modules

#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
struct birthday{
	int day;
 	int month;
	int year;
	struct list_head list;
};
static LIST_HEAD(birthday_list);
int project1_init(void){
	struct birthday *person1,*person2,*person3,*person4,*person5;

	person1 = kmalloc(sizeof(*person1),GFP_KERNEL);
	person2 = kmalloc(sizeof(*person2),GFP_KERNEL);
	person3 = kmalloc(sizeof(*person3),GFP_KERNEL);
	person4 = kmalloc(sizeof(*person4),GFP_KERNEL);
	person5 = kmalloc(sizeof(*person5),GFP_KERNEL);
	
	person1->day = 2;
	person1->month = 8;
	person1->year = 1995;
	person2->day = 3;
	person2->month = 9;
	person2->year = 1995;
	person3->day = 6;
	person3->month = 4;
	person3->year = 1990;
	person4->day = 7;
	person4->month = 10;
	person4->year = 2000;
 	person5->day = 24;
	person5->month = 12;
 	person5->year = 2001;


	list_add_tail(&person1->list,&birthday_list);
	list_add_tail(&person2->list,&birthday_list);
	list_add_tail(&person3->list,&birthday_list);
	list_add_tail(&person4->list,&birthday_list);
	list_add_tail(&person5->list,&birthday_list);
	struct list_head *ptr;
	struct birthday *p;
	int i = 1;
	list_for_each(ptr,&birthday_list){
		p= list_entry(ptr,struct birthday,list);
		printk("%d person's birthday is %d.%d.%d\n",i,p->year,p->month,p->day);
		i++;
	}
	return 0;
}
void project1_exit(void){
	struct list_head *pos,*n;
	struct birthday *p;
	int i = 1;
	list_for_each_safe(pos,n,&birthday_list){
		list_del(pos);
		p = list_entry(pos,struct birthday,list);
		kfree(p);
		printk("%d person havs removed from the list\n",i);
		i++;
	}
}

module_init(project1_init);
module_exit(project1_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

在这里插入图片描述
在这里插入图片描述
抄了代码给我点个赞

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值