c - 在也使用字符设备的内核模块中写入 proc

标签 c linux timer linux-kernel

我有一个内核模块,它实现了一个计时器并且它可以工作。同时,我能够创建一个新的内核模块来写入和读取 proc。我不明白的是如何在同一个内核模块中组合这两个操作。

我的应用程序就是这样工作的。用户程序向内核模块写入一个数字 n用于创建将在 n 到期的计时器毫秒。为此,我实现了 writeread功能,我将它们链接到 struct file_operations我在 init 中使用功能来注册我的字符设备(定时器)。

现在为 proc文件我需要声明一个writeread函数以及它应该处理来自用户程序的请求。这让我感到困惑,我无法理解如何将所有内容组合在一起。

最佳答案

正如 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/

相关文章:

linux - Mailx 和 Gmail nss 配置目录

Java如何不断重绘

c - C语言中如何正确使用指针?

C - 访问结构中的结构

C++ `assign()` 等价于 C

c - linux 和 windows 速度不匹配

C 函数在另一个函数中没有返回正确的值

linux - 用监听套接字 fork

android - 当计时器在每一行内运行时如何提高列表性能

安卓 2.2 : How to update textviews automatically with a timer?