前一篇文章讨论了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. showModalDialog改进版,包括Chrome下的特殊处理

    父页面: if(window.ActiveXObject){ //IE          $("#choose_entp").click(function(){           ...

  2. java入门了解03

    ASSCII表 . 1.final关键字    (1)含义:最终的意思,修饰方法,类,变量    (2)特点:          A:修饰类,不能被继承          B:修饰的方法,不能被重载  ...

  3. wareshark网络协议分析之DHCP

    声明:本文关于DHCP协议介绍部分摘自百度百科 一.DHCP协议介绍: DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用 ...

  4. 创建表空间及用户的SQL

    --创建表SOFA空间: CREATE SMALLFILE TABLESPACE "SOFA" DATAFILE 'G:\oracle\product\10.2.0\ORADATA ...

  5. UOJ12 猜数

    这一天,小Y.小D.小C正在愉快地玩耍. 小Y是个数学家,他一拍脑袋冒出了一个神奇的完全平方数 nn. 小D是个机灵鬼,很快从小Y嘴里套出了 nn 的值.然后在脑内把 nn 写成了 a×ba×b的形式 ...

  6. PHP error_get_last() 函数

    error_get_last() 函数返回最后发生的错误 返回了一个关联数组,描述了最后错误的信息,以该错误的 "type". "message"." ...

  7. 智课雅思词汇---十五、前缀co-com-con-col-cor-是什么意思

    智课雅思词汇---十五.前缀co-com-con-col-cor-是什么意思 一.总结 一句话总结:前缀:co- 表示"共同", 通常放在元音词根前 1.前缀co-com-con- ...

  8. Excel 导入到处问题处理!

    1.未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0" 因为没有安装64位的Jet40驱动.可以到 http://www.microsoft.com/downl ...

  9. 批处理BAT(一)

    批处理 全盘根目录下查找文件夹并删除(1)@echo offfor %%a in (C D E F G H) DO (cd\if exist "%%a:\123\" rd /s / ...

  10. Python正则表达式之 - ?: / ?= / ?!

    Python正则表达式之 - ?: / ?= / ?! 用圆括号将所有选择项括起来,相邻的选择项之间用|分隔.但用圆括号会有一个副作用,使相关的匹配会被缓存,此时可用?:放在第一个选项前来消除这种副作 ...