Linux Proc文件系统
来源:互联网 发布:通达信软件使用说明书 编辑:程序博客网 时间:2024/05/16 06:03
/proc文件系统一中虚拟文件系统,提供了一种内核空间和内核空间通信新的方式,虚拟文件系统相对其它文件系统,文件时动态生成的,起初,/proc文件系统就是为了提供内核运行的一些基本信息,下面将做一个简单/proc文件读写操作。
proc_test.c 源码如下
#include <linux/module.h>#include <linux/kernel.h>#include <linux/proc_fs.h>#include <linux/string.h>#include <asm/uaccess.h>#include <linux/seq_file.h>#include <linux/slab.h>MODULE_LICENSE("GPL");MODULE_DESCRIPTION("proc write and read");MODULE_AUTHOR("lchj");#define PROC_TEST_FILE_NAME "proc_test_file"#define PROC_TEST_PARENT_FILE_NAME NULLstatic int proc_test_read(struct file *flip,char *buf,size_t count,loff_t *f_ops);static int proc_test_write(struct file *flip,char *buf,size_t count,loff_t *f_ops);static struct proc_dir_entry *proc_entry;int len,temp;static char *str;static char test[100];struct file_operations hello_proc_fops = { read :proc_test_read, .write = proc_test_write};int init_proc_module(void){ printk("proc_test_modlue_init\n"); proc_entry = proc_create( PROC_TEST_FILE_NAME, 0466, PROC_TEST_PARENT_FILE_NAME ,&hello_proc_fops); str ="hello proc test"; len = strlen(str); temp = len; printk("%d",len); return 0;}void exit_proc_module(void){ printk("proc_test module exit\n"); remove_proc_entry(PROC_TEST_FILE_NAME,PROC_TEST_PARENT_FILE_NAME);}static int proc_test_read(struct file *flip,char *buf,size_t count,loff_t *f_ops){ printk("test\n"); printk("%d\n",count); if(count>temp){ count=temp; } printk("%d\n",count); temp=temp-count; copy_to_user(buf,str, count); if(count==0){ temp=len; } printk("%d\n",count); return count;}static int proc_test_write(struct file *flip,char *buf,size_t count,loff_t *f_ops){ printk("write count%zu",count); str = kmalloc(100,GFP_KERNEL); copy_from_user(str,buf,count); len = strlen(str); temp = len; return count;}module_init(init_proc_module);module_exit(exit_proc_module);
MakeFile文件如下
obj-m += proc_test.oCURRENT_PATH := $(shell pwd)LINUX_KERNEL := $(shell uname -r)LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL)all: make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modulesclean:uninstall make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) cleaninstall: sudo insmod proc_test.kouninstall: sudo rmmod proc_testlog: dmesgread: cat /proc/proc_test_filewrite: echo ${str} > /proc/proc_test_file
运行效果如下
lchj@lchj-VirtualBox:~/Documents/proc_test$ make installsudo insmod proc_test.ko[sudo] password for lchj: lchj@lchj-VirtualBox:~/Documents/proc_test$ make readcat /proc/proc_test_filehello proc testlchj@lchj-VirtualBox:~/Documents/proc_test$ make write str=hi,lchj!echo hi,lchj! > /proc/proc_test_filelchj@lchj-VirtualBox:~/Documents/proc_test$ make readcat /proc/proc_test_filehi,lchj!lchj@lchj-VirtualBox:~/Documents/proc_test$ make write str=hi,procecho hi,proc > /proc/proc_test_filelchj@lchj-VirtualBox:~/Documents/proc_test$ make readcat /proc/proc_test_filehi,proc
代码很简单,如没有Linux driver相关经验的话,需要注意一下代码中的read方法会掉用多次。
代码github地址
0 0
- Linux文件系统之proc文件系统
- linux下 proc文件系统
- proc 文件系统 for linux
- linux proc文件系统学习
- Linux proc文件系统
- linux proc文件系统
- linux proc文件系统学习
- Linux proc文件系统
- linux /proc虚拟文件系统
- linux proc 文件系统
- 理解linux Proc文件系统
- linux proc文件系统学习
- linux proc文件系统探索
- Linux的proc文件系统
- Linux proc文件系统相关
- linux 的proc文件系统
- linux 文件系统之/proc
- linux proc文件系统节点
- java正则匹配及替换的一点思考
- 风花雪月
- 七、二叉排序树--(1)什么是二叉排序树
- win/mac下反编译Android安装包-APK文件
- 通讯录-第三版
- Linux Proc文件系统
- Python在Myeclipse上配置(解决Pydev插件不出现和安装标准库的方法)
- Android的Window
- android布局属性详解
- numerical
- 柴俊理金:5、16午夜黄金、宁贵沥青操作策略
- 删除文件夹和复制文件夹 java
- 一个操作系统的实现(3)-保护模式进阶
- iOS开发中常见的单例类