linux内核链表的使用
linux内核链表:
链表通常包括两个域:数据域和指针域。
struct list_head{
struct list_head *next,*prev;
};
include/linux/list.h中实现了一套精彩的链表数据结构。
传统的链表指针指向下一个节点的头部。linux链表指针指向下一个指针list_head结构(*next),双向循环。不会随着外部数据的变化而变化,使它具有通用性。?
-------------------------------------------------------------------
linux内核提供的链表主要操作:
1.初始化链表头:
INIT_LIST_HEAD(list_head *head):将*head,*prev指向list本身。
2.插入节点
list_add(struct list_head *new,struct list_head *head)
list_add_tail(struct list_head *new,struct list_head *head)
3.删除节点
list_del(struct list_head *entry:一个指针结构体)
4.提取数据结构
list_entry(ptr(指向list_head的指针),type(外部结构的类型),member(struct list_head对应的成员名))
已知数据结构中的节点指针ptr,找出数据结构,例
list_entry(aup,struct,autofs,list)
5.遍历
list_for_each(struct list_head *pos,struct list_head *head(你所需要遍历的链表的链表头))
如:
struct list_head *entry;
struct list_head cs46xx_devs;//链表头
list_for_each(entry,&cs46xx_devs){
card = list_entry(entry,struct cs_card,list);
if(card->dev_midi == minor)
break;
}
看内核代码看list_entry是如何实现的???
----------------------------------------------------------
内核链表的一个具体实现实例:
#include<linux/list.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/slab.h> //kfree kmalloc
MODULE_LICENSE("GPL");//(general public license)
struct student{
char name[100];
int num;
struct list_head list;
}
struct student *pstudent;
struct student *tmp_student;
struct list_head student_list;
struct list_head *pos;
int mylist_init(){
int i=0;
INIT_LIST_HEAD(&student_list);
pstudent=kmalloc(sizeof(struct student)*5,GFP_KERNEL);//分配空间。
memset(pstudent,0,sizeof(struct student)*5);//初始化
for(i=0;i<5;i++){
sprintf(pstudent[i].name,"student%d",i+1);
pstudent[i].num=i+1;
list_add(&(pstudent[i].list),&student_list);
}//循环插入学生信息
list_for_each(pos,&student_list)
{
tmp_student = list_entry(pos,struct student,list);
printk("<0>student%d name:%s\n",tmp_student->num,tmp_student->num,tmp_student->name);
}
return 0;
}//遍历学生信息
void mylist_exit()
{
int i;
for(i=0;i<5;i++){
list_del(&(pstudent[i].list));
}
kfree(pstudent);
}
module_init(mylist_init);
module_exit(mylist_exit);
linux内核链表的使用的更多相关文章
- C语言 Linux内核链表(企业级链表)
//Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 深入分析 Linux 内核链表--转
引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...
- Linux 内核链表
一 . Linux内核链表 1 . 内核链表函数 1.INIT_LIST_HEAD:创建链表 2.list_add:在链表头插入节点 3.list_add_tail:在链表尾插入节点 4.list_d ...
- linux内核链表分析
一.常用的链表和内核链表的区别 1.1 常规链表结构 通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...
- 深入分析 Linux 内核链表
转载:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/ 一. 链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指 ...
- Linux 内核 链表 的简单模拟(2)
接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...
- Linux 内核 链表 的简单模拟(1)
第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...
- linux内核链表的移植与使用
一. Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲.由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用. /********************* ...
- [国嵌攻略][108][Linux内核链表]
链表简介 链表是一种常见的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...
随机推荐
- HTTP协议简介
一.简介 HTTP(HyperText Transfer Protocol, 超文本传输协议) 是访问互联网使用的核心通信协议,也是所有web应用程序使用的通信协议.消息模型:客户端发送请求消息,服务 ...
- navicat将多个表导出为一个sql文件
1.shift选中多个表 2右键选择--转储sql文件---结构和数据
- git只添加指定类型的文件的.gitignore规则
#忽略根目录下的所有文件 * #忽略子目录下的所有文件 /* #包含目录 !*/ #指定不忽略的文件 !*.c !*.h #忽略根目录下的文件 /build/ /appveyor/ /pear/ /s ...
- plist涉及到沙盒的一个问题
http://blog.csdn.net/wowxavi1/article/details/8557271
- different between<A Href> and <jsp: forward>
i want to access Servelt by hyperlink in a jsp web site connection.jsp <%@ page contentType=" ...
- maven配置文件详解
settings.xml 本篇文章主要对maven中setting.xml配置文件进行解释 1.声明规范 <settings xmlns="http://maven.apache.or ...
- mybatis实战教程三:多对多关联
MyBatis3.0 添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作 一.创建student.te ...
- Django类方式写view
问题: Django官方教程中都是通过def函数方式来写view,如何通过类方式写view以及为何要通过类方式写view? 那,如何解决这个问题? 用户访问浏览器,一般两种方式,get获取网页和pos ...
- java.lang.IllegalArgumentException: Document base D:\Project\workspace\space\.metadata\.plugins\org.eclipse.wst.server.core\tmp7\wtpwebapps\Blog
java.lang.IllegalArgumentException: Document base D:\Project\workspace\space\.metadata\.plugins\org. ...
- 计算器(Ext)
<html> <head> <title>计算器</title> <meta charset="UTF-8"> < ...