350 likes | 685 Views
The Linux /proc Filesystem. CSE8343 – Fall 2001 Group A1 – Alex MacFarlane, Garrick Williamson, Brad Crabtree. Topics. Introduction / History Problems Addressed Layout Process Information Kernel Information Kernel Configuration Implementation Programming for /proc
E N D
The Linux /proc Filesystem CSE8343 – Fall 2001 Group A1 – Alex MacFarlane, Garrick Williamson, Brad Crabtree
Topics • Introduction / History • Problems Addressed • Layout • Process Information • Kernel Information • Kernel Configuration • Implementation • Programming for /proc • Advantages & Disadvantages
Introduction • What is /proc? • A pseudo-filesystem that acts as an interface to internal data structures in the kernel • What is it used for? • Can be used to obtain information about the system • Can be used to change certain kernel parameters at runtime.
History • The idea of a Process Filesystem • Used for reporting process information only • Seen in UNIXes such as Solaris • /proc extends the concept • A similar implementation available for various flavors of BSD, including FreeBSD • /proc for Linux is the most actively developed
The Problem • Modern kernel is highly complex • Linux kernel has device drivers built-in • An enormous amount of status information • Many run-time configurable parameters • How do we allow controlled access to kernel data and parameters and provide a familiar interface that programmers can easily adopt?
The Solution • Create pseudo-filesystem to represent status information and configuration parameters as files • Provides a unified ‘API’ for collecting status information and configuring drivers • Control access through UNIX permissions • No new libraries needed – simple filesystem calls are all that is necessary • Quick, easy access via command line • Not version- or configuration-specific
/proc Layout • Two major subdivisions • Read-only files/directories • Configurable settings in /proc/sys/ • Hierarchical Subdirectories for • Network • SCSI • IDE • Device Drivers • Etc…
Process Information • Each process has a /proc directory identified by its PID - /proc/PID/ • Symlink /proc/self/ points to the process reading the file system • Allows access to • Process status • Process memory information • Links to cwd, exe, root dir • CPU and Memory Map information (2.4 only)
Configuring the Kernel • Read-write entries in /proc/sys/ • Allow for tuning, monitoring and optimization of running kernel • Modifiable only by root • Parameters may be changed simply via ‘echo’ # cat /proc/sys/fs/file-max 4096 # echo 8192 > /proc/sys/fs/file-max # cat /proc/sys/fs/file-max 8192
Configuring the Kernel (Cont’d) • Filesystem Data • Miscellaneous Binary Formats • General Kernel Parameters • Virtual Memory Subsystem • Device Specific Parameters • Remote Procedure Calls • Networking
Implementation • Linux has virtual filesystem layer (VFS) • VFS provides an abstraction layer between user processes and filesystems • Allows for any filesystem to be used transparently in the system • Filesystems don’t have to be physical • /proc fileystem resides entirely in memory
Programming for /proc • Simple filesystem representation allows for easy programming • C calls uptimefp = myfopen (PROC_DIR "uptime"); fgets (line, sizeof (line), uptimefp); new.uptime = (unsigned long) (atof (strtok (line, " ")) * (unsigned long) HZ); • Web interfaces <html><body> <? if ($fp = fopen('/proc/sys/kernel/hostname','r')) { $result = trim(fgets($fp, 4096)); echo gethostbyaddr(gethostbyname($result)); } ?> </body></html> • Shell scripts – bash, PERL, etc.
Advantages & Disadvantages • Advantages • Coherent, intuitive interface to the kernel • Great for tweaking and collecting status info • Easy to use and program for • Disadvantages • Certain amount of overhead, must use fs calls • Alleviated somewhat by sysctl() interface • User can possibly cause system instability