read/write proc例程
来源:互联网 发布:查车档案软件 编辑:程序博客网 时间:2024/06/06 03:27
Proc做为用户态进程和内核进行通信的工具,可以用Proc访问内核的数据,本文例程创建一个Proc,然后通过一个Hello world例程进行Proc的读写操作。
Proc的源代码:(此代码转自:http://tuxthink.blogspot.com/2011/02/creating-readwrite-proc-entry.html)
/*************************************************************************> File Name: proc_write_read.c> Author: > Mail: > Created Time: Mon 20 Apr 2015 01:57:22 PM CST ************************************************************************/#include <linux/module.h>#include <linux/kernel.h>#include <asm/uaccess.h>#include <linux/cdev.h>#include <linux/proc_fs.h>#define MAX_PROC_SIZE 100static char proc_data[MAX_PROC_SIZE];static struct proc_dir_entry *proc_write_entry;int read_proc(char *buf,char **start,off_t offset,int count,int *eof,void *data ){ int len=0; len = sprintf(buf,"%s",proc_data); return len;}int write_proc(struct file *file,const char *buf,int count,void *data ){ if(count > MAX_PROC_SIZE) count = MAX_PROC_SIZE; if(copy_from_user(proc_data, buf, count)) return -EFAULT; return count;}void create_new_proc_entry(){ proc_write_entry = create_proc_entry("proc_entry",0666,NULL); if(!proc_write_entry) { printk(KERN_INFO "Error creating proc entry"); return -ENOMEM; } proc_write_entry->read_proc = read_proc ; proc_write_entry->write_proc = write_proc; printk(KERN_INFO "proc initialized");}int proc_init (void) { create_new_proc_entry(); return 0;}void proc_cleanup(void) { printk(KERN_INFO " Inside cleanup_module\n"); remove_proc_entry("proc_entry",NULL);}MODULE_LICENSE("GPL"); module_init(proc_init);module_exit(proc_cleanup);Makefile文件
ifneq ($(KERNELRELEASE),) obj-m := proc_write_read.o aa=1234else aa=4567KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd)default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules @echo $(aa)endif clean: $(MAKE) -C $(KERNELDIR) M=$(PWD) clean @echo $(aa)
通过Make命令可以生成proc_write_read.ko 模块文件。
加载模块:
sudo insmod proc_write_read.ko
卸载模块:
sudo rmmod proc_write_read.ko
读写Proc的例程:
/*************************************************************************> File Name: test_proc_writeread.c> Author: > Mail: > Created Time: Mon 20 Apr 2015 02:45:58 PM CST ************************************************************************/#include<stdio.h>int main(int argc, char * argv[]){ FILE *fd_read, *fd_write = NULL; char data1[30] = {0}; char data2[30] = {0}; memcpy(data1, "Hello World!!!", 30);#if 1 fd_write = fopen("/proc/proc_entry", "w"); if(NULL == fd_write) { return -1; } printf("write \"%s\" to proc_entry", data1); fwrite(data1, 1, sizeof(data1), fd_write); fclose(fd_write);#endif printf("\r\n~~~~~~~~~~~~~~~~~~~~~\r\n"); fd_read = fopen("/proc/proc_entry", "r"); if(NULL == fd_read) { return -1; } fread(data2, 1, sizeof(data2), fd_read); printf("read data \"%s\" \nend\n", data2); fclose(fd_read); return 0;}
编译生成test程序
cc -o test test_proc_writeread.c
运行test
结果如下:
write "Hello World!!!" to proc_entry
~~~~~~~~~~~~~~~~~~~~~
read data "Hello World!!!"
end
0 0
- read/write proc例程
- Read and Write a /proc File
- Read read read, write write write!
- read/write
- read/write
- Read && Write
- Read and Write XML
- socket api -- read write
- READ Write xml
- strcpy(), memcpy(), Write(), Read().
- Read/Write Lock
- QTcpSocket read write buffer
- QTcpSocket read write buffer
- read、write函数
- read()与write()函数
- read/write&mmap&aio
- Read and Write Excel
- read/write函数
- 基姆拉尔森计算公式
- opencv学习2
- DFS Letter Combinations of a Phone Number
- TMCoverFlowLayout
- tomcat配置过程中出现JRE_HOME environment variable is not defined correctly的错误
- read/write proc例程
- 园林隔离栅
- Linux上的free命令详解
- Duplicate standby database from active database
- 动态规划之最优二叉搜索树
- linux服务器用作路由器
- eclipse 工程导入可能出现的情况
- 编程之美2015资格赛 题目2 : 回文字符序列
- re-sign 重签