Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99498
  • 博文数量: 23
  • 博客积分: 516
  • 博客等级: 中士
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-29 16:45
文章分类
文章存档

2012年(11)

2011年(12)

分类: LINUX

2012-01-14 00:09:59

mylist.c

#include
#include
#include
#include
#include

MODULE_LICENSE("GPL");

#define mylist_for_each(pos, head) \
    for (pos = (head)->next; prefetch(pos->next), pos != (head); \
            pos = pos->next)


#define mylist_entry(ptr, type, member) \
    container_of(ptr, type, member)

struct student
{
    char name[10];
    int num;
    struct list_head list;
};

struct student *pstudent;
struct student *tmp_student;
struct list_head student_list;
struct list_head *pos;

static inline void INIT_MYLIST_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 mylist_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 mylist_del(struct list_head *entry)
{
    _list_del(entry->prev, entry->next);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
}


int mylist_init()
{
    int i = 0;
    INIT_MYLIST_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; 
    if(i<=3)
        mylist_add(&(pstudent[i].list), &student_list);
    else
        mylist_add(&(pstudent[i].list), &(pstudent[i-1].list));  
    }
  
    mylist_for_each(pos, &student_list)
    {
        tmp_student = mylist_entry(pos, struct student, list);  
        printk("student %d name: %s\n",tmp_student->num,
            tmp_student->name);  
    }

    return 0;
}

void mylist_exit()
{
    int i;

    for(i = 0; i < 5; i++)
    {
        mylist_del(&(pstudent[i].list));      
    }
    kfree(pstudent);
}

module_init(mylist_init);
module_exit(mylist_exit);


Makefile

ifneq ($(KERNELRELEASE),)
    obj-m := mylist.o
else
KERNELDIR:=/usr/src/linux-headers-2.6.32-33-generic
PWD:=$(shell pwd)
all:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
endif


result:
[ 7515.739790] student 4 name: student4
[ 7515.739791] student 5 name: student5
[ 7515.739792] student 3 name: student3
[ 7515.739793] student 2 name: student2
[ 7515.739795] student 1 name: student1

阅读(1282) | 评论(0) | 转发(0) |
0

上一篇:内核通知机制

下一篇:ubuntu testlink安装

给主人留下些什么吧!~~