歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 內核鏈表和普通鏈表的區別

內核鏈表和普通鏈表的區別

日期:2017/3/3 10:58:48   编辑:Linux技術

轉載自:http://blog.csdn.net/linux_wgl/article/details/7308133

內核鏈表和普通鏈表的區別

內核鏈表是一個雙向鏈表,但是與普通的雙向鏈表又有所區別。內核鏈表中的鏈表元素不與特定類型相關,具有通用性。

我們先來看一幅圖

kernel list展示的是內核鏈表的結構,normallist展示的是普通鏈表的結構。head是鏈表頭,p1,p2,p3是鏈表節點。從圖中可以看出普通鏈表的p1的next指針是指向的結構體p2的地址,p2的pre指針指向p1結構體的地址。而內核鏈表的p1的next指向的是p2結構體中包含pre和next部分的地址,的p2的pre指向的是p1結構體中包含pre和next部分的地址。依此類推,這就是區別。內核結構元素不與特定類型結構相關,任何結構體都可通過內核的添加成為鏈表中的節點。

#include <linux/kernel.h>

#include <linux/module.h>

#include <linux/init.h>

#include <linux/slab.h>

#include <linux/list.h>

MODULE_LICENSE("GPL");

MODULE_AUTHOR("David Xie");

MODULE_DESCRIPTION("ListModule");

MODULE_ALIAS("List module");

struct student

{

char name[100];

int num;

struct list_head list;

};

struct student *pstudent;

struct student *tmp_student;

struct list_head student_list;

struct list_head *pos;

int mylist_init()

{

inti = 0;

INIT_LIST_HEAD(&student_list);

pstudent= kmalloc(sizeof(struct student)*5,GFP_KERNEL);

memset(pstudent,0,sizeof(structstudent)*5);

for(i=0;i<5;i++)

{

sprintf(pstudent[i].name,"Student%d",i+1);

pstudent[i].num= i+1;

list_add(&(pstudent[i].list), &student_list);

}

list_for_each(pos,&student_list)

{

tmp_student= list_entry(pos,struct student,list);

printk("<0>student%d name: %s\n",tmp_student->num,tmp_student->name);

}

return0;

}

void mylist_exit()

{

inti ;

for(i=0;i<5;i++)

{

list_del(&(pstudent[i].list));

}

kfree(pstudent);

}

module_init(mylist_init);

module_exit(mylist_exit);

Copyright © Linux教程網 All Rights Reserved