|
|
...making Linux just a little more fun! Exploring procfs
Why this articleI am trying to collect some tips for getting userland information from procfs. Warning: Information may be specific to kernel version 2.6. What is procfs all about?Procfs is a virtual file system in linux mounted in /proc, and serves multiple purposes including access to kernel information in userland or for debugging. One of the features which makes Linux special to me is access to process information as a text stream. A lot of linux commands (ps, top, pstree, etc.) rely on this filesystem for information. The virtual file systemThe files and directories of the /proc filesystem are virtual because the data is not actually stored on any sort of permanent storage like a hard disk; instead, the directories, files, and data within them are created dynamically in memory from raw kernel data whenever you attempt to read them. Mounting proc
Check if you already have procfs mounted on your system ( mount -t proc proc /proc Process information
Each process has an entry in the
Some examples of getting the process information are:
i) Some time back I got stuck in a tricky problem of determining whether
any particular process is doing a core dump. After some research, I noticed
that the per-process flag in 0x00000002 Process being created 0x00000004 Exiting 0x00000008 Dead 0x00000040 Process using superuser privilage 0x00000200 Process dumping core 0x00000400 Process received some signal 0x00000800 Process allocating memory 0x00001000 Killed due to out-of-memory condition
I picked up these flags from
ii) ls -l /proc/[pid]/fd/0 iii) To find the sockets being used by a process:
ls -l /proc/[pid]/fd|sed -n '/socket/{s/.*\[//;s/\]//p}'
Information about these sockets can be obtained from netstat -ae iv) To get command line arguments passed to any process: cat /proc/[pid]/cmdline v) Getting parent process ID of a process: grep PPid /proc/[pid]/status
General system informationProcfs contains a lot of system information; this includes the CPU load, the file system, and the networking configuration. Following are some examples of viewing or changing the system information using procfs: i) To find out the amount of free system memory: grep Free /proc/meminfo ii) System statistics since it was last started can be collected from /proc/stat file. To find out number of processes system had since last reboot: grep processes /proc/stat iii) To find out the one, five, and fifteen minute system load averages:
awk '{print "1 min:\t" $1 "\n5 min:\t" $2 "\n15 min:\t" $3 }' /proc/loadavg
iv)
v) echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all or to enable IP forwarding / IP Masquerading: echo 1 > /proc/sys/net/ipv4/ip_forward
vi) Mounted filesystem information can be retrieved
from vii) To change hostname on the fly, do echo www.abc.com > /proc/sys/kernel/hostname viii) To get CPU information: cat /proc/cpuinfo ix) To get swap space utilization: cat /proc/swaps x) To get the system uptime: cat /proc/uptime xi) To list the file systems being shared by NFS: cat /proc/fs/nfsd/exports A bit of kernel informationThough I intend to cover it in another article, here are some kernel tidbits: i) To get the version information for the currently-running kernel: cat /proc/version
ii) The
iii) The
To get more information, have a look
at I will try to look into this filesystem from the kernel perspective in a future article.
I am currently working for Induslogic, India. I have a Bachelor's degree in
Computer Science.
I am a strong supporter of Free Software. In my free time, I write
programs or read books. My areas of interest includes Device drivers, P2P
and operating systems. I maintain my blog at http://www.nirendra.net.
|