使用seq_file,实现大文件的/proc file

下面是个用seq_file实现大文件/proc file的例子。 重要的就是那几个迭代的函数。

值得一提的是,我这个例子在cat /proc/seq_test的时候 会显示出很多东西。也就是比我想象的要多。

后来我仔细的看了log,发现输出如下

Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426472] scull: position is 0
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426479] scull: next: 0
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426482] scull: next: 50
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426484] scull: next: 100
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426486] scull: next: 150
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426488] scull: next: 200
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426490] scull: next: 250
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426492] scull: next: 300
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426494] scull: next: 350
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426516] scull: position is 351
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426518] scull: next: 351
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426522] scull: position is 352
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426524] scull: next: 352
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426527] scull: position is 353
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426529] scull: next: 353
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426532] scull: position is 354
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426534] scull: next: 354
Nov 21 15:44:14 wizard-desktop kernel: [ 9354.426538] scull: position is 355
Nov 21 15:44:14 wizard-desktop ke

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#include <linux/module.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/lockdep.h> static int lockdep_enabled = 1; static int lockdep_proc_show(struct seq_file *m, void *v) { seq_printf(m, "%d\n", lockdep_enabled); return 0; } static ssize_t lockdep_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { char buf[32]; if (count > sizeof(buf) - 1) return -EINVAL; if (copy_from_user(buf, buffer, count)) return -EFAULT; buf[count] = '\0'; if (strcmp(buf, "0\n") == 0) { lockdep_enabled = 0; pr_info("lockdep disabled\n"); } else if (strcmp(buf, "1\n") == 0) { lockdep_enabled = 1; pr_info("lockdep enabled\n"); } else { return -EINVAL; } return count; } static int lockdep_proc_open(struct inode *inode, struct file *file) { return single_open(file, lockdep_proc_show, NULL); } static const struct file_operations lockdep_proc_fops = { .owner = THIS_MODULE, .open = lockdep_proc_open, .read = seq_read, .write = lockdep_proc_write, .llseek = seq_lseek, .release = single_release, }; static int __init lockdep_init(void) { struct proc_dir_entry *entry = proc_create("lockdep", 0666, NULL, &lockdep_proc_fops); if (!entry) { pr_err("failed to create /proc/lockdep\n"); return -ENOMEM; } pr_info("lockdep module loaded\n"); return 0; } static void __exit lockdep_exit(void) { remove_proc_entry("lockdep", NULL); pr_info("lockdep module unloaded\n"); } module_init(lockdep_init); module_exit(lockdep_exit); MODULE_LICENSE("GPL");这个程序哪里实现了对lockdep工具的控制
05-22
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值