你好,我是内核空间编程的新手,我是来学习 proc 文件系统的。
我写了一个在注册时创建一个proc文件的模块
我在注销模块时删除了它。 但是,如果我不使用 remove_proc_entry() 删除该文件并取消注册我的模块,那么系统会在一段时间后挂起
为什么会这样?
代码是
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/proc_fs.h>
#include<linux/module.h>
#include<linux/fs.h>
int myopen(struct inode *, struct file *);
ssize_t myread(struct file *, char __user *, size_t, loff_t *);
int myopen(struct inode *p, struct file *q)
{
printk(KERN_ALERT "I am in myopen\n");
return 0;
}
ssize_t myread(struct file *p, char __user *q, size_t r, loff_t *s)
{
printk(KERN_ALERT "I am in myread\n");
return 0;
}
static const struct file_operations fs={
.open=myopen,
.read=myread
};
int start(void)
{
proc_create("myprocfile", 0, NULL, &fs);
return 0;
}
void stop(void)
{
//remove_proc_entry("myprocfile", NULL);
/*
If I uncomment the above line
then everything works fine
*/
}
module_init(start);
module_exit(stop);
MODULE_LICENSE("GPL");
最佳答案
当文件被访问时,file_operations
结构被读取,进一步的操作依赖于指针,存储在这个结构中。
如果您在模块退出时不删除文件,文件仍然可以访问。但是与其关联的 file_operations
结构被释放。因此,在模块卸载后对文件的任何访问都会导致读取已释放的数据。此触发内存访问错误。
这就是为什么内核模块删除它之前创建的所有对象是至关重要的。
关于c -/proc 文件系统挂起我的系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33393319/