proc_create实践(linux用户空间和内核空间交互之二)

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


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值