c - 如何在procfs中执行顺序读取?

标签 c linux linux-kernel linux-device-driver procfs

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/jiffies.h>
#include <linux/seq_file.h>

//extern uint64_t interrupt_time;

static struct proc_dir_entry *test_dir;

static int my_proc_show(struct seq_file *m, void *v)
{
    seq_printf(m, "%lu\n", jiffies);
    //seq_printf(m, "%lu", interrupt_time);
    return 0;
}

static int my_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, my_proc_show, NULL);
}

static const struct file_operations tst_fops = {
    .open       = my_proc_open,
    .read       = seq_read,
    .llseek     = seq_lseek,
    .release    = single_release,
};

static int __init test_init(void)
{
    test_dir = proc_mkdir("myproc", NULL);

    if (test_dir)
            proc_create("jiffies", 0, test_dir, &tst_fops);

    return 0;
}
static void __exit test_exit(void)
{
    remove_proc_entry ("jiffies", test_dir);
    proc_remove (test_dir);
}
module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Test");

上面的代码是procfs驱动的,上面的代码包含了init函数,exit函数,文件操作函数,但是如何创建一个从内核到用户的seq_read()函数。其 API 是什么?

这是我在/linuxversion/net/core/dev.c中修改的代码

int netif_rx(struct sk_buff *skb) 
{
  skb->tstamp = ktime_get_real();   //this will give a timestamp and it will be stored in //skb buffer
  //I am calculating a timestamp here. because whenever kernel receive the data then the kernel is 
  //interrupted and start executing the newly arrived task but I have to read the time when the 
 //interrupt  occurs and get the value of it.
} 

我的问题是:如何将此时间戳复制到procfs

最佳答案

我不确定您的问题是如何在 /proc 中创建和填充条目,还是如何从现有条目中读取条目。关于后者:

how to read it to the user application

从用户程序中打开 /proc/foo/bar 并从中读取,就像从任何其他文件中一样。

关于c - 如何在procfs中执行顺序读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23193078/

相关文章:

c - 为什么给指定的整数提供 float 格式说明符会导致 0.000000?

C 指针 - 很好的教程

基于 Linux 内核头文件中功能的条件编译

c - 这个 Project Euler #5 解决方案是否比已知解决方案更有效?

c - Sparc 函数编译对齐

具有最小配置的 Linux 备份目标

linux - 将多个shell脚本集成到一个脚本中

linux - "find"和 "ls"与 GNU 并行

c - Linux 如何知道要调用哪个 ioctl 函数?

c - malloc() 32 位机器上的 5GB 内存