我有一个内核模块,它实现了一个计时器并且它可以工作。同时,我能够创建一个新的内核模块来写入和读取 proc。我不明白的是如何在同一个内核模块中组合这两个操作。
我的应用程序就是这样工作的。用户程序向内核模块写入一个数字 n
用于创建将在 n
到期的计时器毫秒。为此,我实现了 write
和 read
功能,我将它们链接到 struct file_operations
我在 init
中使用功能来注册我的字符设备(定时器)。
现在为 proc
文件我需要声明一个write
和 read
函数以及它应该处理来自用户程序的请求。这让我感到困惑,我无法理解如何将所有内容组合在一起。
最佳答案
正如 Tsyvarev 提到的使用不同的文件操作
static struct proc_dir_entry *procfs;
static const struct file_operations proc_fops = {
.owner = THIS_MODULE,
.open = open_proc_fn,
.read = read_proc_fn,
};
static const struct file_operations char_fops = {
.owner = THIS_MODULE,
.open = open_char_fn,
.read = read_char_fn,
.write = write_char_fn,
};
int __init init_mod (void) {
procfs = proc_create("filename", 0, NULL, &proc_fops);
if(!proc)
return -1;
<Register char device with &char_fops >
return 0;
}
关于c - 在也使用字符设备的内核模块中写入 proc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858275/