使用seq_file
在《使用procfs》一文的源码示例中有说到proc文件系统每次读取的数据只能是1个页,如果超过则需多次读取,这样的话会增加读取次数,增多系统调用次数,影响了整体的效率,故而才有seq file序列文件的出现,该项功能使得内核对于大文件的读取更加容易。 对于seq file,其结构体定义在include/linux/seq_file.h文件中,内容如下: struct seq_file { char *buf; size_t size; size_t from; size_t count; size_t pad_until; loff_t index; loff_t read_pos; u64 version; struct mutex lock; const struct seq_operations *op; int poll_event; #ifdef CONFIG_USER_NS struct user_namespace *user_ns; #endif void *private; }; 这里不详细说明该结构体,仅供感受下。 对于seq_file,相应的文件操作需要实现seq_operations结构体的成员,该结构体在同一头文件中有如下定义: struct seq_operations { void * (*start) (struct seq_file *m, loff_t *pos); void (*stop) (struct seq_file *m, void *v); void * (*next) (struct seq_file *m, void *v, loff_t *pos); int (*show) (struct seq_file *m, void *v); }; 实现该结构体成员后,需要使用seq_open打开,让文件与该seq_operations关联起来,最终这个open函数要作为file_oper...