文档介绍:http://lxr.linux.no/linux+v2.6.37/Documentation/filesystems/proc.txt
以下内容抄录linux设备驱动开发详解-宋宝华
在/proc文件系统中,我们可以将对虚拟文件的读写作为与内核中实体进行通信的一种手段。/proc被内核用于向用户导出信息。linux系统的许多命令本身都是通过分析/proc下的文件来完成的,如ps、top、uptime和free等。例如free命令通过分析/proc/meminfo文件得到可用内存信息。

在linux3.9及以前的内核版本中,proc应用

可用如下函数创建/proc节点:

struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent);
static inline struct proc_dir_entry *create_proc_read_entry(const char *name, mode_t mode, struct proc_dir_entry *base, read_proc_t *read_proc, void * data);

create_proc_entry()用于创建/proc节点,create_proc_read_entry()用于创建只读的/proc节点。参数name为/proc节点名字,parent或base为父目录的节点,若为NULL,则指/proc目录,read_proc为节点的读函数指针。

创建/proc目录,

struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);

proc_mkdir()和create_proc_entry()使用范例:

struct proc_dir_entry *example_dir = proc_mkdir(“proc_example”, NULL);
if(example_dir == NULL){
rv = -ENOMEM;
goto out;
} example_dir->owner = THIS_MODULE;
example_file = create_proc_entry(“example_file”, , example_dir);
if( example_file == NULL){
rv = -ENOMEM;
goto out;
} example_file->owner = THIS_MODULE;
example_file->read_proc = example_file_read;
example_file->write_proc = example_file_write;

proc节点的读写函数的类型分别为:

typedef int (read_proc_t)(char *page, char **start, off_t off, int count, int *eof, void *data);
typedef int (write_proc_t)(struct file *file, const char __user *buffer, unsigned long count, void *data);

page指向用于写入数据的缓冲区,start用户返回实际的数据并写到内存页的位置,eof用于返回读结束标志,offset是读的偏移,count是要读的数据长度。start参数比较复杂,对于/proc只包含简单数据的情况,通常不需要再读函数中设置*start,这意味着内核将认为数据保存在内存页偏移0的地方。
写函数与file_operation中的write()成员函数类似,需要一次从用户缓冲区到内存空间的复制过程。
data是struct proc_dir_entry的私有数据,实际读写就是通过page或buf将数据读取或保存到data所指数据区中。

void remove_proc_entry(const char *name, struct proc_dir_entry *parent);

在linux系统中已经定义好的可使用的/proc节点宏包括:proc_root_fs(/proc),proc_net(/proc/net),proc_bus(/proc/bus),proc_root_driver(proc_driver)等,proc_root_fs实际就是NULL。

linux3.10及以后版本中proc应用

proc的内核API和实现架构变更较大,create_proc_entry(),create_proc_read_entry()之类的API都被删除了,取而代之的是直接使用proc_create()、proc_create_date()API。同时,也不存在read_proc()、write_proc()之类的针对proc_dir_entry的成员函数了,而是直接把file_operations结构体的指针传入proc_create()或者proc_create_date()函数中。

static inline struct proc_dir_entry *proc_create(const char *name, mode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops)
static inline struct proc_dir_entry *proc_create_data(const char *name, mode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops, void *data)

代码中通过宏定义区分:

#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)

#else

#endif

内核交互--procfs的更多相关文章

  1. ifconfig源码分析之与内核交互数据

    <ifconfig源码分析之与内核交互数据>本文档的Copyleft归rosetta所有,使用GPL发布,可以自由拷贝.转载,转载时请保持文档的完整性.参考资料:<Linux设备驱动 ...

  2. 内核交互--sysfs

    文档介绍:http://lxr.linux.no/linux+v2.6.37/Documentation/filesystems/sysfs.txt The sysfs Filesystem Sysf ...

  3. 内核交互--debugfs_转

    转载:Linux内核里的DebugFS DebugFS,顾名思义,是一种用于内核调试的虚拟文件系统,内核开发者通过debugfs和用户空间交换数据.类似的虚拟文件系统还有procfs和sysfs等,这 ...

  4. Python IO内核交互了解

    注:Unix \ Linux 环境下的network IO   用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操作系 ...

  5. linux内核交互,设备驱动控制管理接口

    1,ioctl preface--starting point ,format,mount volume,in addition to the above file system -- allows ...

  6. 深入理解Linux网络技术内幕——用户空间与内核空间交互

    概述:     内核空间与用户空间经常需要进行交互.举个例子:当用户空间使用一些配置命令如ifconfig或route时,内核处理程序就要响应这些处理请求.     用户空间与内核有多种交互方式,最常 ...

  7. Linux 用户态与内核态的交互【转载】

    Linux 用户态与内核态的交互  在 Linux 2.4 版以后版本的内核中,几乎全部的中断过程与用户态进程的通信都是使用 netlink 套接字实现的,例如iprote2网络管理工具,它与内核的交 ...

  8. (转)Linux 文件系统:procfs, sysfs, debugfs 用法简介

    网址:http://www.tinylab.org/show-the-usage-of-procfs-sysfs-debugfs/ 1 前言 内核中有三个常用的伪文件系统:procfs,debugfs ...

  9. quagga源码分析--内核通信netlink

    Linux操作系统中当CPU处于内核状态时,可以分为有用户上下文的状态和执行硬件.软件中断两种.其中当处于有用户上下文时,由于内核态和用户态的内 存映射机制不同,不可直接将本地变量传给用户态的内存区: ...

随机推荐

  1. 修复XAMPP安装过程中 因端口80被占用 Apache无法启动的问题

    Fix XAMPP Apache Not Starting Because Port 80 In Use XAMPP中Apache服务器无法启动,出现该问题的最常见原因是由于默认端口号80可能已被其他 ...

  2. 扑克模拟,牌型判断java版

    Card类 package com.company; public class Card { private String color; private Integer value; public S ...

  3. 阿里p6面试

    电话面试: 第一次面试关注的问题,1)java基础: jvm 内存回收,垃圾回收基本原理,Java并发包的线程池,Java8的新特性.nio 堆排序.conrenthashmap , concurre ...

  4. mysql 5.7.20解压版安装配置

    MySql 5.7.20版本免安装版配置过程   下载地址为: https://dev.mysql.com/downloads/mysql/ 最下面根据自己的操作系统选择合适的型号 下载完以后解压缩到 ...

  5. Coherence代理节点在离开集群时的恢复

    Coherence的架构参考 在极端压力之下,有时候代理节点会忙于处理请求而不响应其他的心跳,同步,导致其他节点传输的报文没有回应,而被认为是离开集群,从而影响业务. 写了一段代码,能让进程在监听到有 ...

  6. mysql 5.7 安装手册(for linux)

    1.下载和解压mysql数据库   wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-x86_6 ...

  7. druid.io使用技术简介: Hyperloglog

    druid.io 使用Hyperloglog 估计基数 参照如下连接 http://blog.codinglabs.org/articles/algorithms-for-cardinality-es ...

  8. C#趣味程序---求两个数的最大公约数和最小公倍数

    using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Cons ...

  9. 为Linux上FireFox安装Flash插件

    废话少说,步骤如下: 1.点击网页上插件缺失处,根据提示下载tar.gz版本的插件,我下载的版本是install_flash_player_11_linux.i386.tar.gz,这个文件被下载到了 ...

  10. HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...