乙個最簡單的Linux核心模組

2021-10-08 20:50:07 字數 999 閱讀 6591

#include #include static int __init hello_init(void)

static void __exit hello_exit()

module_init(hello_init);

module_exit(hello_exit);

module_license("gpl");

這個最簡單的核心模組只包含核心模組載入函式、解除安裝函式和許可許可權的宣告。

下面需要編寫對應的makefile檔案,生成對應的.ko檔案

kern_dir = /home/fitz/home/kernel/linux-2.6.22.6

all:

make -c $(kern_dir) m=`pwd` modules

clean:

make -c $(kern_dir) m=`pwd` modules clean

rm -rf modules.order

obj-m += hello.o

/home/fitz/home/kernel/linux-2.6.22.6是對應linux核心的路徑,需要注意的是,這裡的核心必須成功經過編譯。

輸入make命令即可生成對應的.ko檔案

將hello.ko檔案上傳到單板系統,輸入

insmod hello.ko載入模組,rmmod hello.ko解除安裝模組,可以看到對應的列印。

核心模組中用於輸出的函式是核心空間的printk()而非使用者空間的printf(),printk()的用法和printf()的用法基本相似,前者可定義輸出級別。printk()可作為一種最基本的核心除錯手段。

最簡單的核心模組例子

include include include static int init hello init void static void exit hello exit void module init hello init module exit hello exit static int init...

乙個簡單的linux核心驅動

一,核心結構簡單概述 上層程式操作裝置驅動簡單概述 在使用者空間使用相關的c庫,比如open函式會造成乙個中斷,系統會去呼叫sys call函式 系統呼叫 然後會去呼叫相關的sys open函式,在核心空間的時候會去驅動鏈表裡面查詢對應的外設驅動,我們編寫完驅動程式,載入到核心,核心會去呼叫相關的驅...

Linux 寫乙個簡單的模組

1.建立乙個目錄 mkdir zhu cd zhu vim hello.c 編寫乙個名為hello的檔案 2.hello.c include 所有模組都需要的標頭檔案 include static int hello init void static void hello exit module i...