1. 什么是proc文件系统?

实例:通过 /proc/meminfo,查询当前内存使用情况。

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

2.Proc文件分类

特点

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

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

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

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

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

3. 功能函数

内核描述

struct proc_dir_entry{

{

。。 。。。。。。。。。。。。。。。。。

read_proc_t  *read_proc;

write_proc_t  *write_proc;

。。。。。。。。。。。。。。。。。。。

}

创建文件

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:读到文件尾时,需要把*peof置1

 data:一般不使用

写操作

int write_func (struct file*file,const char *buffer,unsigned long count,void*data) //提供用户的写操作

参数:

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

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

 count :待写数据的大小

 data :一般不使用

4. 实现流程

实现一个proc文件的流程:

(1)调用create_proc_entry创建一个struct proc_dir_entry。

(2)对创建的struct proc_dir_entry进行赋值:read_proc,mode,owner,size,write_proc 等等。

5.实例一

#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/proc_fs.h>
#define procfs_name "proctest" // /proc目录下创建的文件名 struct proc_dir_entry *Our_Proc_File; //结构体,create_proc_entry返回一个指针给他 int procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data ) //回调函数,对应写操作
{
int ret;
ret = sprintf(buffer, "HelloWorld!\n");
return ret;
} int proc_init()
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if(Our_Proc_File == NULL)
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_ALERT"Error: Could not init /proc/%s\n", procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read; //回调函数
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk("/proc/%s created \n", procfs_name);
return 0;
} void proc_exit()
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_INFO"/proc/%s removed \n", procfs_name);
} module_init(proc_init);
module_exit(proc_exit);

5. 实例二 (由1修改为 能够 echo xxx > /proc/proctest)

#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/proc_fs.h>
#include<linux/uaccess.h>
#define procfs_name "proctest" struct proc_dir_entry *Our_Proc_File;
static char msg[255]; //输入保存数组 int procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data )
{
//int ret;
//ret = sprintf(buffer, "HelloWorld!\n"); int len = strlen(msg);
if(offset >= len)
return 0;
if(buffer_length > len-offset)
buffer_length = len - offset;
memcpy(buffer+offset, msg+offset, buffer_length);
return offset+buffer_length;
} int procfile_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;
} int proc_init()
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if(Our_Proc_File == NULL)
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_ALERT"Error: Could not init /proc/%s\n", procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
Our_Proc_File->write_proc = procfile_write;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk("/proc/%s created \n", procfs_name);
return 0;
} void proc_exit()
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_INFO"/proc/%s removed \n", procfs_name);
} module_init(proc_init);
module_exit(proc_exit);

echo xxxx > /proc/proctest

cat  /proc/proctest

makefile

ifneq ($(KERNELRELEASE),)

obj-m :=proc.o

else

KDIR := /lib/modules/3.5.0-17-generic/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ok *.o *.mod.o *.mod.c *.symvers
endif





Linux嵌入式 -- 内核 - proc文件系统的更多相关文章

  1. 深入理解linux系统下proc文件系统内容

    深入理解linux系统下proc文件系统内容 内容摘要:Linux系统上的/proc目录是一种文件系统,即proc文件系统. Linux系统上的/proc目录是一种文件系统,即proc文件系统.与其它 ...

  2. Linux嵌入式 -- 内核 (arm) & 文件系统

    1. make distclean 2. 配置内核  ----> 生成 .config文件 make menuconfig ARCH=arm Makefile文件分析: 3.  编译内核 mak ...

  3. linux kernel (proc文件系统)参数

    http://blog.csdn.net/guowake/article/details/3279796 Linux Proc文件系统,通过对Proc文件系统进行调整,达到性能优化的目的. 二./pr ...

  4. linux文件管理之proc文件系统

    proc 文件系统 ==================================================================================== Linux ...

  5. Linux嵌入式 -- 内核简介(x86)

    0. 嵌入式系统 以应用为中心,软硬件可裁剪,对功耗.对体积.对成本等都有严格要求的专用计算机系统. 1.  linux体系结构 2. 为什么 划分为 用户空间 和 内核控件 ?  分两级,内核和应用 ...

  6. Linux嵌入式 -- 内核 - 系统调用

    1. 系统调用 定义 Linux内核中设置了一组用于实现各种系统功能的子程序,称为系统调用.用户可以通过系统调用命令在自己的应用程序中调用它们. 系统调用和普通的函数调用非常相似,区别仅仅在于,系统调 ...

  7. Linux嵌入式 -- 内核 - 进程控制 和 调度

    1. 进程四要素 1. 有一段程序供其执行.这段程序不一定是某个进程所专有,可以与其他进程共用. 2. 有进程专用的内核空间堆栈. 3. 在内核中有一个task_struct数据结构,即通常所说的&q ...

  8. Linux嵌入式 -- 内核 - 内核定时器

    1.  度量时间差 时钟中断由系统的定时硬件以周期性的时间间隔产生,这个间隔(即频率)由内核根据HZ来确定,HZ是一个与体系结构无关的常数,可配置(50-1200),在X86平台,默认值为1000(每 ...

  9. Linux嵌入式 -- 内核 - 内核链表

    1. linux内核链表 链表数据结构的定义: struct list_head  {  struct list_head *next, *prev;  };  list_head结构包含两个指向li ...

随机推荐

  1. Kotlin——中级篇(一):类(class)详解

    在任何一门面向对象编程的语言里,类(class)是非常基础.但也是非常重要的一项组成,通俗的说就是万般皆对象,而所说的对象就是我们生成的类.Kotlin也是如此,下面详细为大家介绍Kotlin中的类的 ...

  2. Nuxt使用高德地图

    事先准备 注册账号并申请Key 1. 首先,注册开发者账号,成为高德开放平台开发者 2. 登陆之后,在进入「应用管理」 页面「创建新应用」 3. 为应用添加 Key,「服务平台」一项请选择「 Web ...

  3. 【题解】CF611H New Year and Forgotten Tree

    [题解]CF611H New Year and Forgotten Tree 神题了... 题目描述 给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度.现在用问号的个数代表每个节点编号那个 ...

  4. window.onload和$(document).ready()比较

    浏览器在页面加载完毕后,JS通常使用window.onload方法为DOM元素添加事件,而jQuery使用的是$(document).ready()方法.两者功能相似,但也有细微差异,下面简要对比一下 ...

  5. Linux中的awk命令

    awk '条件1{动作1} 条件2{动作2} ...'  文件名 条件: BEGIN          在处理文件里的第一行数据之前执行 END              在处理完文件里的最后一行数据 ...

  6. javaweb action无法跳转、表单无法跳转的解决方法

    action无法跳转,表单无法跳转的解决方法 刚在网上搜索了一下,发现我的这篇文章已被非常多人转载了去其他站点.暗爽,只是还是希望大家注明出处. 顺便说明一下.下面是在struts2中通过測试的 ac ...

  7. JavaScript setInterval()執行clearInterval() 再恢復setInterval()

    clearInterval() 方法可取消由 setInterval() 设置的 timeout. clearInterval() 方法的参数必须是由 setInterval() 返回的 ID 值. ...

  8. TCP/IP/UDP 协议

    互连网早期的时候,主机间的互连使用的是NCP协议.这种协议本身有很多缺陷,如:不能互连不同的主机,不能互连不同的操作系统,没有纠错功能.为了改善这种缺点,大牛弄出了TCP/IP协议.现在几乎所有的操作 ...

  9. Eclipse部署项目的时候抛异常【Multiple Contexts have a path of "/cdcpm".】

    Eclipse部署项目的时候抛异常[Multiple Contexts have a path of "/cdcpm".]重新clean .删除server都不好使.查看一下tom ...

  10. SQL SREVER, ORACLE数据库连接字符串

    采用windows身份验证模式 Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;