Introduction to "procfs" (proc file system)

Source: Internet
Author: User
Introduction to "procfs"


Introduction From wiki:

Procfs (or the proc filesystem) is a special filesystem in Unix-like operating systems that presents information about processes and other system information in a hierarchical file-like structure, providing a more convenient and standardized method for dynamically accessing process data held in the kernel than traditional tracing methods or direct access tokernel memory. typically, it is mapped to a mount point named/procAt boot time. the proc file system acts as an interface to internal data structures in the kernel. it can be used to obtain information about the system and to change certain kernel parameters at runtime (sysctl ).

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------


Beginners with 'proc' is often confused, What Is proc? The blue section above has a profound understanding of what proc is.


Procfs is used to provide a more convenient and accurate way to dynamically read process-related data information in the kernel. This data acquisition method is different from the traditional direct trace tracing method.


The reason why proc exists before procfs is that process. The purpose of procfs is to obtain process-related data! Otherwise, why are there so many numbers in/proc? That's PID!





If we are interested in process 2802, what information can we view about this process?

Go to the directory/proc/2802.


For example, you can view the File status.

cat /proc/2802/status

Hey, it happens that this process has something to do with the Chrome browser I opened. We can know that this process is currently sleeping... and the group ID is 2802, the parent PID is 2801, and so on.



Here, I was probably impressed. "Oh ~ Proc is used to view kernel data"

Proc is incomplete and related to the process. The non-directory white part in the proc/directory. For example, modules is related to the module. It records the name of the current kernel module, this is irrelevant to a specific process.


Therefore, as wiki said:

The proc file system acts as an interface to internal data structures in the kernel. It can be used to obtain information about the system.



When I mentioned proc, I had to talk about seq_file. I tried to open seq_file separately and write a demo for Proc. But I found that the create_proc_entry interface is no longer available in the new kernel (3.13, all interfaces of Proc have completely transitioned to seq_file.


So, it's time to talk about seq_file.


Why is seq_file?


It exists as a supplement to proc, and a separate proc method is flawed! --- Only one page size of memory can be read at a time.


It may sound complicated, but, in fact, the process is quite simple. We'll step through the creation of a/proc file in the scull driver to show how it is done.

It will not be very troublesome. Here I will give a simple demo and then explain it with the demo:

/***********************************************************code writer: EOFcode date: 2014.09.01code file        : proc_demo.ce-mail: [email protected]code purpose:This code is programmed for how to create a proc-entryin procfs.If there is something wrong with my code, please touchme by e-mail. Thank you.************************************************************/#include <linux/init.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/fs.h>#include <linux/proc_fs.h> /* for procfs */#include <linux/seq_file.h>#include <linux/types.h>#define PROC_NAME "proc_demo"MODULE_AUTHOR("Jason Leaster");MODULE_LICENSE("Dual BSD/GPL");static int proc_demo_seq_show(struct seq_file* sfile,void* v){int tmp = 0;for(tmp = 0; tmp < 10;tmp++){seq_printf(sfile,"Hello world!\n");}return 0;}static void* proc_demo_seq_start(struct seq_file* sfile,loff_t *pos){return NULL;}static void proc_demo_seq_stop(struct seq_file* sfile,void* v){/* Nothing to be done. */}static void* proc_demo_seq_next(struct seq_file* sfile,void* v,loff_t* pos){return NULL;}static struct seq_operations proc_demo_ops = {.start=proc_demo_seq_start,.next=proc_demo_seq_next,.stop=proc_demo_seq_stop,.show=proc_demo_seq_show,};static int proc_demo_open(struct inode* inode, struct file* filp){return single_open(filp,&proc_demo_seq_show,NULL);}struct file_operations proc_demo_fops = {.owner=THIS_MODULE,.open=proc_demo_open,.read=seq_read,.release=seq_release,};int proc_demo_init(void){struct proc_dir_entry * entry = NULL;entry =proc_create(PROC_NAME,0,NULL,&proc_demo_fops);if(!entry){printk(KERN_ALERT "line:%d proc_create failed!",__LINE__);}return 0;}void proc_demo_exit(void){/***       The second parameter of 'remove_proc_entry()' is ** a pointer which point to parent directory.We create our** proc-entry-point in /proc/, so we pass NULL into it.*/remove_proc_entry(PROC_NAME,NULL);}module_init(proc_demo_init);module_exit(proc_demo_exit);

The demo for creating the proc file system portal is provided above, which is equivalent to a framework. The Data Reading operation here is replaced by a simple "Hello World" output to the outside. for different applications, process and output the data.

In proc_demo_open (), I used single_open (). Here we can also use seq_open.


After the module is loaded, an entry item named "proc_demo" will be created under the/proc/directory. You can use Cat to read it!



Use this simplest demo to explain how to create a proc entry


The first step, inevitably, is the prior sion of <Linux/seq_file.h>. Then you must create four iterator methods, calledstart, next, stop, and show.


The start method is always called first. The prototype for this function is:

void *start(struct seq_file *sfile, loff_t *pos);
 

The sfile argument can almost always be ignored. POS is an integer position indicating where the reading shoshould start.

The role of star is to locate the read operation of seq_file starting from that position (show later). As for the sfile parameter, you do not have to worry about it.

We don't need to worry about any special offset operations here. Reader can look at the implementation of scull and then look at the operations on proc in scull.

Pay special attention

Start, next, stop, and show.

The return values of these four functions are different, and the entry parameters are also different. Be sure to pay attention when writing programs

struct seq_operations {void * (*start) (struct seq_file *m, loff_t *pos);void (*stop) (struct seq_file *m, void *v);void * (*next) (struct seq_file *m, void *v, loff_t *pos);int (*show) (struct seq_file *m, void *v);};




References:

1: <data exchange between user space and kernel space in Linux, Part 1: procfs, seq_file, debugfs, and relayfs>

Yan Yang ([email protected]), master of Computer Science


2: proc file system --- Wikipedia



3: Driver porting: The seq_file interface [posted February 10,200 3 by Corbet]

Introduction to "procfs" (proc file system)

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.