proc文件系统3(创建目录或编写文件)

本文详细介绍了如何在Linux内核的proc文件系统中使用proc_create()和proc_create_data()函数创建虚拟文件,以及proc_mkdir()函数创建目录。这些函数允许指定文件操作回调、权限和私有数据。proc文件系统的访问是通过file_operations结构体的read和write回调实现的。文中还给出了一个示例,展示了如何创建并读写'hello_proc'文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要在/proc文件系统中创建一个虚拟文件,要用proc_create()或proc_create_data()函数。这个函数可以接收一个文件名、一组权限和这个文件在/proc文件系统中出现的位置。这两个函数的返回值是一个proc_dir_entry类型指针(或者为NULL,说明在发生了错误时)。然后就可以使用这个返回的指针来配置这个虚拟文件的其他参数,例如在对该文件执行读操作时应该调用的函数。函数的原型和proc_dir_entry结构中的一部分如下图所示。

struct proc_dir_entry {
    /*
     * number of callers into module in progress;
     * negative -> it's going away RSN
     */
    atomic_t in_use;
    atomic_t count;        /* use count */
    struct list_head pde_openers;    /* who did ->open, but not ->release */
    /* protects ->pde_openers and all struct pde_opener instances */
    spinlock_t pde_unload_lock;
    struct completion *pde_unload_completion;
    const struct inode_operations *proc_iops;    //Inode operations functions
    const struct file_operations *proc_fops;    //File operations functions
    void *data;            //私有数据指针,在操作函数中可以使用的到
    unsigned int low_ino;
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值