标签:file his operation opera 卸载 const 结构体 文件 write
#include <linux/init.h> //定义了module_init
#include <linux/module.h> //最基本的头文件,其中定义了MODULE_LICENSE这一类宏
#include <linux/fs.h> // file_operations结构体定义在该头文件中
static int first_drv_open (struct inode *inode, struct file *file)
{
printk("open\n");
return 0;
}
static ssize_t first_drv_write (struct file *file, const char __user *buf, size_t size, loff_t * ppos)
{
printk("write\n");
return 0;
}
static struct file_operations first_drv_fo =
{
.owner = THIS_MODULE,
//将对应的函数关联在file_operations的结构体中
.open = first_drv_open,
.write = first_drv_write,
};
static int first_drv_init(void)
{
//注册设备,实际是将file_operations结构体放到内核的制定数组中,以便管理
//在register_chrdev中制定major作为主设备号
register_chrdev(120, "first_driver" , &first_drv_fo);
printk("init\n");
return 0;
}
static void first_drv_exit(void)
{
printk("exit\n");
unregister_chrdev(120, "first_driver"); //卸载设备,实际是将file_operations结构体从内核维护的相关数组中以主设备号作为索引删除
}
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
参考:https://www.jianshu.com/p/716ed9cdb8f3
标签:file his operation opera 卸载 const 结构体 文件 write
原文地址:https://www.cnblogs.com/Malphite/p/10436225.html