在/sys目录下建立一个文件,并且实现信息的读取和存储
来源:互联网 发布:网络舆情监控系统 编辑:程序博客网 时间:2024/04/28 12:34
下面通过在网络上截取的一个例子加以说明:
此文件是在/sys/kernel/目录里面建立三个文件foo、baz、bar,每个文件都是静态的int型的缓存空间。
可以在这三个文件中读写一些整形的数据。
/*
* * Sample kobject implementation
* *
* * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
* * Copyright (C) 2007 Novell Inc.
* *
* * Released under the GPL version 2 only.
* *
* */
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
/*
* * This module shows how to create a simple subdirectory in sysfs called
* * /sys/kernel/kobject-example In that directory, 3 files are created:
* * "foo", "baz", and "bar". If an integer is written to these files, it can be
* * later read out of it.
* */
static int foo;
static int baz;
static int bar;
/*
* * The "foo" file where a static variable is read from and written to.
* */
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,char *buf)
{
return sprintf(buf, "%d\n", foo);
}
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,const char *buf, size_t count)
{
sscanf(buf, "%du", &foo);
return count;
}
static struct kobj_attribute foo_attribute = __ATTR(foo, 0666, foo_show, foo_store);
/*
* * More complex function where we determine which variable is being accessed by
* * looking at the attribute for the "baz" and "bar" files.
* */
static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
int var;
if (strcmp(attr->attr.name, "baz") == 0)
var = baz;
else
var = bar;
return sprintf(buf, "%d\n", var);
}
static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
int var;
sscanf(buf, "%du", &var);
if (strcmp(attr->attr.name, "baz") == 0)
baz = var;
else
bar = var;
return count;
}
static struct kobj_attribute baz_attribute = __ATTR(baz, 0666, b_show, b_store);
static struct kobj_attribute bar_attribute = __ATTR(bar, 0666, b_show, b_store);
/*
* * Create a group of attributes so that we can create and destroy them all
* * at once.
* */
static struct attribute *attrs[] = {
&foo_attribute.attr,
&baz_attribute.attr,
&bar_attribute.attr,
NULL, /* need to NULL terminate the list of attributes */
};
/*
* * An unnamed attribute group will put all of the attributes directly in
* * the kobject directory. If we specify a name, a subdirectory will be
* * created for the attributes with the directory being the name of the
* * attribute group.
* */
static struct attribute_group attr_group = {
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
/*
* Create a simple kobject with the name of "kobject_example",
* located under /sys/kernel/
*
* As this is a simple directory, no uevent will be sent to
* userspace. That is why this function should not be used for
* any type of dynamic kobjects, where the name and number are
* not known ahead of time.
*/
example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
/* Create the files associated with this kobject */
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
static void __exit example_exit(void)
{
kobject_put(example_kobj);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
下面这个文件是本人修改的一个文件:
将目录修改为/sys/cpuInfo/下的cpuload、cpueffic、cpuoneeffic、cputwoeffic的大小为64的缓存空间。
这样就可以存储整形数据了。
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
/*************************************
*在/sys/目录下建立cpuInfo目录,此目录中包含四个文件
*
*cpuload是一个静态变量,用于存储当前的cpu负载信息
*
*cpueffic是一个静态变量,用于存储cpu的平均使用率信息
*
*cpuoneeffic是一个静态变量,用于存储第一个cpu的使用率信息
*
*cputwoeffic是一个静态变量,用于存储第二个cpu使用率的信息
*
*************************************/
#define SIZE 64
static char buffercpuload[SIZE];
static char buffercpueffic[SIZE];
static char buffercpuoneeffic[SIZE];
static char buffercputwoeffic[SIZE];
static ssize_t cpuload_show(struct kobject *kobj, struct kobj_attribute *attr,char *buf)
{
return sprintf(buf, "%s \n", buffercpuload);
}
static ssize_t cpuload_store(struct kobject *kobj, struct kobj_attribute *attr,const char *buf, size_t count)
{
sscanf(buf, "%s", buffercpuload);
return count;
}
static struct kobj_attribute cpuload_attribute = __ATTR(cpuload, 0666, cpuload_show, cpuload_store);
static ssize_t cpueffic_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%s \n", buffercpueffic);
}
static ssize_t cpueffic_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%s", buffercpueffic);
return count;
}
static struct kobj_attribute cpueffic_attribute = __ATTR(cpueffic, 0666, cpueffic_show, cpueffic_store);
static ssize_t cputwoeffic_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%s \n", buffercputwoeffic);
}
static ssize_t cputwoeffic_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%s", buffercputwoeffic);
return count;
}
static struct kobj_attribute cputwoeffic_attribute = __ATTR(cputwoeffic, 0666, cputwoeffic_show, cputwoeffic_store);
static ssize_t cpuoneeffic_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%s \n", buffercpuoneeffic);
}
static ssize_t cpuoneeffic_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%s", buffercpuoneeffic);
return count;
}
static struct kobj_attribute cpuoneeffic_attribute = __ATTR(cpuoneeffic, 0666, cpuoneeffic_show, cpuoneeffic_store);
/************************
*创建一个attributes组用来创建和销毁这些文件
********************** */
static struct attribute *attrs[] = {
&cpuload_attribute.attr,
&cpueffic_attribute.attr,
&cpuoneeffic_attribute.attr,
&cputwoeffic_attribute.attr,
NULL,
};
static struct attribute_group attr_group = {
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
/***************************
** 在/sys/目录下创建一个简单的kobject,名字是cpuInfo
**************************/
example_kobj = kobject_create_and_add("cpuInfo", NULL); 中第一个参数是创建的目录的名称,第二个参数是创建的目录的位置,如果定义static struct kobject *example_kobj; 并且把example_kobj置于NULL位置就会在/sys/kernel/目录下创建cpuInfo目录
if (!example_kobj)
return -ENOMEM;
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
static void __exit example_exit(void)
{
kobject_put(example_kobj);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
- 在/sys目录下建立一个文件,并且实现信息的读取和存储
- 在/sys目录下建立一个文件,并且实现信息的读取和存储
- 在/sys目录下建立一个文件,并且实现信息的读取和存储
- python读取一个目录下的文件,并且写入.txt文件中
- 【练习题】 使用程序建立一个多级的目录,在每个目录下,新建一个和目录名字一样的txt文件,并写入1990
- 获取一个目录下的文件信息
- subsys_system_register 会在/sys/devices/system下面建立一个目录
- 读取Assets目录下的文件信息(此处主要是读取Zip包和Apk的信息)
- 使用linux c 目录操作函数,读取任一目录下的所有目录信息文件信息
- kettle读取文件目录下的数据结构一致的所有数据文件信息和内容
- kettle读取文件目录下的数据结构一致的所有数据文件信息和内容
- 总线初始化及/sys下bus目录的建立
- 设备初始化及/sys下devices等目录的建立
- Linux下读取一个目录下的多个文件
- windows下c++读取一个目录下的所有文件
- 为Tiny4412设备驱动在proc目录下添加一个可读版本信息的文件
- JavaScript读取文件目录信息以及目录下的文件名信息
- 写的一个读取指定目录下的所有文件和子目录的程序
- wordpress标签大全
- 建设全功能团队——实践篇
- hive数据类型(翻译自Hive Wiki)
- 一个整数数列,元素取值可能是0~65535中的任意一个数,相同数值不会重复出现。
- lvs-rrd 监控LVS
- 在/sys目录下建立一个文件,并且实现信息的读取和存储
- 天涯易栈编程助手1.02版
- ubuntu12.04中修改系统语言
- drupal7 导入翻译时出错解决方法--最新办法
- chap9.顺序容器
- google protocol buffer的使用
- 数字证书原理
- 成功的软件项目经理
- Operation dashboard 学习总结