proc_create实践(linux用户空间和内核空间交互之二)
2015-06-12 21:10
656 查看
1、proc_create实例
static int mytest_flag = 0;//create flag variable static ssize_t mytest_proc_write(struct file *file,const char __user *buffer,size_t count,loff_t *pos) { char mode; if(count > 0) { if(get_user(mode,buffer)) return EFAULT; mytest_flag = (mode != '0'); } return count; } static int mytest_proc_show(struct seq_file *seq,void *v) { seq_puts(seq,mytest_flag ?"True\n":"False\n"); return 0; } static int mytest_proc_open(struct inode *inode,struct file *file) { return single_open(file,mytest_proc_show,inode->i_private); } static const struct file_operations myTest_proc_fops = { .open = mytest_proc_open, .read = seq_read, .write = mytest_proc_write, .llseek = seq_lseek, .release = single_release, }; static int battery_probe(struct platform_device *dev) { 。。。。。。 struct proc_dir_entry *testFile = proc_create("myTest",0644,NULL,&myTest_proc_fops); if(NULL == testFile) printk("%s,proc_create error.\n",__func__); }
执行结果:
1)在CMD输入adb shell进入linux 文件系统;
2)进入proc目录
3)查看myTest结点,通过cat myTest或echo 1 > myTest,可对结点myTest进行读写操作
注意头文件:proc_fs.h
2、proc_create和create_proc_entry的区别
在最新linux内核中,不在使用create_proc_entry,请使用proc_create()进行结点建立3、一些实用函数实现
extern struct proc_dir_entry *proc_symlink(const char *,struct proc_dir_entry *, const char *);
extern struct proc_dir_entry *proc_mkdir(const char *, struct proc_dir_entry *);
extern struct proc_dir_entry *proc_mkdir_data(const char *, umode_t,
struct proc_dir_entry *, void *);
extern struct proc_dir_entry *proc_mkdir_mode(const char *, umode_t,
struct proc_dir_entry *);
extern struct proc_dir_entry *proc_create_data(const char *, umode_t,
struct proc_dir_entry *,
const struct file_operations *,
void *);
static inline struct proc_dir_entry *proc_create(
const char *name, umode_t mode, struct proc_dir_entry *parent,
const struct file_operations *proc_fops);
参考:http://www.ibm.com/developerworks/cn/linux/l-proc.html#ibm-pcon
相关文章推荐
- Linux下ppp拨号+3G模块(evdo 中兴MC8630模块)
- Centos常用快捷键
- linux 命令——4 mkdir (转)
- linux 命令——3 pwd (转)
- linux入门基础——linux软件管理RPM
- linux 命令——2 cd (转)
- linux 命令——ls
- linux命令10--file
- linux命令9--diffstat
- sqlite数据库读写在linux下的权限问题
- linux 命令之 ping
- centos下快速安装JDK
- Linux与Windows文件共享
- 删除grub引导的linux双系统
- tar文件的安装 基础
- linux下软件安装和卸载 一般方法
- crontab:自动执行的周期性工作
- Linux GCC常用命令
- makefile 基础中的基础
- rpm包下载地址 ftp://rpmfind.net/linux/