• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-145  評論-173  文章-70  trackbacks-0
            • Hello,world:

              1.編寫hello.c文件如下:

              #include "/usr/src/linux-2.6.31.12/include/linux/kernel.h"
              #include "/usr/src/linux-2.6.31.12/include/config/modules.h"

              #if CONFIG_MODVERSIONS == 1
              #define MODVERSIONS
              #include "/usr/src/linux-2.6.31.12/include/linux/version.h"
              #endif

              int init_module()
              {
                printk("Hello,world!\n");
                printk("I am running in a kernel mod!\n");
                return 0;
              }

              void cleanup_module()
              {
                printk("I will shut down myself in kernel mod!\n");
              }

              2.編寫Makefile如下:

              # If KERNELRELEASE is defined, we've been invoked from the
              # kernel build system and can use its language.
              ifeq ($(KERNELRELEASE),)

                  # Assume the source tree is where the running kernel was built
                  # You should set KERNELDIR in the environment if it's elsewhere
                  KERNELDIR ?= /lib/modules/$(shell uname -r)/build
                  # The current directory is passed to sub-makes as argument
                  PWD := $(shell pwd)

              modules:
                  $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

              modules_install:
                  $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

              clean:
                  rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

              .PHONY: modules modules_install clean

              else
                  # called from kernel build system: just declare what our modules are
                  obj-m := hello.o
              endif


              3.在命令行下輸入: insmod ./hello.ko(注:此文件即生成的文件)
              然后進入到/var/log/messages下面打開,看到最后顯示的是:
              Mar  6 19:03:42 liuchang-laptop kernel: [ 8472.343068] Hello,world!
              Mar  6 19:03:42 liuchang-laptop kernel: [ 8472.343074] I am running in a kernel mod!
              同時,使用命令查看mod: lsmod,會發現此時模塊就又hello
              如下:
              root@liuchang-laptop:/home/liuchang/桌面/test3# lsmod
              Module                  Size  Used by
              hello                   1052  0
              nls_iso8859_1           3740  0
              nls_cp437               5372  0
              vfat                   10716  0
              fat                    51452  1 vfat
              usb_storage            52544  0


              4.在命令行下輸入: rmmod ./hello.ko
              刪除該模塊,發現/var/log/messages文件中的內容最后是:
              Mar  6 19:09:02 liuchang-laptop kernel: [ 8791.665905] I will shut down myself in kernel mod!
              此時再觀察模塊,內容為:lsmod
              nls_iso8859_1           3740  0
              nls_cp437               5372  0
              vfat                   10716  0
              fat                    51452  1 vfat
              usb_storage            52544  0
              binfmt_misc             8356  1
              ppdev                   6688  0
              snd_hda_codec_nvhdmi     4828  1
              不再含有hello說明該模塊被刪除了。

            • 至此,表示該模塊能夠正確的掛載和卸載。剩下來的問題,就是如何實現字符設備的一個具體的應用了。
            posted on 2010-03-06 22:29 deercoder 閱讀(1019) 評論(2)  編輯 收藏 引用 所屬分類: Unix/Linux

            評論:
            # re: Linux驅動程序開發-------模塊程序設計(Ubuntu 9.10---2.6.31.14) 2010-04-21 22:31 | zxj
            我想問下 我也是裝的9.10 怎么按照你這樣的程序會有好多文件不能被包含呢,原因就跟沒有建立內核樹一樣?  回復  更多評論
              
            # re: Linux驅動程序開發-------模塊程序設計(Ubuntu 9.10---2.6.31.14) 2010-04-23 16:27 | 劉暢
            @zxj
            一般來說上面的文件信息應該足夠,對于一個簡單的功能來說應該沒有問題。確認下你的路徑是否正確,然后看看代碼的問題。  回復  更多評論
              
            久久久久AV综合网成人 | 伊人久久综合无码成人网| 午夜精品久久久久成人| AV无码久久久久不卡蜜桃| 久久99精品久久久久久hb无码| 欧美伊香蕉久久综合类网站| 久久996热精品xxxx| 久久人妻少妇嫩草AV无码专区| 青青草国产精品久久| 久久久无码精品亚洲日韩蜜臀浪潮| 久久久免费精品re6| 伊人色综合久久天天人守人婷| 性欧美大战久久久久久久久| 久久99精品国产麻豆蜜芽| 久久精品九九亚洲精品| 伊人久久大香线蕉无码麻豆| 国产福利电影一区二区三区久久老子无码午夜伦不| 国产午夜电影久久| 午夜不卡久久精品无码免费| 人妻中文久久久久| 国产福利电影一区二区三区,免费久久久久久久精 | 伊人色综合久久天天人手人婷 | 精品久久久一二三区| 久久99精品综合国产首页| 久久人妻无码中文字幕| 久久精品国产亚洲5555| 亚洲成色999久久网站| 无码精品久久久久久人妻中字| 欧美久久一级内射wwwwww.| 一本久久久久久久| 久久久久久狠狠丁香| 俺来也俺去啦久久综合网| 婷婷伊人久久大香线蕉AV| 精品国产青草久久久久福利| 一级做a爰片久久毛片看看| 亚洲精品无码久久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 国产精品va久久久久久久| 99久久伊人精品综合观看| 久久久精品波多野结衣| 久久综合久久性久99毛片|