1、软件打开的文件在内核中如何表示
软件在打开文件时,可以得到一个整数,这个整数被称为文件句柄。对于APP的每一个文件句柄,在内核里面都有一个“struct file”与之对应。
在使用open打开文件时,传入的flags、mode等参数会被记录在内核中对应的struct file结构体里(int flags、mode_t mode ):
int open(const char *pathname, int flags, mode_t mode);
2、file_operations 结构体介绍
file_operations 结构体也在struct file中。
结构体struct file_operations的定义如下:
3、字符设备驱动框架
4、驱动程序编写步骤
(1)、确定主设备号,也可以让内核进行分配。
(2)、构造file_operations结构体
(3)、注册:register_chrdev
(4)、编写入口函数
(5)、编写出口函数
5、chardevs的结构图
6、class_create/device_create
驱动程序的核心是file_operations结构体:分配、设置、注册。class_create/device_create只起到辅助作用。
Class_create表示在”/sys/class“”目录下创建一个子目录“hello_class”
Device_create表示在“/sys/class/hello_class”目录下创建一个文件“myhello”