本文转载自: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. 记录:在老XPS1330上安装CentOS7

    下图是设置时的图片,注意分区设置. 下图是安装成功的画面. 下图是在Gnome桌面环境打开Firefox上本博客的画面. 注意点: 1.安装时没啥特殊的,就两点,一是要分区设置好,图省事就让自动分区: ...

  2. LeetCode H-Index II

    原题链接在这里:https://leetcode.com/problems/h-index-ii/ 题目: Follow up for H-Index: What if the citations a ...

  3. opencv hog+svm行人检测

    http://blog.csdn.net/masibuaa/article/details/16105073 http://blog.csdn.net/u011263315/article/detai ...

  4. POJ 1028解答

    #include <iostream>#include <cstdio>#include <cmath>#include <stack>#include ...

  5. Android --资料集合

    google android 官方教程 http://hukai.me/android-training-course-in-chinese/basics/index.html android视频资料 ...

  6. 启用Service Broker

    2015-10-20 17:31 整理,未发布数据库邮件配置向导,在选择配置任务页面点击下一步时,弹出"数据库邮件依赖于 Service Broker...".点击是,整个SSMS ...

  7. 装饰器、生成器,迭代器、Json & pickle 数据序列化

    1. 列表生成器:代码例子 a=[i*2 for i in range(10)] print(a) 运行效果如下: D:\python35\python.exe D:/python培训/s14/day ...

  8. Azure billing 分析

    昨天把西欧的2012的VM删掉,在北美新建一个2008的VM,装了sql2005 express 在C盘,这样存储就变成2个位置了,西欧和美国,然后放在那里不操作一天,发现billing多了很多, S ...

  9. PG sys function

    The System Catalogs of PostgreSQLscott=# \dS List of relations Schema | Name | Type | Owner -------- ...

  10. PostgreSQL configuration file postgresql.conf recommanded settings and how it works

    1        Set max_connections to three times the number of processor cores on the server. Include vir ...