Chinaunix首页 | 论坛 | 博客
  • 博客访问: 690486
  • 博文数量: 192
  • 博客积分: 1875
  • 博客等级: 上尉
  • 技术积分: 2177
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 23:21
个人简介

有时候,就是想窥视一下不知道的东东,因为好奇!

文章分类

全部博文(192)

文章存档

2024年(8)

2023年(3)

2020年(1)

2019年(1)

2018年(1)

2017年(2)

2016年(69)

2015年(53)

2014年(14)

2013年(1)

2012年(5)

2011年(25)

2010年(9)

分类: LINUX

2012-03-10 18:00:02

内核中的list_head连接件使用备忘。
从链表头、链表尾插入节点,从头到尾遍历结点,从尾到头遍历结点,删除节点,遍历并立即删除节点,判断节点是否空。
 
系统环境:ubuntu-10.04/linux-2.6.32-38-generic/gcc version 4.3.4(Ubuntu 4.3.4 10ubuntu1)
代码:
myklist.c

点击(此处)折叠或打开

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/list.h>

  6. MODULE_LICENSE("GPL");
  7. MODULE_AUTHOR("zl");
  8. MODULE_DESCRIPTION("klist test");

  9. struct list_head klisthead;

  10. struct mytype {
  11.     char *keystring;
  12.     struct list_head knode;
  13. };

  14. typedef int (*cmp_t)(void *arg1, void *arg2);

  15. char *array[] = {
  16.     "aaaaa",
  17.     "bbb",
  18.     "333",
  19.     "c",
  20.     "dddd",
  21.     "111",
  22.     "eeeeeeeee",
  23.     "ffffghi",
  24.     "222",
  25.     "gggabcd",
  26.     "13579"
  27.     "abcdefg"
  28. };

  29. #define TAB_SIZE(array) (sizeof(array)/sizeof(array[0]))

  30. static int __init klist_init(void)
  31. {
  32.     int i;
  33.     struct mytype *node;
  34.     struct list_head *tmp;
  35.     struct list_head *pos;
  36.     INIT_LIST_HEAD(&klisthead);

  37. /** first time list_add/list_del/list_for_each/list_for_each_safe **/
  38.     printk("\n********list_add*************************************\n");
  39.     if(list_empty(&klisthead))
  40.     {
  41.         printk("[%s-%d]: The list have no node !\n", __func__, __LINE__);
  42.     }
  43.     printk("----------list_for_each------------------------------\n");
  44.     for(i = 0; i < TAB_SIZE(array); i++)
  45.     {
  46.         node = kzalloc(sizeof(struct mytype), GFP_KERNEL);
  47.         if(node == NULL)
  48.         {
  49.             printk("[%s-%d]kzalloc failed !\n", __func__, __LINE__);
  50.             goto err1;
  51.         }
  52.         node->keystring = array[i];

  53.         list_add(&(node->knode), &klisthead);
  54.     }
  55.     
  56.     if(!list_empty(&klisthead))
  57.     {
  58.         printk("[%s-%d]: The list have some node !\n", __func__, __LINE__);
  59.     }

  60.     i = 0;
  61.     list_for_each(pos, &klisthead)
  62.     {
  63.         node = list_entry(pos, struct mytype, knode);
  64.         printk("i = %d, keystring = %s \n", i, node->keystring);
  65.         i++;
  66.     }

  67.     printk("\n----------list_for_each_safe/list_del----------------\n");
  68.     i = 0;
  69.     list_for_each_safe(pos, tmp, &klisthead)
  70.     {
  71.         node = list_entry(pos, struct mytype, knode);
  72.         printk("i = %d, keystring = %s \n", i, node->keystring);
  73.         i++;
  74.         list_del(&(node->knode));
  75.         kfree(node);
  76.     }

  77.     if(list_empty(&klisthead))
  78.     {
  79.         printk("[%s-%d]: The list have no node !\n", __func__, __LINE__);
  80.     }
  81. /** second time list_add_tail/list_del_init/list_for_each_prev/list_for_each_prev_safe **/
  82.     printk("\n********list_add_tail********************************\n");
  83.     printk("----------list_for_each------------------------------\n");
  84.     for(i = 0; i < TAB_SIZE(array); i++)
  85.     {
  86.         node = kzalloc(sizeof(struct mytype), GFP_KERNEL);
  87.         if(node == NULL)
  88.         {
  89.             printk("[%s-%d]kzalloc failed !\n", __func__, __LINE__);
  90.             goto err1;
  91.         }
  92.         node->keystring = array[i];

  93.         //list_add(&(node->knode), &klisthead);
  94.         list_add_tail(&(node->knode), &klisthead);
  95.     }
  96.     if(!list_empty(&klisthead))
  97.     {
  98.         printk("[%s-%d]: The list have some node !\n", __func__, __LINE__);
  99.     }

  100.     i = 0;
  101.     list_for_each(pos, &klisthead)
  102.     {
  103.         node = list_entry(pos, struct mytype, knode);
  104.         printk("i = %d, keystring = %s \n", i, node->keystring);
  105.         i++;
  106.     }

  107.     printk("----------list_for_each_prev-------------------------\n");
  108.     i = 0;
  109.     list_for_each_prev(pos, &klisthead)
  110.     {
  111.         node = list_entry(pos, struct mytype, knode);
  112.         printk("i = %d, keystring = %s \n", i, node->keystring);
  113.         i++;
  114.     }

  115.     printk("----------list_for_each_prev_safe/list_del_init------\n");
  116.     i = 0;
  117.     list_for_each_prev_safe(pos, tmp, &klisthead)
  118.     {
  119.         node = list_entry(pos, struct mytype, knode);
  120.         printk("i = %d, keystring = %s \n", i, node->keystring);
  121.         i++;
  122.         list_del_init(&(node->knode));
  123.         kfree(node);
  124.     }
  125.     if(list_empty(&klisthead))
  126.     {
  127.         printk("[%s-%d]: The list have no node !\n", __func__, __LINE__);
  128.     }
  129.     return 0;
  130. err1:
  131.     return -1;
  132. }

  133. static void __exit klist_exit(void)
  134. {
  135.     printk("exit !\n");
  136. }

  137. module_init(klist_init);
  138. module_exit(klist_exit);
makefile文件

点击(此处)折叠或打开

  1. obj-m = myklist.o
  2. #KERNELS = /home/zl/linux-2.6.30.4
  3. #KERNELS = /media/STUDY/linux/kernel/my2440-2.6.36
  4. KERNELS = /lib/modules/$(shell uname -r)/build/
  5. default:
  6. make -C $(KERNELS) M=$(shell pwd) modules
  7. .PHONY:clean
  8. clean:
  9. make -C $(KERNELS) M=$(shell pwd) clean

编译,运行,及查看运行结果:符合预期

make clean;make;sudo insmod myklist.ko;sudo rmmod myklist;tail -n200 /var/log/messages

阅读(1483) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~