Linux裝置驅動程式初探示例

2021-06-26 12:12:29 字數 938 閱讀 3617

本示例為在linux裝置驅動程式中文版第三版中摘錄出來的示例程式:

程式原始碼(hello.c):

#include #include #include // 告知核心該模組帶有乙個自由許可證

module_license("dual bsd/gpl");

// 模組載入時被呼叫

static int __init hello_init(void)

// 模組解除安裝時被呼叫

static void __exit hello_exit(void)

// 註冊模組初始化函式

module_init(hello_init);

// 註冊模組清理函式

module_exit(hello_exit);

makefile檔案:

#表示模組從hello.o這個目標檔案建立的,之後建立的模組名將會被命名為hello.ko

obj-m := hello.o

kerneldir := /lib/modules/$(shell uname -r)/build

pwd := $(shell pwd)

all:

$(make) -c $(kerneldir) subdirs=$(pwd) modules

編譯步驟:

1.在終端進入原始檔的目錄執行make,生成ko檔案

2.執行insmod hello.ko命令載入模組,檢視/var/log目錄檢視messages檔案可看到

hello, world

3.執行rmmod hello命令解除安裝模組,檢視/var/log目錄檢視messages檔案可看到

goodbye, cruel world

linux裝置驅動程式 字元裝置驅動程式

先留個 有一起學習驅動程式的加qq295699450 字元裝置驅動 這篇比較惱火。載入成功,但是讀不出來資料,有知道怎麼回事的,留個言,一起討論下 資料結構 struct scull mem struct scull dev dev 整個驅動程式 如下 include include include...

Linux裝置驅動程式 字元裝置驅動程式

1.檢視主裝置號,次裝置號 進入 dev目錄執行ls l,第四,五列分別為主次裝置號,10,180,1,5,這些是主裝置號,而60,63這些就是次裝置號 130 shell android dev ls l crw rw r system radio 10,60 1969 12 31 21 00 a...

Linux裝置驅動程式

linux系統中的裝置分為字元裝置 char device 塊裝置 block 和網路裝置 net device 三種,字元裝置是指在訪問時沒有快取,能夠像檔案一樣被訪問的裝置,字元裝置驅動程式至少要實現open close read和write系統呼叫。多數的linux裝置驅動程式可以在核心模組發...