Linux 中list.h使用实例和坑

2023-11-11

以前都是自己写链表或者所用框架都自带链表操作,本次工作换了框架没有找到框架自带的链表操作,所以尝试使用linux自带的list.h中定义的相关宏和函数写了简单的链表操作,竟然踩坑了,记录一下。

一、list.h简介

list.h一般放在include/list.h,当然有的linux系统放list.h地方在别的地方,比如ubuntu。可以使用find / -name list.h找到,使用gcc编译的时候include上。

list.h是Linux内核中,为了提供统一的链表操作,减少结构体的额外开支提供的链表操作。list.h中定义的链表了一个双向循环链表,和一个哈希表。

1,基本结构

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

2,初始化定义

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)

3,功能定义

/**添加节点
 * 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_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.
 */
static inline void list_del(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
        entry->next = (struct list_head*)LIST_POISON1;
        entry->prev = (struct list_head*)LIST_POISON2;
}

/**链表判断
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
        return head->next == head;
}

4,查询定义


/** 直接读取节点的查询方法1
 * list_for_each_entry  -       iterate over list of given type
 * @pos:        the type * to use as a loop cursor.
 * @head:       the head for your list.
 * @member:     the name of the list_head within the struct.
 */
#define list_for_each_entry(pos, head, member)                          \
        for (pos = list_entry((head)->next, typeof(*pos), member);      \
             &pos->member != (head);    \
             pos = list_entry(pos->member.next, typeof(*pos), member))

/**直接读取节点的查询方法2
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:        the type * to use as a loop cursor.
 * @n:          another type * to use as temporary storage
 * @head:       the head for your list.
 * @member:     the name of the list_head within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)                  \
        for (pos = list_entry((head)->next, typeof(*pos), member),      \
                n = list_entry(pos->member.next, typeof(*pos), member); \
             &pos->member != (head);                                    \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))

二、实战

程序大概的意思是:创建一个5个成员的stu链表,打印;删除其中一个成员,再打印。带参数的可执行程序,./test 1:标识删除第一个,后面数字表示删除第几个。

#include "list.h"
#include <stdlib.h>
#include <stdio.h>

typedef struct _ststudent_
{
        int age;
        int grade;
}STU_T;

typedef struct _stulist_
{
        STU_T stStu;
        struct list_head list;
}STU_LIST_T;

LIST_HEAD(g_stStuList_Head);
void main(int argc,char *argv[])
{
        STU_LIST_T *pstStu=NULL;
        STU_LIST_T *pstStu_out=NULL;
        STU_LIST_T *n=NULL;
        int i=0;
        for(i=0;i<5;i++)
        {
                pstStu=(STU_LIST_T*)malloc(sizeof(STU_LIST_T));
                pstStu->stStu.age=i+10;
                pstStu->stStu.grade=i+100;
                list_add_tail(&pstStu->list,&g_stStuList_Head);
        }

        printf("----------------add---------------------\n");
        list_for_each_entry(pstStu_out,&g_stStuList_Head,list)
        {
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);
        }


        printf("----------------del---------------------\n");

#ifdef TEST_TRUE
        list_for_each_entry(pstStu_out,&g_stStuList_Head,list)
        {
                if(atoi(argv[1])==pstStu_out->stStu.age-10)
                {
                        list_del(&pstStu_out->list);
                        free(pstStu_out);
                        continue;
                }
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);

        }

#else

        list_for_each_entry_safe(pstStu_out,n,&g_stStuList_Head,list)
        {
                if(atoi(argv[1])==pstStu_out->stStu.age-10)
                {
                        list_del(&pstStu_out->list);
                        free(pstStu_out);
                        continue;
                }
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);

        }
#endif
        return;
}

1,为啥不用#include <list.h> ?

     因为我自己系统带的list.h不在标准库。。

root@ubuntu:/home/Ctest/clistTest# find / -name list.h
/lib/firmware/carl9170fw/tools/include/list.h
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-150/include/linux/list.h
/usr/src/linux-headers-5.4.0-150-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/revocation/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/include/linux/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/include/linux/list.h
/usr/src/linux-headers-5.4.0-135-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/revocation/list.h
/usr/src/linux-headers-5.4.0-136-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/revocation/list.h

2,编译

 gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE

3,-D是为了方便测试 预编译宏。

用来分别编译两个可执行程序说明遇到的坑

4,运行结果,带参数运行

坑:如果只查询使用list_for_each_entry()list_for_each_entry_safe()都可以,但是如果查出来要进行删除操作就需要用list_for_each_entry_safe(),否则会宕机。具体原因可以自己查看实现。

root@ubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE
root@ubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=11,grade=101
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
----------------del---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=593,grade=0
Segmentation fault (core dumped)
root@ubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ 
root@ubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=11,grade=101
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
----------------del---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
root@ubuntu:/home/Ctest/clistTest# 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Linux 中list.h使用实例和坑 的相关文章

  • 使用 matplotlib 从“列表列表”绘制 3D 曲面

    我已经搜索了一些 虽然我可以找到许多有用的网格网格示例 但没有一个清楚地表明我如何将列表列表中的数据转换为可接受的形式 以适应我所讨论的各种方式 当谈到 numpy matplotlib 以及我所看到的建议的术语和步骤顺序时 我有点迷失 我
  • 子目录中的头文件(例如 gtk/gtk.h 与 gtk-2.0/gtk/gtk.h)

    我正在尝试使用 GTK 构建一个 hello world 其中包括以下行 include
  • NUMA 在虚拟内存中是如何表示的?

    有许多资源 https en wikipedia org wiki Non uniform memory access从硬件角度描述NUMA的架构性能影响 http practical tech com infrastructure num
  • 如何在 Python 中创建多个(但单独的)空列表?

    我编写了一个脚本 该脚本有时会生成一堆空列表 应用具有以下结构的代码 A B C D 产生输出 A B C D 现在的情况是 每次使用不同的数据集作为输入时 我都必须手动修改字母 我希望能够实现自动化 我想过这样做 FieldList A
  • 高效的内存屏障

    我有一个多线程应用程序 其中每个线程都有一个整数类型的变量 这些变量在程序执行期间递增 在代码中的某些点 线程将其计数变量与其他线程的计数变量进行比较 现在 我们知道在多核上运行的线程可能会无序执行 一个线程可能无法读取其他线程的预期计数器
  • 捕获实时流量时如何开启纳秒精度?

    如何告诉 libpcap v1 6 2 将纳秒值存储在struct pcap pkthdr ts tv usec 而不是微秒值 捕获实时数据包时 Note This question is similar to How to enable
  • git 错误:无法处理 https

    当我尝试使用 git clone 时https xxx https xxx我收到以下错误我不处理协议 https 有人可以帮我吗 完整消息 dementrock dementrock A8Se git 克隆https git innosta
  • 无法在 Perl 中找到 DBI.pm 模块

    我使用的是 CentOS 并且已经安装了 Perl 5 20 并且默认情况下存在 Perl 5 10 我正在使用 Perl 5 20 版本来执行 Perl 代码 我尝试使用 DBI 模块并收到此错误 root localhost perl
  • Gearman,php 扩展问题:使用终端在 .. 中找不到类“GearmanWorker”,但可以在浏览器上使用

    我最近在 ubuntu 10 04 上安装了 gearman 并安装了它的 pecl 扩展 现在 当我在浏览器中运行一个 php 文件时 其中包含 client new GearmanWorker die var Dump client I
  • Fedora dnf 更新不起作用?

    当我尝试使用 update 命令更新 Fedora 22 时 sudo dnf update 我收到以下错误 错误 无法同步存储库 更新 的缓存 无法准备内部镜像列表 Curl 错误 6 无法解析主机名 无法解析主机 mirrors fed
  • 嵌入式linux编写AT命令

    我在向 GSM 模块写入 AT 命令时遇到问题 当我使用 minicom b 115200 D dev ttySP0 term vt100 时它工作完美 但我不知道如何在 C 代码中做同样的事情 我没有收到任何错误 但模块对命令没有反应 有
  • 为什么 list() == 1 给出结果,而 list() > 1 给出错误? [复制]

    这个问题在这里已经有答案了 即使这些比较没有意义 为什么 list 1 return False but list gt 1 return gt not supported between instances of list and int
  • 如何在 AppleScript 的处理程序中有效地构建列表?

    AppleScript 文档建议使用以下代码来有效构建列表 set bigList to set bigListRef to a reference to bigList set numItems to 100000 set t to ti
  • 使用 posix_spawn 启动进程

    我正在使用以下代码在 Linux 中启动新进程 pid t processID char argV 192 168 1 40 char 0 int status 1 status posix spawn processID home use
  • Raspberry 交叉编译 - 执行程序以“分段错误”结束

    我有一个自己编写的程序 我想从我的 x86 机器上为 Raspberry Pi 构建它 我正在使用 eclipse 生成的 makefile 并且无法更改此内容 我已经阅读了 CC for raspi 的教程 Hackaday 链接 htt
  • Linux中使用管道进行进程间通信

    我已经编写了在 linux 中写入数字以进行管道传输的代码 如下所示 但显示错误 任何人都可以帮助我解决这个问题 基本上该程序的问题陈述如下 一个程序将打开一个管道 向管道写入一个数字 其他程序将打开同一管道 读取数字并打印它们 关闭两个管
  • 递归分割列表函数 LISP

    split list 函数接受一个列表并返回一个由两个列表组成的列表 其中两个列表由输入的交替元素组成 我写了以下内容 defun split list L cond endp L list NIL NIL t let X split li
  • 使用 prolog 添加另外两次出现

    我有一个清单 a b a a a c c 我需要为每个元素添加两次以上的出现 最终结果应该是这样的 a a a b b b a a a a a c c c c 如果列表中有一个与下一个项目相同的项目 那么它会继续下去 直到出现一个新项目 当
  • X11 模式对话框

    如何使用 Xlib 在 X11 中创建模式对话框 模态对话框是一个位于应用程序其他窗口之上的窗口 就像瞬态窗口一样 并且拒绝将焦点给予应用程序的其他窗口 在 Windows 中 当试图从模态窗口夺取焦点时 模态也会通过闪 烁模态窗口的标题栏
  • R 中的整数或双精度列表

    我有一个大约 1000 个整数的列表 我需要能够进行一些数学计算 但它们被困在列表或字符形式中 我怎样才能切换它们以便它们可用 样本数据 gt y 1 1 7 3 1 6 7 1 7 6 5 3 1 3 3 0 6 2 4 9 19 1 9

随机推荐

  • 【java】maven引用外部 jar 包,以RXTXcomm.jar为例

    目录 1 作为资源文件夹内的资源引用 2 将jar包手动安装到maven仓库 工具 IntelliJ IDEA 2020 2 3 x64 1 作为资源文件夹内的资源引用 1 在项目根路径新建文件夹lib 将资源文件复制到该文件夹 2 将文件
  • python处理压缩文件

    Zip 模块安装 pip install zipfile 使用 import zipfile 打开zip文件 zipfileObj zipfile ZipFile tmp zip with上下文 with zipfile ZipFile t
  • C语言实现的 通讯录管理系统

    通讯录 C语言实现 文章目录 通讯录1 静态 通讯录2 动态 通讯录3 动态 储存 前言 本文讲解如何用C语言来创建一个通讯录 这是一个小项目 非常适合新手上手 同时也可以提高自己的代码能力 里面用到了 结构体传参 枚举常量 文件 动态内存
  • 惠普服务器开机系统密码,惠普(hp)各型号打印机冷复位,清零,回复出厂设置方法 以及 服务菜单(service menu)密码...

    惠普 hp 各型号打印机冷复位 清零 回复出厂设置方法 以及 服务菜单 service menu 密码 由会员分享 可在线阅读 更多相关 惠普 hp 各型号打印机冷复位 清零 回复出厂设置方法 以及 服务菜单 service menu 密码
  • 【java学习】String字符串

    1 概念 1 String 不可变 不可变类 final 不可被继承 public final class String implements java io Serializable Comparable
  • Typora如何将图片使用相对路径保存到统一文件夹中(解决.md文档传输丢图片的方法)

    Typora是一款编辑markdown语法的做离线的笔记软件 目前广受喜爱 由于通过Typora导入的图片不集中 图片要么在原始位置分散着 如果换台电脑图片就会丢失 默认是保存图片的原始绝对路径 而我们保存的地址放到别人电脑 绝对路径就不行
  • 鼠标移动实现标签自动切换

  • Linux进程通信:命名管道FIFO小结

    Linux下进程之间通信可以用命名管道FIFO完成 命名管道是一种特殊类型的文件 因为Linux中所有事物都是文件 它在文件系统中以文件名的形式存在 在程序中 我们可以使用两个不同的函数调用来建立管道 include
  • 自动化测试:yaml结合ddt实现数据驱动!

    在python unittest selenium ddt的框架中 数据驱动常见有以下几种方式实现 Csv txt Excel YAML 本文主要给大家介绍测试数据存储在YAML文件中的使用场景 首先先来简单介绍一下YAML 1 什么是YA
  • Jmeter(十四) - 从入门到精通 - JMeter定时器 - 下篇(详解教程)

    1 简介 用户实际操作时 并非是连续点击 而是存在很多停顿的情况 例如 用户需要时间阅读文字内容 填表 或者查找正确的链接等 为了模拟用户实际情况 在性能测试中我们需要考虑思考时间 若不认真考虑思考时间很可能会导致测试结果的失真 例如 估计
  • 华为荣耀30s怎样升级鸿蒙系统,华为手机鸿蒙系统升级名单:华为鸿蒙os适配手机汇总...

    护卫鸿蒙os马上就要正式在12月份上线了 此次适配的机型也是有很多的 首先是mate40可以体验 还有哪些可以搭配呢 鸿蒙系统升级名单 哪些机型能够升级到鸿蒙OS呢 此前有爆料信息显示 麒麟9000 麒麟990 5G 麒麟990 麒麟985
  • ug建模文本怎么竖着_UG编程文字加工,全方位实例讲解,文末有作业哦!

    文字加工常用于模具标记 零件装饰 简单文字雕刻等 如图6 1所示 文字加工一般在零件精加工之后进行 由于加工的刀具直径很小 很容易折断 因此文字加工切削量少 需要在转速高达10000 30000r min的高速机或雕刻机上才能以较短的时间完
  • 数据可视化练习-用powerBI生成自动播放的动态排行榜

    数据可视化练习 用powerBI生成自动播放的动态排行榜 前言 1 前期准备 账号注册及软件下载 2 数据获取及预处理 3 可视化效果搭建 4 发布共享 其他 前言 最近在B站很流行各种动态排行榜视频 动态排行榜制作源代码来自一位大神基于d
  • 笔记:以太坊geth客户端命令及参数

    geth命令的参数 nodiscover 使用此选项可确保未手动添加您的人员无法发现您的节点 否则 如果您的节点具有相同的创世纪文件和网络ID 则可能无意中将您的节点添加到陌生人的区块链中 maxpeers 0 如果您不希望任何其他人连接到
  • OCR字符识别

    OCR字符识别 流程 流程 读取图片并转灰度图 dev close window read image Image20230324222437 F halcon halconStudy 联想截图 20230324222437 png get
  • 关于druid的一些设置(idea版本)

    关于druid的一些设置 idea版本 1 首先是加入依赖
  • CTF Show Web6

    也是一道备份文件泄露的题目 在提示中可以看的很清楚 解压源码到当前目录 测试正常 收工 在上一题的wp中 看到了工具fzbk 使用之后发现所有备份文件都返回200 问了问群主之后发现这是nginx服务器的设置 遇到不存在的路径就直接跳转到默
  • Numpy中argsort()函数的用法

    argsort 函数的作用是将数组按照从小到大的顺序排序 并按照对应的索引值输出 argsort 函数中 当axis 0时 按列排列 当axis 1时 按行排列 如果省略默认按行排列 下边通过例子来说明其用法 usr bin env pyt
  • SpringBoot工程实现aop进行日志记录

    前言 我是在做某培训机构的外卖项目自己新增的一个功能 上网查了很多资料 资料很丰富但有些东西没有解释清楚 于是我花了一个晚上把那些大佬代码里面没有解释清楚的地方加了很多注解 仅供初学者参考 准备工作 0 建议了解一下aop的一些知识 以下代
  • Linux 中list.h使用实例和坑

    以前都是自己写链表或者所用框架都自带链表操作 本次工作换了框架没有找到框架自带的链表操作 所以尝试使用linux自带的list h中定义的相关宏和函数写了简单的链表操作 竟然踩坑了 记录一下 一 list h简介 list h一般放在inc