Friday 12 September 2014

Procfs filesystem

Procfs  (/proc)

The /proc file system is a pseudo file system that provides an interface to kernel data structures in a form that looks like files and directories on a file system. This provides an easy mechanism for viewing and changing various system attributes.

The contents of /proc files are generally in human-readable text form and can be parsed by shell scripts. A program can simply open and read from, or write to, the desired file. In most cases, a process must be privileged to modify the contents of files in the /proc directory.

 This file system resides under the /proc directory and contains various files that expose kernel information, allowing processes to conveniently read that information, and change it in some cases, using normal file I/O system calls.
The /proc file system is said to be virtual because the files and subdirectories that it contains don’t reside on a disk. Instead, the kernel creates them “on the fly” as processes access them.


Content of /proc filesystem
.  
For each process on the system, the kernel provides a corresponding directory named /proc/PID, where PID is the ID of the process. Within this directory are various files and subdirectories containing information about that process.

For example,
We can obtain information about the init process, which always has the process ID 1, by looking at files under the directory /proc/1.

Major content of Proc filesystem is  

Directory                  Information exposed by files in this directory
/proc                          various system information
/proc/net                               Status information about networking and sockets
/proc/sys/fs                         Settings related to file systems
/proc/sys/kernel                 Various general kernel settings
/proc/sys/net                        Networking and sockets settings
/proc/sys/vm                       Memory-management settings


This figure will help you ti understand content of procfs
      
      




Procfs often accessed by shell script using “cat” and “echo “. Proc can also be access by c program using simple file I/O system calls.  We should be take care access permission of proc while accessing them.

We can create our own Procfs entry by writing linux kernel module, Sample tutorial for this is  available here.

Reference: Linux Programming interface

No comments:

Post a Comment