Shell learns 58 days----/proc File system

Source: Internet
Author: User
Tags function prototype

/proc File System

the /proc file system in Linux consists of a set of directories and files , mounted ( Mount) with the /proc directory .

The/proc file system is a virtual file system that provides an interface to the kernel data structure in the form of file system directories and Files . This opens the door to viewing and changing various system Properties . also , It is also possible to pass a set of /proc/pid form-Named directories (PID is the ID of the process ) View information about the processes that the system rollup runs .

Typically , the contents of the file in the/proc directory are in a readable form of text , and the shell script can parse it . Program can open , Read and write /proc established documents under the catalogue . Most of the cases , only privileged processes can modify /proc contents of the file under the directory .

I. proc File System preliminary

1./proc File System

The/proc file system is a special , Software-created file system that the kernel uses to spread information around the world ./proc Each of the following files is bound to a kernel file , and when the user reads the file , the function dynamically generates the "contents" of the file.

Because /proc file system has been added a lot of information so The best way is to use the sysfs /proc file system want to skew export information

The/proc file can be used not only for reading data but also for writing data , But it is more cumbersome to write data, only to describe the usage of the data. . The method of writing data can be referenced after reading the data. Kernel Source Code

2. functions for creating /proc Files

said earlier that /proc files are all accessed in real-time to generate file content So in order to create /proc A read-only file , We must implement a function to generate data when reading a file lucky Span style= "font-family: Song body" > This function interface is designed We just need to follow the function interface to achieve our own required functions can be :

Int (*read_proc) (char *page,char **start,off_t offset,int count,int *eof,void *data);
Parameter Description :

Name of parameter

Description

Page

A buffer used to write data ; That means from /proc The unique data in the file is written to page point in the buffer

Start

The data used to specify the deeds is written to the page pointing to the memory also of the specific location

Offset

same as the parameters in the read function

Count

same as the parameters in the read function

Eof

When no data is returned , The parameter must be set to an integer , For example :* eof=1;

Data

This parameter is a dedicated pointer to the kernel feed driver and can be used for internal records

* Function of creating the system's /proc file

struct Proc_dir_entry *create_proc_read_entry (const char *name,mode_t mode, struct proc_dir_entry *base, read_proc_t * Read_proc, void * data)

Parameter Description :

Name of parameter

Description

Name

To create a file name under /proc

Mode

The mask for the file permissions created , if 0, uses the system default permissions

Base

The parent directory where the file resides , and if the parameter is null, The file will be created in the root directory of the /proc

Read_proc

The function that is called when reading a file under /proc, that is , the function explained earlier

Data

The kernel ignores date, but passes the argument to the read_proc function

To delete a /proc System file :
void Remove_proc_entry (const char *name, struct proc_dir_entry *parent)
Parameter Description :

Name of parameter

Description

Name

FileName created in the /proc file System

Parent

Parent directory Name

3. Disadvantages of using the/proc file system

(1). the Delete call may be /proc occurs when a file system file is being used

(2) The same file name may be registered two times , This error will occur

Two. Create a simple /proc file

#cd/proc; the contents of VI Read_proc//read_proc are as follows :

#include <linux/kernel.h>

#include <linux/init.h>

#include <linux/module.h>

#include <linux/proc_fs.h>

int Read_proc (char *page,char **start,off_t offset,int count,int *eof,void *data);

static int __init test_proc_init (void)

{

Create_proc_read_entry ("Read_proc", 0,null,read_proc,null);

return 0;

}

static void __exit test_proc_exit (void)

{

Remove_proc_entry ("Read_proc", NULL);

}

int Read_proc (char *page,char **start,off_t offset,int count,int *eof,void *data)

{

int len = sprintf (page, "%s\n", "Hello World");

return Len;

}

Module_init (Test_proc_init);

Module_exit (Test_proc_exit);

Module_license ("GPL");

Module_author ("Wangxq");

#cat/proc/read_proc

Hello World




Application of/proc directory

Access to this file system is the same as for general files.

Cases:

1. Count The number of CPUs :

Cat/proc/cpuinfo | grep ' physical ID ' |uniq-c|wc –l

2.CPU Model

Cat/proc/cpuinfo|grepname|cut-f2-d:|uniq

3. calculate The number of cores per CPU

Cat/proc/cpuinfo | grep ' physical id ' |awk-f ': ' {count[$2]++;} End{sum=0;for (A in count) {cc++;sum+=count[a]}printsum/cc;} '

4. Kernel version

Cat/proc/version|cut-f1-d ' ('

5. Number of context conversions performed by the kernel

Cat/proc/stat|grep Ctxt|awk ' {print $} '

6. number of processes created by the system

Cat/proc/stat|grep Processes|awk ' {print $} '

7. The amount of memory currently available

Cat/proc/meminfo|grep Memfree


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Shell learns 58 days----/proc File system

Related Article

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.