[转载]Linux内核list_head学习(二)
前一篇文章讨论了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学习(二)的更多相关文章
- [转载]Linux 内核list_head 学习(一)
		
在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head.虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作li ...
 - Linux 内核list_head 学习
		
Linux 内核list_head 学习(一) http://www.cnblogs.com/zhuyp1015/archive/2012/06/02/2532240.html 在Linux内核中,提 ...
 - Linux内核分析(二)----内核模块简介|简单内核模块实现
		
原文:Linux内核分析(二)----内核模块简介|简单内核模块实现 Linux内核分析(二) 昨天我们开始了内核的分析,网上有很多人是用用源码直接分析,这样造成的问题是,大家觉得很枯燥很难理解,从某 ...
 - Linux内核驱动学习(八)GPIO驱动模拟输出PWM
		
文章目录 前言 原理图 IO模拟输出PWM 设备树 驱动端 调试信息 实验结果 附录 前言 上一篇的学习中介绍了如何在用户空间直接操作GPIO,并写了一个脚本可以产生PWM.本篇的学习会将写一个驱动操 ...
 - linux内核分析实践二学习笔记
		
Linux实践二--内核模块的编译 标签(空格分隔): 20135328陈都 理解内核的作用 Linux内核[kernel]是整个操作系统的最底层,它负责整个硬件的驱动,以及提供各种系统所需的核心功能 ...
 - Linux内核驱动学习(二)添加自定义菜单到内核源码menuconfig
		
文章目录 目标 drivers/Kconfig demo下的Kconfig 和 Makefile Kconfig Makefile demo_gpio.c 目标 Kernel:Linux 4.4 我编 ...
 - “Linux内核分析”实验二报告
		
张文俊 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.第二周学习内 ...
 - linux内核数据结构学习总结
		
目录 . 进程相关数据结构 ) struct task_struct ) struct cred ) struct pid_link ) struct pid ) struct signal_stru ...
 - Linux 内核协议栈 学习资料
		
终极资料 1.<Understanding Linux Network Internals> 2.<TCP/IP Architecture, Design and Implement ...
 
随机推荐
- avaweb学习总结(八)——HttpServletResponse对象(二)
			
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,
 - Ubuntu无法使用root用户登陆的解决办法
			
对于一个新安装的Ubuntu操作系统,经常会遇到无法使用root用户ssh连接操作系统,比如说root用户的密码被拒绝等原因. 面对这种问题,介绍一下Ubuntu无法使用root登陆解决 1. 修改 ...
 - ActiveMQ部署和503的错误
			
最近部署ActiveMQ的时候,发现有的服务器可以打开后台管理网址,有的服务器无法打开,Jetty报503 Service Unavailable. 搞了很久终于发现了问题,现将部署和解决过程做笔记如 ...
 - spring boot 基础学习
			
构建微服务:Spring boot 入门篇 http://www.cnblogs.com/ityouknow/p/5662753.html SpringBoot入门系列:第一篇 Hello World ...
 - HTML5 新元素之VIDEO标签的js操作
			
本文参考w3school的HTML DOM Video 对象. Video 对象属性 属性 描述 audioTracks 返回表示可用音频轨道的 AudioTrackList 对象. autoplay ...
 - mysql数据库优化课程---1、数据库的本质是什么
			
mysql数据库优化课程---1.数据库的本质是什么 一.总结 一句话总结: 文件夹-文件:一个数据库其实就是一个的文件夹,数据库里面的表就是文件夹里的一个或者多个文件(根据数据库引擎不同而不同,My ...
 - php的http数据传输get/post...
			
php的http数据传输get/post... 一般有:file_get_contents,curl,fsockopen.... 下面介绍fsockopen: //构造要post的字符串 $argv ...
 - if 条件判断 和 判断总结---shell脚本
			
本文主要介绍了Shell脚本IF条件判断和判断条件总结,本文先是给出了IF条件判断的语法,然后给出了常用的判断条件总结,需要的朋友可以参考下. 前言: 无论什么编程语言都离不开条件判断.SHELL也不 ...
 - web自动化:前端页面组成
			
一.web页面的组成 1. 常用:HTML + CSS + Javascript 2. HTML:定义页面的呈现内容 3. CSS:Cascading Style Sheets,层叠样式表 控制你的网 ...
 - Sublime Text 3 (含:配置 C# 编译环境)
			
Sublime Text 3http://www.sublimetext.com/3http://www.sublimetext.com/3dev 1. 关闭自动更新 菜单:Preferences ...