linux list的应用实例

/*此篇文章是介绍的一个linux list的一个应用实例*/
 1 #include <linux/module.h>
 2 #include <linux/init.h>
 3 #include<linux/sched.h>
 4 #include<linux/sem.h>
 5 #include <linux/slab.h>
 6 #include <linux/list.h>
 7 
 8 MODULE_LICENSE("Dual BSD/GPL");
 9 
     //定义用户结构类型group
10 struct group{
11     int id;
12     char character;
13     struct    list_head member;//定义linux中通用链表头结点
14 }; 
15 //我们可以简单的理解list_head头结点是linux提供我们,用来实现由结构group的组成的链表
16 
17 int list_head_init(void)
18 {
19     printk("list_head init\n");
20 
         //定义结构体变量testhead
21     struct  group testhead={
22     .id=-1,                 //初始化成员变量
23     .character='a',     //初始化成员变量
24     .member=LIST_HEAD_INIT(testhead.member),  //调用linux提供的通用接口LIST_HEAD_INIT来初始化头结点member
25     };
26     
27      //插入10个元素
28     int i=0;
29     for(i=0;i<10;i++)                            
30     {
31         struct group *testtemp;
32         testtemp=kmalloc(sizeof(struct group),GFP_KERNEL);
33         if(testtemp==NULL)
                     return -1;

34         testtemp->id=i;
35         testtemp->character=(char)(64+i);
36         list_add(&testtemp->member,&testhead.member);
37          //printk(KERN_ALERT"%d--->%c\n",testtemp->intdata,testtemp->chardata);
38     }
39 
40 
41     //遍历10个元素    
42     struct group *groups,*p;
43           struct list_head *pos;
44           int count=0;
45           printk(KERN_ALERT"Hello World enter begin:\n");
46           groups=&testhead;
47           list_for_each(pos,&groups->member)
48                {
49                p=list_entry(pos, struct group, member);
50                count++;
51                printk(KERN_ALERT"id is %d---> name %c\n",p->id,p->character);
52                }
53           printk(KERN_ALERT"the member of group is:%d\n",count);
54 
55     //删除元素
56     list_del(&p->member);
57     count=0;
58 
59     //遍历剩余元素
60     list_for_each(pos,&groups->member)
61                {
62                p=list_entry(pos, struct group, member);
63                count++;
64                printk(KERN_ALERT"id is %d---> name %c\n",p->id,p->character);
65                }
66           printk(KERN_ALERT"the member of group is:%d\n",count);
67     
68     return 0;
69 }
70 static void list_head_exit(void)
71 {
72     printk("list_head exit\n");
73 }
74 
75 module_init(list_head_init);
76 module_exit(list_head_exit);
77  
复制代码

Makefile :

复制代码
1 obj-m := list_head.o
2 KERNELDIR := /lib/modules/$(shell uname -r)/build
3 PWD := $(shell pwd)
4 modules:
5     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
6 modules_install:
7     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
8 clean:
9     rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)
复制代码

运行命令:

1 root@lab:sudo su
2 root@lab:make
3 root@lab:insmod list_head.ko
4 root@lab:dmesg
5 root@lab:rmmod  list_head.ko



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值