Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7538061
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2011-09-19 20:51:53

proc文件系统是一种在用户态检查内核状态的机制

Proc文件

子目录/文件名      内容描述

apm              高级电源管理信息

bus                      总线以及总线上的设备

devices          可用的设备信息

driver            已经启用的驱动程序

interrupts              中断信息

ioports           端口使用信息

version          内核版本

特点

1.         每个文件都规定了严格的权限 (可读?可写?哪个用户可读?哪个用户可写?)

2.         可以用文本编辑程序读取(more命令,cat命令,vi程序等等)

3.         不仅可以有文件,还可以有子目录。

4.         可以自己编写程序添加一个/proc目录下的文件。

5.         文件的内容都是动态创建的,并不存在于磁盘上。

 

创建文件

struct proc_dir_entry* create_proc_entry (const char*name, mode_t mode, struct proc_dir_entry *parent)

功能:创建proc文件

参数:

 name :要创建的文件名

 mode :要创建的文件的属性默认0755

 parent :这个文件的父目录

 

创建目录

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

功能:创建proc目录

参数:

 name :要创建的目录名

 parent :这个目录的父目录

 

删除目录/文件

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

功能:删除proc目录或文件

参数:

name :要删除的文件或目录名

parent :所在的父目录

 

读写

为了能让用户读写添加的proc文件,需要挂接上读写回调函数:

 read_proc     write_proc

 

读操作

int read_func (char *buffer,char **stat,off_t off,int count,int *peof,void *data)

参数:

 buffer :把要返回给用户的信息写在buffer里,最大不超过PAGE_SIZE

 stat :一般不使用

 off :偏移量

 count :用户要取的字节数

 peof :读到文件尾时,需要把*peof1

 data :一般不使用

 

写操作

int write_func (struct file *file,const char *buffer,unsigned long count,void *data)

参数:

file :proc文件对应的file结构,一般忽略。

buffer :待写的数据所在的位置

count :待写数据的大小

data :一般不使用

 

实现流程

实现一个proc文件的流程:

1)调用create_proc_entry创建一个struct proc_dir_entry

2)对创建的struct proc_dir_entry进行赋值:read_procmodeownersizewrite_proc 等等。

 

实例:

#include

#include

#include

 

static struct proc_dir_entry *mydir;

static struct proc_dir_entry *pfile;

 

static char msg[255];

 

static int myproc_read(char *page, char **start, off_t off, int count, int *eof, void *data)

{

        int len = strlen(msg);

 

        if (off >= len)

                return 0;

 

        if (count > len - off)

                count = len - off;

 

        memcpy(page + off, msg + off, count);

        return off + count;

}

 

static int myproc_write(struct file *file, const char __user *buffer, unsigned long count, void *data)

{

        unsigned long count2 = count;

 

        if (count2 >= sizeof(msg))

                count2 = sizeof(msg) - 1;

 

        if (copy_from_user(msg, buffer, count2))

                return -EFAULT;

 

        msg[count2] = '\0';

        return count;

}

 

static int __init myproc_init(void)

{

        mydir = proc_mkdir("mydir", NULL);

        if (!mydir) {

                printk(KERN_ERR "Can't create /proc/mydir\n");

                return -1;

        }

 

        pfile = create_proc_entry("pool", 0666, mydir);

        if (!pfile) {

                printk(KERN_ERR "Can't create /proc/mydir/pool\n");

                remove_proc_entry("mydir", NULL);

                return -1;

        }

 

        pfile->read_proc = myproc_read;

        pfile->write_proc = myproc_write;

 

        return 0;

}

 

static void __exit myproc_exit(void)

{

        remove_proc_entry("pool", mydir);

        remove_proc_entry("mydir", NULL);

}

 

module_init(myproc_init);

module_exit(myproc_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("David Xie");

 

1、编译加载模块

2、查看/proc目录,是否创建mydir/ pool

3、测试文件读写:

       写:# echo “Lzy” > /proc mydir/ pool

    # cat /proc mydir/ pool

 

使用命令fdisk l 查看磁盘信息, cat /proc/cpuinfo 查看CPU的信息都是利用proc文件系统,所以自己实现一个proc对于理解proc文件工作原理很有帮助。

 

阅读(1759) | 评论(0) | 转发(4) |
0

上一篇:系统调用参考手册

下一篇:Linux内核异常

给主人留下些什么吧!~~