一、實踐原理
- Linux模塊是一些可以作為獨立程序來編譯的函數和數據類型的集合。之所以提供模塊機制,是因為Linux本身是一個單內核。單內核由於所有內容都集成在一起,效率很高,但可擴展性和可維護性相對較差,模塊機制可彌補這一缺陷。
- Linux模塊可以通過靜態或動態的方法加載到內核空間,靜態加載是指在內核啟動過程中加載;動態加載是指在內核運行的過程中隨時加載。
- 一個模塊被加載到內核中時,就成為內核代碼的一部分。模塊加載入系統時,系統修改內核中的符號表,將新加載的模塊提供的資源和符號添加到內核符號表中,以便模塊間的通信。
二、實踐過程
1.編寫模塊代碼

2.編寫makefile
在此之前先確定本機具有什麼內核型號

然後使用uname指令查看本機的內核是什麼型號

然後根據剛才寫的c文件和內核號編寫makefile

3、加載、測試、卸載模塊
使用insmod指令加載模塊並使用lsmod指令來查看當前已經加載了的模塊

使用dmesg來查看測試模塊的狀態

最後使用rmmod來刪除模塊
