前一篇文章讨论了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. Centos7 关闭Ipv6

  2. C++使用命名空间中成员的三种方式

    通过简单的代码来介绍使用命名空间中成员的三种方式(我们最常用到的命名空间是是标准库std,下面的命名空间都以std为例): 使用作用域符:: #include<iostream> int ...

  3. 多版本python的使用

    装任一版本的virtualenv都可以 在创建virtualenv时只需指定python的安装位置就可使用该版本的python virtualenv --python=D:\install\pytho ...

  4. delphi中httpencode使用注意事项

    delphi中httpencode使用注意事项 一.uses HTTPApp二.使用前要用UTF8Encode转换成utf-8编码HTTPEncode(UTF8Encode(Text));不然和标准的 ...

  5. lua 写逻辑打印log时,打印到一半后停止不再打印,程序停止

    问题描述:ubuntu下用lua开发游戏电子邮件模块,自己测试时向用户推送100封,而用户最多只能有50封.这是调用sysdelete删除一些邮件.当打印log时,打印到一半后程序中途停止.将打印lo ...

  6. uva 11752 The Super Powers 素数+大数判断大小

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  7. Spring中AOP的初窥和入门小案例

    AOP:面向切面编程 AOP的主要作用:是为了程序员更好的关注"业务",专心"做事" 加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心 ...

  8. Python运行的17个时新手常见错误小结

    1)忘记在if , elif , else , for , while , class ,def 声明末尾添加 :(导致“SyntaxError :invalid syntax”) 该错误将发生在类似 ...

  9. spring boot: java @interface注解

    @interface是用来自定义JAVA Annotation的语法,@interface是用来自定义注释类型的 注释类型的定义跟定义一个接口相似,我们需要在 interface这个关键字前面加上一个 ...

  10. php的http数据传输get/post...

    php的http数据传输get/post... 一般有:file_get_contents,curl,fsockopen.... 下面介绍fsockopen: //构造要post的字符串 $argv ...