proc文件系统

57 篇文章 4 订阅

/proc 文件系统是一个虚拟文件系统,通过它可以使用一种新的方法在 Linux内核空间和用户间之间进行通信。在 /proc 文件系统中,我们可以将对虚拟文件的读写作为与内核中实体进行通信的一种手段,但是与普通文件不同的是,这些虚拟文件的内容都是动态创建的。

struct file_operations 定义文件操作
1、 创建/proc下目录

struct proc_dir_entry *proc_mkdir(const char *name,  
struct proc_dir_entry *parent); 
//name是文件名
//parent是要创建的proc文件的父目录,没有就填NULL
例如:
example_dir = proc_mkdir("test",NULL);

2、 创建文件

struct proc_dir_entry *create_proc_entry( const char *name,  mode_t mode,  
                struct proc_dir_entry *parent );
//mode是文件模式
//create_proc_entry 的返回值是一个 proc_dir_entry 指针(或者为 NULL,说明在 create 时发生了错误)
例如:
debug_file = proc_create("state",S_IRUGO,example_dir);

3、然后就可以使用这个返回的指针debug_file来配置这个虚拟文件的其他参数,例如在对该文件执行读操作时应该调用的函数。
create_proc_entry()创建proc文件 //5.4内核create_proc_entry被proc_create取代注意内核版本。https://blog.csdn.net/wh_19910525/article/details/16983005

struct proc_dir_entry {  
    ......  
    const struct file_operations *proc_fops;    <==文件操作结构体  
    struct proc_dir_entry *next, *parent, *subdir;  
    void *data;  
    read_proc_t *read_proc;                    <==读回调  
    write_proc_t *write_proc;                  <==写回调  
    ......  
};
例如:
debug_file->read_proc= proc_read;
debug_file->write_proc = proc_write;
//proc_read和proc_write为自己定义的读写函数

4、注意事项:在实现读写函数前,可以实现函数来检测当前是否有已经有创建的相同名字的文件。

5、
读回调函数:

static int (*proc_read)(char *page, char **start,  off_t off, int count,  int *eof, void *data)//echo时会调用该函数

写回调函数:

static int proc_write_foobar(struct file *file,  const char *buffer, unsigned long count,  void *data);
//buffer即输入的参数,可以用数组来转换每个参数
//count即传参数量

在注册entry的时候,如果提供read_proc,读文件时调用路径为
proc_file_operations->read->proc_file_read->read_proc

Read/write调用:
https://blog.csdn.net/ma111000522/article/details/51356709

参考链接:
https://blog.csdn.net/sty23122555/article/details/51638697?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值