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);

转载于:https://www.cnblogs.com/defen/p/4728430.html

linux内核链表的使用相关推荐

  1. linux内核链表使用例,linux内核链表的使用例子

    linux内核链表的使用例子 #include #include #include #include #include #include MODULE_LICENSE("GPL") ...

  2. linux内核链表以及list_entry--linux内核数据结构(一)

    传统的链表实现 之前我们前面提到的链表都是在我们原数据结构的基础上增加指针域next(或者prev),从而使各个节点能否链接在一起, 比如如下的结构信息 typedef struct fox { un ...

  3. linux内核链表分析

    一.常用的链表和内核链表的区别 1.1  常规链表结构        通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...

  4. Linux内核链表交换节点,[笔记]Linux内核链表:结点的插入、删除以及链表的遍历...

    Linux内核链表:结点的插入.删除以及链表的遍历 1. Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p,这样每个结构类型为A的变量a中,都拥有同样的成员p, ...

  5. 深入分析 Linux 内核链表--转

    引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...

  6. 第六天2017/04/11(2:Linux内核链表Demo、顺序表、链表的开发与设计)

    //结构体相关的高级话题#include "stdio.h" #include "stdlib.h" #include "string.h" ...

  7. Linux 内核链表 【转】

    原文:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据的数 ...

  8. Linux驱动编程 step-by-step (十) Linux 内核链表

    终于可以清闲下来打理一下我的blog了,台资企业真的事情很多很烦-- 前几篇文章对字符设备有个简单介绍,并以简单的一个字符设备驱动作结尾,其实linux上大部分驱动程序都是字符设备程序,Linux源码 ...

  9. Linux 内核链表剖析(二十)

    上节博客中,我们讲到了 Linux 中的宏定义 offsetof 与 container_of 宏.那么本节我们的课程目标就是一直 Linux 内核链表,使其适用于非 GNU 编译器,分析 Linux ...

最新文章

  1. 机器学习中常用到的知识点总结
  2. 一起谈.NET技术,初识Silverlight 4及其架构
  3. 基础数据仓库环境搭建(二) Hadoop的环境搭建
  4. php网页论坛制作教程,PHP开发 小型论坛教程之登录HTML页面
  5. [js] 举例说明Object.defineProperty会在什么情况下造成循环引用导致栈溢出?
  6. tomcat服务器配置优化
  7. idea中java导包快捷键_JAVA入门:IntelliJ Idea 常用快捷键
  8. 3mx转osgb_如何将无人机Las点云数据转为Osgb数据
  9. 用键盘输入一位整数,当输入1~7时,显示对应的英文星期名称的缩写。
  10. KDD2015,Accepted Papers
  11. 黑马瑞吉外卖项目开发笔记
  12. 【编程实践】为了带你搞懂RPC,我们手写了一个RPC框架
  13. org.springframework.dao.InvalidDataAccessApiUsageException: ERR wrong number of arguments for ‘srem‘
  14. 苹果申请新专利,iPhone或取消刘海设计
  15. Linux下串口的配置
  16. RT-Thread 传感器软件包归类
  17. thinkphp构架
  18. 华裔科学家张首晟:个人数据完全由个人拥有
  19. 怎么修改设置路由器默认密码
  20. 3.笔记go语言——函数

热门文章

  1. 9月PMP考试起用新考纲,这些变化你了解了吗?
  2. 阿里云携手 VMware 共建云原生 IoT 生态,聚开源社区合力打造领域标准
  3. Dubbo 在 K8s 下的思考
  4. Serverless 与容器决战在即?有了弹性伸缩就不一样了
  5. 技术解析系列 | PouchContainer 支持 LXCFS 实现高可靠容器隔离
  6. php 执行命令屏幕输出捕捉,在php执行linux命令时显示所有输出
  7. cpu飙升 死循环_java排查一个线上死循环cpu暴涨的过程分析
  8. 远程定位gps linux,GPS(NMEA)数据解析
  9. 何不用python_人生几何,何不Python当歌
  10. python for everybody课程作业_Programming for Everybody (Python)-Lecture 7 课程笔记