Interface between user space and kernel space: Proc File system

Source: Internet
Author: User
Tags sprintf

First in contact with the Linux BSP, the project has a specification is the bottom layer to provide hardware PCB and BOM information. Because in the Linux root file system/proc directory with hardware CPU, memory, memory and other hardware-related information, and in user space through the cat command can be easily obtained, so there is an idea, is to try to the project hardware PCB and BOM information displayed in the/proc directory, User space can then be obtained by cat or by reading and writing files. So I looked up some knowledge about the Linux proc file system and sorted out a routine.

What is the proc file system.

Proc File system is a pseudo file system, it only exists in memory, and does not occupy the outer village space. It accesses the operational interface of kernel data in a file system manner. The Linux kernel provides a mechanism through which the/proc file system accesses kernel internal data structures, changes the kernel settings, and allows users and applications to get information about the system through proc, and can change some of the kernel's parameters. Because the information of the system, such as the process, can be changed dynamically, so when the user or application reads the proc file, the proc file system reads the required information from the system kernel dynamically and submits it. (This can be sysfs somewhat similar, now in driver programming, often through SYSFS to implement some parameter changes, even the SYSFS as a "switch" to use).

Previous routine:

#include <linux/module.h>	
#include <linux/proc_fs.h>	
char mystring[] = "Hello_proc";
int My_proc_read (char *buf, Char **start, off_t off, int count, int *eof, void *data) 
{
	int len = 0;
	Len + + sprintf (Buf+len, "\n%s\n", mystring);
	Len + + sprintf (buf+len, "\ n");
	return	len;
}
static int __init proc_init (void)
{
	create_proc_read_entry ("MyProc", 0, NULL, my_proc_read, NULL);
	return	0;  
}
static void __exit proc_exit (void)
{
	remove_proc_entry ("MyProc", NULL);
}
Module_init (proc_init);
Module_exit (proc_exit);

Module_license ("GPL");
Module_description ("Test proc filesystem");
Module_author ("Vincent Wu <wei2009job@gmail.com>");

The above routine compile layer module Test_proc.ko

1. Insmod Test_proc.ko

Generate a file node in the/proc directory after the module is loaded MyProc

2.cat/proc/mypoc

See output information as Hello_proc

In addition, the data can be obtained by the read system call reading/proc/myproc Hello_proc


Note that the above is generated directory MyProc in the/proc directory, if you want to build a directory in/proc, and then build a subdirectory in the generated directory. If you create a directory/proc/myproc/myproc_sub, you can do this:

#include <linux/module.h>	
#include <linux/proc_fs.h>	
char mystring[] = "Hello_proc";
struct Proc_dir_entry *parent;
int My_proc_read (char *buf, Char **start, off_t off, int count, int *eof, void *data) 
{
	int len = 0;
	Len + + sprintf (Buf+len, "\n%s\n", mystring);
	Len + + sprintf (buf+len, "\ n");
	return	len;
}
static int __init proc_init (void)
{

	parent = Proc_mkdir ("MyProc", NULL);
	Create_proc_read_entry ("Myproc_sub", 0, parent, My_proc_read, NULL);
	return	0;  
}
static void __exit proc_exit (void)
{
	remove_proc_entry ("Myproc_sub", parent);
}
Module_init (proc_init);
Module_exit (proc_exit);

Module_license ("GPL");
Module_description ("Test proc filesystem");
Module_author ("Vincent Wu <wei2009job@gmail.com>");




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.