首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

算法与数据结构之十----内核中的链表操作学习

/**************************************************************** 文件内容:内核之链队操作 版本V1.0 作者:HFL 时间:2013-12-22 说明:用户态中链表每个节点包含数据域和指针域,而内核态是每个数据中包含链表 因此内核态链表一般是嵌套在某个包含数据成员的结构体来实现。 内核的链表应用非常广泛:进程管理,定时器,工作队列,运行队列。总之 内核对于多个数据的组织和多个熟悉的描述都是通过链表串起来的。  *****************************************************************/  #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/list.h> MODULE_DESCRIPTION("My Module"); MODULE_ALIAS("My module"); MODULE_LICENSE("GPL"); MODULE_AUTHOR("HFL21014"); struct student {     char name[100];     int counter;     struct list_head list; }; struct student *Mystudent; struct student *Temp_student; struct list_head student_list; struct list_head *pos; int Kernel_list_init() { int j = 0; INIT_LIST_HEAD(&student_list); Mystudent = kmalloc(sizeof(struct student)*5,GFP_KERNEL); memset(Mystudent,0,sizeof(struct student)*5); for(j=0;j<5;j++) {        sprintf(Mystudent[i].name,"Student%d",j+1);       Mystudent[j].counter = j+1;      list_add( &(Mystudent[j].list), &student_list); }  list_for_each(pos,&student_list) //遍历整个内核链表,pos其实就是一个for循环标量。中间临时使用,既不输入也不输出 { Temp_student = list_entry(pos,struct student,list);  printk("hello,my student %d  name: %s\n",Temp_student->counter,Temp_student->name); } return 0; } void Kernel_list_exit() { int k ; /* 模块卸载是要删除链表,并释放内存 */ for(k=0;k<10;jk++) { list_del(&(Mystudent[k].list));      } kfree(Mystudent); } module_init(Kernel_list_init);

03
您找到你想要的搜索结果了吗?
是的
没有找到
领券