前一篇文章讨论了list_head 结构的基本结构和实现原理,本文主要介绍一下实例代码。

自己如果想在应用程序中使用list_head 的相应操作(当然应该没人使用了,C++ STL提供了list 用起来貌似更方便), 在应用程序中需要包含自己的 "list.h" 头文件:

/*
注:这个list.h 是为了配合示例程序而建的,内容来自:linux/include/linux/list.h 和相关文件
*/
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H struct list_head {
struct list_head *next, *prev;
}; #define LIST_HEAD_INIT(name) { &(name), &(name) } #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );}) static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = 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;
} static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
} static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
} static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
} #define prefetch(x) __builtin_prefetch(x) //注:这里prefetch 是gcc的一个优化,也可以不要
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next) #define list_entry(ptr, type, member) \
container_of(ptr, type, member) #endif

写了一个简单的应用程序:

#include "list.h"
#include <stdio.h>
#include <string.h> #define MAX_NAME_LEN 32
#define MAX_ID_LEN 10 typedef struct stud
{
struct list_head list;
char name[MAX_NAME_LEN];
char stu_number[MAX_ID_LEN];
}num_n_stu; int main(void)
{
struct list_head head;
num_n_stu stu_1;
num_n_stu stu_2;
num_n_stu *entry; struct list_head *p;
INIT_LIST_HEAD(&head);
strcpy(stu_1.name,"lisi");
strcpy(stu_1.stu_number,"10000000"); strcpy(stu_2.name,"zhangsan");
strcpy(stu_2.stu_number,"10000001");
list_add(&stu_1.list,&head);
list_add(&stu_2.list,&head);
list_del(&stu_2.list);
list_for_each(p,&head)
{
entry=list_entry(p,struct stud,list);
printf("name: %s\n",entry->name);
printf("stu_number: %s\n",entry->stu_number);
}
list_del(&stu_1.list);
return 0;
}

在Linux内核中可以使用这个以类似驱动模块的形式加载到内核:(这里就不用使用自定义的list.h了)

#include <linux/list.h>
#include <linux/init.h>
#include <linux/module.h> MODULE_LICENSE("GPL"); #define MAX_NAME_LEN 32
#define MAX_ID_LEN 10 typedef struct stud
{
struct list_head list;
char name[MAX_NAME_LEN];
char stu_number[MAX_ID_LEN];
}num_n_stu; static int my_main(void)
{ struct list_head head;
num_n_stu stu_1;
num_n_stu stu_2;
num_n_stu *entry; struct list_head *p;
INIT_LIST_HEAD(&head); strcpy(stu_1.name,"lisi");
strcpy(stu_1.stu_number,"10000000"); strcpy(stu_2.name,"zhangsan");
strcpy(stu_2.stu_number,"10000001"); list_add(&stu_1.list,&head);
list_add(&stu_2.list,&head); list_del(&stu_2.list); list_for_each(p,&head)
{ entry=list_entry(p,struct stud,list); printk("name: %s\n",entry->name); printk("stu_number: %s\n",entry->stu_number); } list_del(&stu_1.list); return 0; } static void my_exit(void)
{
printk("my_exit ! \n");
} module_init(my_main);
module_exit(my_exit);

[转载]Linux内核list_head学习(二)的更多相关文章

  1. [转载]Linux 内核list_head 学习(一)

    在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head.虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作li ...

  2. Linux 内核list_head 学习

    Linux 内核list_head 学习(一) http://www.cnblogs.com/zhuyp1015/archive/2012/06/02/2532240.html 在Linux内核中,提 ...

  3. Linux内核分析(二)----内核模块简介|简单内核模块实现

    原文:Linux内核分析(二)----内核模块简介|简单内核模块实现 Linux内核分析(二) 昨天我们开始了内核的分析,网上有很多人是用用源码直接分析,这样造成的问题是,大家觉得很枯燥很难理解,从某 ...

  4. Linux内核驱动学习(八)GPIO驱动模拟输出PWM

    文章目录 前言 原理图 IO模拟输出PWM 设备树 驱动端 调试信息 实验结果 附录 前言 上一篇的学习中介绍了如何在用户空间直接操作GPIO,并写了一个脚本可以产生PWM.本篇的学习会将写一个驱动操 ...

  5. linux内核分析实践二学习笔记

    Linux实践二--内核模块的编译 标签(空格分隔): 20135328陈都 理解内核的作用 Linux内核[kernel]是整个操作系统的最底层,它负责整个硬件的驱动,以及提供各种系统所需的核心功能 ...

  6. Linux内核驱动学习(二)添加自定义菜单到内核源码menuconfig

    文章目录 目标 drivers/Kconfig demo下的Kconfig 和 Makefile Kconfig Makefile demo_gpio.c 目标 Kernel:Linux 4.4 我编 ...

  7. “Linux内核分析”实验二报告

    张文俊 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.第二周学习内 ...

  8. linux内核数据结构学习总结

    目录 . 进程相关数据结构 ) struct task_struct ) struct cred ) struct pid_link ) struct pid ) struct signal_stru ...

  9. Linux 内核协议栈 学习资料

    终极资料 1.<Understanding Linux Network Internals> 2.<TCP/IP Architecture, Design and Implement ...

随机推荐

  1. C++中容器的使用(一)

    C++中有两种类型的容器:顺序容器和关联容器. 顺序容器主要有vector.list.deque等.其中vector表示一段连续的内存,基于数组实现,list表示非连续的内存,基于链表实现,deque ...

  2. Python安装setuptools时报Compression requires the (missing) zlib

    装机员为您提供Python安装setuptools时报Compression requires the (missing) zlib的文章咨询供您阅读,如何使用Python安装setuptools时报 ...

  3. java转义符的一些用法

    那么这里在列上一些转义字符 \\ 反斜杠 \t 间隔 ('\u0009')\n 换行 ('\u000A')\r 回车 ('\u000D')\d 数字 等价于 [0-9]\D 非数字 等价于 [^0-9 ...

  4. js提示确认删除吗

    <script language="javascript"> function delcfm() { if (!confirm("确认要删除?")) ...

  5. 使用标签代替goto关键字

    众所周知,java中没有goto语句,但是保留了goto这个关键字.由于goto是在源码级上的跳转,多次使用goto会引起代码混乱容易出错,这也是java取消goto语句的目的所在,但是goto语句也 ...

  6. js添加后缀防止缓存

    jsp页面: 时间戳的话需要引入: <%@ page import="java.util.Date"%> <script type="text/java ...

  7. 将datagridview数据保为xml或txt文件

    using System.IOpublic void SaveFile()        {            //实例化一个保存文件对话框            SaveFileDialog s ...

  8. JAVA设计模式初探之组合模式

    先看看组合模式的定义吧:“将对象组合成树形结构以表示‘部分-整体’的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性.” 就拿剪发办卡的事情来分析一下吧. 首先,一张卡可以在总部,分店, ...

  9. 深度学习—BN的理解(一)

    0.问题 机器学习领域有个很重要的假设:IID独立同分布假设,就是假设训练数据和测试数据是满足相同分布的,这是通过训练数据获得的模型能够在测试集获得好的效果的一个基本保障.那BatchNorm的作用是 ...

  10. Postman工具——Pre-Request Script、Tests

    这篇是介绍 Postman 的最后一篇,也就是最后两个用法:Pre-Request Script 和 Tests ,它支持以嵌入脚本的方式动态准备测试数据,并根据业务需求设计测试用例. 一.Pre-R ...