Proc文件系统
proc 文件系统是一种内核和内核模块用来向进程 (process) 发送信息的机制 (所以叫做 /proc)。这个伪文件系统让你可以和内核内部数据结构进行交互,获取 有关进程的有用信息
(1)实验目的:掌握proc文件系统的工作原理
(2)实验内容:编写文件名proctest的proc文件,实用cat命令读这个文件,输出的结果为helloworld!
#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#define procfs_name "proctest"
struct proc_dir_entry *Our_Proc_File;
int procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;
ret = sprintf(buffer, "HelloWorld!\n");
return ret;
}
int proc_init()
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(procfs_name, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
// Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk("/proc/%s created\n", procfs_name);
return 0;
}
void proc_exit()
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}
module_init(proc_init);
module_exit(proc_exit);
创建一个mydir的proc目录,在这个目录下创建一个pool的proc文件。对这个文件进程读写操作!
proc 文件系统是一种内核和内核模块用来向进程 (process) 发送信息的机制 (所以叫做 /proc)。这个伪文件系统让你可以和内核内部数据结构进行交互,获取 有关进程的有用信息
(1)实验目的:掌握proc文件系统的工作原理
(2)实验内容:编写文件名proctest的proc文件,实用cat命令读这个文件,输出的结果为helloworld!
#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#define procfs_name "proctest"
struct proc_dir_entry *Our_Proc_File;
int procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret;
ret = sprintf(buffer, "HelloWorld!\n");
return ret;
}
int proc_init()
{
Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
if (Our_Proc_File == NULL) {
remove_proc_entry(procfs_name, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
procfs_name);
return -ENOMEM;
}
Our_Proc_File->read_proc = procfile_read;
// Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->mode = S_IFREG | S_IRUGO;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 37;
printk("/proc/%s created\n", procfs_name);
return 0;
}
void proc_exit()
{
remove_proc_entry(procfs_name, NULL);
printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}
module_init(proc_init);
module_exit(proc_exit);
这是一个模块 make之后 用insmod proctest.ko lsmod查看进程在跑
然后/PROC目录下就会有一个proctest文件 输出helloworld!
创建一个mydir的proc目录,在这个目录下创建一个pool的proc文件。对这个文件进程读写操作!