• (冯伟浩--吴晓培)链表内核模块运行与分析


    2010嵌入式作业---6---冯伟浩-----吴晓培

    [root@bogon I21c]# gedit lb.c

    #include
    #include
    #include
    #include

    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("XIYOU");

    #define N 10   //链表节点
    struct numlist {
        int num;//数据
        struct list_head list;//指向双联表前后节点的指针
    };

    struct numlist numhead;//头节点

    static int __init doublelist_init(void)
    {
        //初始化头节点
        struct numlist *listnode;//每次申请链表节点时所用的指针
        struct list_head *pos; //定义list_head的有效指针
        struct numlist *p;
        int i;

        printk("<0>""doublelist is starting... ");//利用"<0>"提升printk级别
        INIT_LIST_HEAD(&numhead.list);
        printk("初始化头节点");
        //建立N个节点,依次加入到链表当中
        for (i = 0; i < N; i++) {
            listnode = (struct numlist *)kmalloc(sizeof(struct numlist), GFP_KERNEL); // kmalloc()在内核空间申请内存,用于存放结点。
            listnode->num = i+1;//给链表numlist的数据域赋值。
            list_add_tail(&listnode->list, &numhead.list);//将结点加入到链表尾部
            printk("<0>""Node %d has added to the doublelist... ", i+1);
        }
        printk("<0>""建立N个节点,依次加入到链表当中");
        printk("<0>""遍历链表");
        //遍历链表
        i = 1;
        list_for_each(pos, &numhead.list) {
            p = list_entry(pos, struct numlist, list);
            printk("<0>""Node %d's data:%d ", i, p->num);
            i++;
        }
        return 0;
    }

    static void __exit doublelist_exit(void)
    {
        struct list_head *pos, *n;
        struct numlist *p;
        int i;
        printk("<0>""依次删除N个节点 ");
        //依次删除N个节点
        i = 1;
        list_for_each_safe(pos, n, &numhead.list) {  //为了安全删除节点而进行的遍历
            list_del(pos);//从双链表中删除当前节点
            p = list_entry(pos, struct numlist, list);//得到当前数据节点的首地址,即指针
            kfree(p);//释放该数据节点所占空间
            printk("<0>""Node %d has removed from the doublelist... ", i++);
        }
        printk("<0>""doublelist is exiting.. ");
    }

    module_init(doublelist_init);
    module_exit(doublelist_exit);


    [root@bogon I21c]# gedit Makefile
    obj-m:=lb.o
    PWD:=$(shell pwd)
    CUR_PATH:=$(shell uname -r)
    KERNEL_PATH:=/usr/src/kernels/$(CUR_PATH)

    all:
        make -C $(KERNEL_PATH) M=$(PWD) modules
    clean:
        make -C $(KERNEL_PATH) M=$(PWD) clean



    [root@bogon I21c]# gedit Makefile
    obj-m:=lb.o                //目标文件
    PWD:=$(shell pwd)      //当前shell的路径赋值给PWD
    CUR_PATH:=$(shell uname -r)    //shell执行 uname -r命令为2.6.32-71.el6.i686
    KERNEL_PATH:=/usr/src/kernels/$(CUR_PATH)    //KERNEL_PATH:的完整路径为/usr/src/kernels/2.6.32-71.el6.i686
    //此时Malefile调用2.6.32-71.el6.i686下的Makefile规则对目标文件进行编译。
    all:
        make -C $(KERNEL_PATH) M=$(PWD) modules
        //make -C /usr/src/kernels/2.6.32-71.el6.i686 M=/root/桌面/I21c modules
    clean:
        make -C $(KERNEL_PATH) M=$(PWD) clean





    [root@localhost I21c]# make
    make -C /usr/src/kernels/2.6.32-71.el6.i686 M=/root/桌面/I21c modules
    make[1]: Entering directory `/usr/src/kernels/2.6.32-71.el6.i686'
      CC [M]  /root/桌面/I21c/lb.o
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /root/桌面/I21c/lb.mod.o
      LD [M]  /root/桌面/I21c/lb.ko.unsigned
      NO SIGN [M] /root/桌面/I21c/lb.ko
    make[1]: Leaving directory `/usr/src/kernels/2.6.32-71.el6.i686'
    [root@localhost I21c]# insmod lb.ko
    [root@localhost I21c]#
    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:doublelist is starting...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 1 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 2 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 3 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 4 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 5 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 6 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 7 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 8 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 9 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 10 has added to the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:建立N个节点,依次加入到链表当中

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:遍历链表

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 1's data:1

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 2's data:2

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 3's data:3

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 4's data:4

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 5's data:5

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 6's data:6

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 7's data:7

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 8's data:8

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 9's data:9

    Message from syslogd@localhost at Oct  8 16:59:13 ...
     kernel:Node 10's data:10

    [root@localhost I21c]# rmmod lb.ko
    [root@localhost I21c]#
    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:依次删除N个节点

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 1 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 2 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 3 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 4 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 5 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 6 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 7 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 8 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 9 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:Node 10 has removed from the doublelist...

    Message from syslogd@localhost at Oct  8 16:59:29 ...
     kernel:doublelist is exiting..

    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(1050) | 评论(0) | 转发(2) |
    0

    上一篇:(实验)作业命名格式

    下一篇:fdisk mbr

    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    马云演讲:给自己一个梦想,给自己一个承诺,给自己一个坚持!
    转:如何成为一个伟大的开发者
    数据挖掘之七种常用的方法
    windows命令行
    100万亿意味着什么?
    ubuntu环境配置
    Ubuntu runlevel修改
    Ubuntu 用户及组管理
    Git学习笔记
    Git详解之三 Git分支
  • 原文地址:https://www.cnblogs.com/ztguang/p/12648178.html
Copyright © 2020-2023  润新知