zoukankan      html  css  js  c++  java
  • linux内核链表的使用

    linux内核链表:
    链表通常包括两个域:数据域和指针域。
    struct list_head{
    struct list_head *next,*prev;
    };
    include/linux/list.h中实现了一套精彩的链表数据结构。
    传统的链表指针指向下一个节点的头部。linux链表指针指向下一个指针list_head结构(*next),双向循环。不会随着外部数据的变化而变化,使它具有通用性。?

    -------------------------------------------------------------------

    linux内核提供的链表主要操作:
    1.初始化链表头:
    INIT_LIST_HEAD(list_head *head):将*head,*prev指向list本身。
    2.插入节点
    list_add(struct list_head *new,struct list_head *head)
    list_add_tail(struct list_head *new,struct list_head *head)
    3.删除节点
    list_del(struct list_head *entry:一个指针结构体)
    4.提取数据结构
    list_entry(ptr(指向list_head的指针),type(外部结构的类型),member(struct list_head对应的成员名))
    已知数据结构中的节点指针ptr,找出数据结构,例
    list_entry(aup,struct,autofs,list)
    5.遍历
    list_for_each(struct list_head *pos,struct list_head *head(你所需要遍历的链表的链表头))
    如:
    struct list_head *entry;
    struct list_head cs46xx_devs;//链表头
    list_for_each(entry,&cs46xx_devs){
    card = list_entry(entry,struct cs_card,list);
    if(card->dev_midi == minor)
    break;
    }
    看内核代码看list_entry是如何实现的???

    ----------------------------------------------------------

    内核链表的一个具体实现实例:

    #include<linux/list.h>

    #include<linux/module.h>

    #include<linux/kernel.h>

    #include<linux/init.h>

    #include<linux/slab.h>  //kfree kmalloc

    MODULE_LICENSE("GPL");//(general public license)
    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(){
    int i=0;
    INIT_LIST_HEAD(&student_list);
    pstudent=kmalloc(sizeof(struct student)*5,GFP_KERNEL);//分配空间。
    memset(pstudent,0,sizeof(struct student)*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 ",tmp_student->num,tmp_student->num,tmp_student->name);
    }
    return 0;
    }//遍历学生信息
    void mylist_exit()
    {
    int i;
    for(i=0;i<5;i++){
    list_del(&(pstudent[i].list));

    }
    kfree(pstudent);
    }
    module_init(mylist_init);
    module_exit(mylist_exit);

  • 相关阅读:
    set用法
    01分数规划
    unique && stl的全排列
    lower_bound() && upper_bound()
    spfa判负环
    倍增求LCA
    数据生成c++程序模板
    samba
    vsftp快速配置
    grub丢失的修复
  • 原文地址:https://www.cnblogs.com/defen/p/4728430.html
Copyright © 2011-2022 走看看