proc文件系统
proc文件系统是一种在用户态检查内核状态的机制。例如,通过/proc/meminfo查询当前内存使用情况。
创建文件
struct porc_dir_entry* creat_proc_entry(const char *name, mode_t mode,struct proc_dir_entry *parent) /* name:文件名 mode:文件属性默认0755 parent:文件的父目录 */
创建目录
struct proc_dir_entry* proc_mkdir(const char *name, mode_t, struct proc_dir_entry *parent) /* name:目录名 parent:目录的父目录 */
删除目录/文件
void remove_procir_entry(const char *name, mode_t, struct proc_dir_entry *parent) /* name:目录名 parent:目录的父目录 */
为了让用户读写proc文件时,需要两个回调函数
read_proc
write_proc
struct proc_dir_entry{ read_proc_t *read_proc; write_proc_t *write_proc; }
int read_func(char *buffer, char **stat, off_t off, int count, int *peof, void *data) @buffer :把要返回给用户的信息写在buffer里,最大不超过PAGE_SIZE(一般4K) @stat :一般不使用 @off :buffer的偏移量 @count :用户要取的字节数 @peof :读到文件尾时,把peof指向的位置置1 @data :被多个proc文件定义为读时,通过data传递参数
int write_func (struct file *file,const char *buffer,unsigned long count,void *data); @file :该proc文件对应的file结构,一般忽略。 @buffer :待写的数据所在的位置 @count :待写数据的大小 @data :同read_func
创建一个proc文件流程:
(1)调用creat_proc_entry创建一个struct proc_dir_entry
(2)对struct proc_dir_entry进行赋值:read_proc,mode,owner,size,write_proc

浙公网安备 33010602011771号