本文转载自:http://blog.csdn.net/forever_key/article/details/6798685

Linux设备驱动工程师之路——内核链表的使用

K-Style

转载请注明来自于衡阳师范学院08电2  K-Style  http://blog.csdn.net/ayangke,QQ:843308498 邮箱:yangkeemail@qq.com

一、重要知识点

1.内核链表和普通链表的区别

内核链表是一个双向链表,但是与普通的双向链表又有所区别。内核链表中的链表元素不与特定类型相关,具有通用性。

我们先来看一幅图

kernel list展示的是内核链表的结构,normallist展示的是普通链表的结构。head是链表头,p1,p2,p3是链表节点。从图中可以看出普通链表的p1的next指针是指向的结构体p2的地址,p2的pre指针指向p1结构体的地址。而内核链表的p1的next指向的是p2结构体中包含pre和next部分的地址,的p2的pre指向的是p1结构体中包含pre和next部分的地址。依此类推,这就是区别。内核结构元素不与特定类型结构相关,任何结构体都可通过内核的添加成为链表中的节点。

2.内核链表的具体操作

链表数据结构的定义

structlist_head

{

struct list_head *next, *prev;
}

初始化链表头

INIT_LIST_HEAD(list_head*head)

插入节点

list_add(structlist_head *new, struct list_head *head)

list_add_tail(structlist_head *new, sturct list_head *head)

第一个函数在head后面插入一个节点

第二个函数在链表尾部插入一个节点

删除节点:

list_del(structlist_head *entry)

提取数据结构:

list_entry(ptr,type, member)

ptr为已知节点指针ptr,type为节点结构体类型,member为节点指针的type结构体中的名字。返回type结构体的指针。

遍历:

list for each(structlist_head *ops, struct list_head *head)

从head开始遍历每个节点,节点指针保存在ops里面。

二、实例

    1. #include <linux/kernel.h>
    2. #include <linux/module.h>
    3. #include <linux/init.h>
    4. #include <linux/slab.h>
    5. #include <linux/list.h>
    6. MODULE_LICENSE("GPL");
    7. MODULE_AUTHOR("David Xie");
    8. MODULE_DESCRIPTION("ListModule");
    9. MODULE_ALIAS("List module");
    10. struct student
    11. {
    12. char name[100];
    13. int num;
    14. struct list_head list;
    15. };
    16. struct student *pstudent;
    17. struct student *tmp_student;
    18. struct list_head student_list;
    19. struct list_head *pos;
    20. int mylist_init()
    21. {
    22. inti = 0;
    23. INIT_LIST_HEAD(&student_list);
    24. pstudent= kmalloc(sizeof(struct student)*5,GFP_KERNEL);
    25. memset(pstudent,0,sizeof(structstudent)*5);
    26. for(i=0;i<5;i++)
    27. {
    28. sprintf(pstudent[i].name,"Student%d",i+1);
    29. pstudent[i].num= i+1;
    30. list_add(&(pstudent[i].list), &student_list);
    31. }
    32. list_for_each(pos,&student_list)
    33. {
    34. tmp_student= list_entry(pos,struct student,list);
    35. printk("<0>student%d name: %s\n",tmp_student->num,tmp_student->name);
    36. }
    37. return0;
    38. }
    39. void mylist_exit()
    40. {
    41. inti ;
    42. for(i=0;i<5;i++)
    43. {
    44. list_del(&(pstudent[i].list));
    45. }
    46. kfree(pstudent);
    47. }
    48. module_init(mylist_init);
    49. module_exit(mylist_exit);

Linux设备驱动工程师之路——内核链表的使用【转】的更多相关文章

  1. linux设备驱动归纳总结(一)内核的相关基础概念【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-59413.html linux设备驱动归纳总结(一):内核的相关基础概念 xxxxxxxxxxxxxx ...

  2. 转:Linux设备驱动开发(1):内核基础概念

    一.linux设备驱动的作用 内核:用于管理软硬件资源,并提供运行环境.如分配4G虚拟空间等. linux设备驱动:是连接硬件和内核之间的桥梁. linux系统按个人理解可按下划分: 应用层:包括PO ...

  3. 【Linux开发】linux设备驱动归纳总结(一):内核的相关基础概念

    linux设备驱动归纳总结(一):内核的相关基础概念 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  4. 《Linux设备驱动开发具体解释(第3版)》(即《Linux设备驱动开发具体解释:基于最新的Linux 4.0内核》)网购链接

    <Linux设备驱动开发具体解释:基于最新的Linux 4.0内核> china-pub   spm=a1z10.3-b.w4011-10017777404.30.kvceXB&i ...

  5. linux设备驱动归纳总结(七):1.时间管理与内核延时【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-100005.html linux设备驱动归纳总结(七):1.时间管理与内核延时 xxxxxxxxxxx ...

  6. (转载)小白的linux设备驱动归纳总结(一):内核的相关基础概念---学习总结

    1. 学习总结 小白的博客讲的linux内核驱动这一块的东西比较基础,因此想通过学习他的博客,搭配着看书的方式来学习linux内核和驱动.我会依次更新在学习小白的博客的过程的感悟和体会. 2.1 内核 ...

  7. 【Linux开发】linux设备驱动归纳总结(七):1.时间管理与内核延时

    linux设备驱动归纳总结(七):1.时间管理与内核延时 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  8. 【Linux开发】linux设备驱动归纳总结(七):2.内核定时器

    linux设备驱动归纳总结(七):2.内核定时器 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  9. linux 设备驱动概述

    linux 设备驱动概述 目前,Linux软件工程师大致可分为两个层次: (1)Linux应用软件工程师(Application Software Engineer):       主要利用C库函数和 ...

随机推荐

  1. mobiscroll.js 使用

    使用较为详情的参考网址:http://www.lanrenmaku.com/jMobile/2014_1231_1357.html

  2. background-size的cover和content的用法

    background-size:cover; 表示背景图拉伸布满,如果在手机上做的话,背景图片会拉大,失真.这样做不妥 background-size:content; 表示背景图片在内容区域正常显示 ...

  3. opencv的高斯混合模型

    http://blog.jasonding.top/2015/04/05/Machine%20Learning/%E3%80%90%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%8 ...

  4. UIlabel多行文字自动换行 (自动折行)

    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(, , , )]; UILabel *label = [[UILabel al ...

  5. How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  6. spring 定时任务标注

    使用spring框架,需要定时任务只需要在方法上加@Component 就可以了 package hello; import java.text.SimpleDateFormat; import ja ...

  7. ios理解 -- Pro Mutlithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch, and Blocks

    Capturing automatic variables Next, you need to learn what the “together with automatic (local) vari ...

  8. mongodb远程连接以及备份、还原、导出、导入

    一.远程连接mongodb 连接命令:mongo -u username -p pwd 192.168.41.215:27017/database(用户名对应的数据库) 二.mongodump备份数据 ...

  9. java 线上问题定位工具

    在JDK的bin目录下有很多命令行工具: 我们可以看到各个工具的体积基本上都稳定在27kb左右,这个不是JDK开发团队刻意为之的,而是因为这些工具大多数是jdk\lib\tools.jar类库的一层薄 ...

  10. C++Primer STL算法

    //1.概览: // A:beg和end是表示元素范围的迭代器. // B:beg2是表示第二个输入序列开始位置的迭代器.end2表示第二个序列的末尾位置,若没有end2,则假定beg2表示的序列至少 ...