嵌入式学习-驱动开发前奏-lesson3-linux内核链表

2024-05-27 09:48

本文主要是介绍嵌入式学习-驱动开发前奏-lesson3-linux内核链表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1)链表简介

链表是一种常用的数据结构,它通过指针将一系列数据节点连接成一条数据链。相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入或删除数据。链表的开销主要是访问的顺序性和组织链的空间损失。

2)链表的分类和组成

链表的最基本组成元素,节点每一个节点都是由数据域和指针域组成。
根据其不同,可以将链表分为单向链表、双向链表、双向循环链表。

注意:linux内核链表属于双向循环链表。

3)链表对比

传统链表与Linux内核链表有一定的区别。
传统链表,指针域的指针是指向节点的开始
而内核链表,并没有指向节点的开始,而是指向指针域,至于再指向指针域后,如何获取数据域的数据,在后面的list_entry函数中会有详细的说明。

4)linux内核链表结构

如下:

struct list_head
{struct list_head *next, *prev;
};

list_head结构包含两个指向list_head结构的指针prev和next,由此可见,内核的链表具备双链表功能,实际上,通常它都组织成双向循环链表。

以下是内核链表中相关的函数:
- INIT_LIST_HEAD:创建链表
- list_add:在链表头插入节点
- list_add_tail:在链表尾插入节点
- list_del:删除节点
- list_entry:取出节点(访问节点里面的内容)
- list_for_each:遍历链表
理解:以上所有的函数需要内核的支持,已经在内核中定义好了

  • 1.INIT_LIST_HEAD
static inline void INIT_LIST_HEAD(struct list_head *list)
{list->next = list;list->prev = list;
}

让其前向指针和后项指针都指向自己,这样就创建了一个空的链表

-2. list_add

在链表头插入节点,其函数原型为:

/*** list_add - add a new entry* @new: new entry to be added* @head: list head to add it after** Insert a new entry after the specified head.* This is good for implementing stacks.*/
static inline void list_add(struct list_head *new, struct list_head *head)
{__list_add(new, head, head->next);
}

上面struct list_head *new 为要加进链表的数据结构,struct list_head *head为链表头,
这个函数的作用就是在链表头和链表头的下一个数据结构之间插入一个新的链表。
__list_add函数的原型为:

/** Insert a new entry between two known consecutive entries.** This is only for internal list manipulation where we know* the prev/next entries already!*/
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,struct list_head *prev, struct list_head *next)
{next->prev = new;new->next = next;new->prev = prev;prev->next = new;
}
#else

将上面的每一步画成图,如下所示:
这里写图片描述

  • 3.list_add_tail

在链表尾插入节点,其原型如下所示:

/*** list_add_tail - add a new entry* @new: new entry to be added* @head: list head to add it before** Insert a new entry before the specified head.* This is useful for implementing queues.*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{__list_add(new, head->prev, head);
}

和上面的list_add函数有些类似,同样是再次调用__list_add函数,但是参数的顺序不同,在结点指针head所指向结点的前面插入new所指向的结点。当head指向头节点时,也相当于在尾结点后面增加一个new所指向的结点

  • 4.list_del

删除节点,其原型如下:(注意:不同的cpu,其内核代码可能有所不同)

/*** list_del - deletes entry from list.* @entry: the element to delete from the list.* Note: list_empty() on entry does not return true after this, the entry is* in an undefined state.*/
#ifndef CONFIG_DEBUG_LIST
static inline void list_del(struct list_head *entry)
{__list_del(entry->prev, entry->next);entry->next = (void *)0xDEADBEEF;entry->prev = (void *)0xBEEFDEAD;
}
#else

__list_del的原型为:

/** Delete a list entry by making the prev/next entries* point to each other.** This is only for internal list manipulation where we know* the prev/next entries already!*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{next->prev = prev;prev->next = next;
}

画图如下:
这里写图片描述

  • 5list_entry

取出节点里面的内容

/*** list_entry - get the struct for this entry* @ptr:    the &struct list_head pointer.* @type:   the type of the struct this is embedded in.* @member: the name of the list_struct within the struct.*/
#define list_entry(ptr, type, member) \container_of(ptr, type, member)#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
/*** container_of - cast a member of a structure out to the containing structure* @ptr:    the pointer to the member.* @type:   the type of the container struct this is embedded in.* @member: the name of the member within the struct.*  */
#define container_of(ptr, type, member) ({          \const typeof(((type *)0)->member)*__mptr = (ptr);    \(type *)((char *)__mptr - offsetof(type, member)); })
  • 6list_for_each

遍历链表其源代码为:

/*** list_for_each    -   iterate over a list* @pos:    the &struct list_head to use as a loop cursor.* @head:   the head for your list.*/
#define list_for_each(pos, head) \for (pos = (head)->next; prefetch(pos->next), pos != (head); \pos = pos->next)

4)内核链表编程实验

mylist.c

#include<linux/module.h>
#include<linux/init.h>
#include<linux/list.h>struct score
{int num;int english;int math;struct list_head list; /*节点*/
};struct list_head score_head; /*链表头*/
struct score stu1,stu2,stu3;
struct list_head *pos; //指针 相当于光标
struct score *tmp;int mylist_init()
{/*创建链表*/INIT_LIST_HEAD(&score_head);stu1.num = 1;stu1.english = 90;stu1.math = 98;list_add_tail(&(stu1.list),&score_head); /*在链表尾插入节点*/stu2.num = 2;stu2.english = 92;stu2.math = 91;list_add_tail(&(stu2.list),&score_head);stu3.num = 3;stu3.english = 94;stu3.math = 95;list_add_tail(&(stu3.list),&score_head);/*遍历链表*/list_for_each(pos,&score_head){tmp = list_entry(pos,struct score,list);/*取出节点*/printk(KERN_WARNING"NO %d, english is %d, math is %d\n",tmp->num,tmp->english,tmp->math);   }return 0;   
}void mylist_exit()
{/*删除节点*/list_del(&(stu1.list));list_del(&(stu2.list));
}module_init(mylist_init);
module_exit(mylist_exit);
MODULE_LICENSE("GPL");

根据上面的程序,将其编译,运行,便可以看到结果,这里就不再贴图了。

菜鸟一枚,如有错误,多多指教。。。

这篇关于嵌入式学习-驱动开发前奏-lesson3-linux内核链表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1007135

相关文章

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

Linux之计划任务和调度命令at/cron详解

《Linux之计划任务和调度命令at/cron详解》:本文主要介绍Linux之计划任务和调度命令at/cron的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux计划任务和调度命令at/cron一、计划任务二、命令{at}介绍三、命令语法及功能 :at

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Linux ls命令操作详解

《Linuxls命令操作详解》通过ls命令,我们可以查看指定目录下的文件和子目录,并结合不同的选项获取详细的文件信息,如权限、大小、修改时间等,:本文主要介绍Linuxls命令详解,需要的朋友可... 目录1. 命令简介2. 命令的基本语法和用法2.1 语法格式2.2 使用示例2.2.1 列出当前目录下的文