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内核链表]
链表简介 链表是一种常见的数据结构,它通过指针将一系列数据节点连接成一条数据链.相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入 ...
随机推荐
- 《你不知道的JavaScript上卷》知识点笔记
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC" } p.p2 { margin: 0.0px ...
- tp5 url 线上访问 在nginx 上 出现404错误,解决办法(1.80nginx 配置 pathInfo)
对于ThinkPHP的URL访问路劲如:http://域名/index.php/Index/BlogTest/read,原先的Nginx的是不支持的pathinfo路劲的,导致你在thinkPHP ...
- Linux中rpm命令用法听语音
rpm 是红帽(RedHat)软件包管理工具,实现类似于 Windows 中的添加/删除程序功能.下面,就来向大家介绍 rpm 命令的用法. 工具/原料 CentOS 一.rpm常用参数 1 rpm ...
- dedecms后台怎么添加发布软件?织梦后台软件内容管理
使用织梦cms有很多的功能,其中有一个是在dedecms后台添加发布软件,然后在前台大家可以直接下载软件,在织梦cms后台怎么添加发布软件呢?下面是织梦软件内容管理的主要操作步骤. 使用织梦cms有很 ...
- css3图片动画旋转
body{ background-color:#021E36; text-align: center; } .container{margin:500px auto;} .round{position ...
- 随手小代码——《Python编程 从入门到实践》项目1:外星人入侵
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- Asp.net mvc 中Action 方法的执行(三)
[toc] 前面介绍了 Action 方法执行过程中的一些主要的组件以及方法执行过程中需要的参数的源数据的提供以及参数的绑定,那些都可以看作是 Action 方法执行前的一些必要的准备工作,接下来便将 ...
- Powerdesigner+PostgreSQL
1.准备软件 Powerdesigner PostgreSQL PostgreSQL ODBC驱动程序: psqlODBC,网址:http://www.postgresql.org/ftp/odbc/ ...
- pjax 笔记
PJAX的基本思路是,用户点击一个链接,通过ajax更新页面变化的部分,然后使用HTML5的pushState修改浏览器的URL地址,这样有效地避免了整个页面的重新加载.如果浏览器不支持history ...
- linux 磁盘加密和tpm搭配使用1
一.基本名称,容易混淆 1.dm-crypt是linux的2.6内核开始集成的一种磁盘加密功能.十几年来,连sche调度算法都被改了N次,但dm-crypt一直稳定在内核中,稳定性还是很好的. 2.c ...