Exploring the /proc file system
Experiment: Exploring the /proc Filesystem
🎯 Aim:
To use the /proc virtual filesystem to gather system information related to CPU, memory, and process statistics.
Theory:
The /proc directory is a virtual filesystem that provides an interface to kernel data structures.
It does not contain real files on disk — instead, it provides real-time system information such as:
-
CPU details
-
Memory usage
-
Process states
-
Kernel parameters
Each running process has its own directory under /proc (e.g., /proc/1, /proc/2, etc.), containing files that describe its current status.
Tasks and Commands
(a) Number of CPU Cores
Command:
Explanation:
-
/proc/cpuinfolists details of each logical CPU. -
Each CPU entry starts with the line
processor : <number>. -
grep -ccounts how many lines matchprocessor, i.e., how many cores/logical CPUs are available.
Sample Output:
➡️ Means the system has 4 CPU cores.
(b) Total Memory and Fraction of Free Memory
Command:
Sample Output:
To calculate the fraction of free memory:
awk '/MemTotal/ {total=$2} /MemFree/ {free=$2} END {print "Free Fraction = " free/total}' /proc/meminfoSample Output:
➡️ About 28.86% of memory is currently free.
(c) Number of Processes Currently Running
Command:
or using /proc directly:
Explanation:
-
Each active process has a directory
/proc/<PID>. -
Counting these directories gives the total number of active processes.
Sample Output:
➡️ There are 203 processes currently active.
(d) Number of Processes in Running and Blocked States
Command:
Sample Output:
Explanation:
-
procs_running: Number of processes currently in running state (ready to run or executing). -
procs_blocked: Number of processes waiting for I/O or blocked on resources.
➡️ At this moment, 2 processes are running and 1 process is blocked.
(e) Number of Processes Forked Since Last Boot
Command:
Sample Output:
Explanation:
-
This value represents the total number of processes created (forked) since the last boot — including all that have already terminated.
➡️ Comparison:
The number in /proc/stat (e.g., 54321) is much larger than the currently running processes (e.g., 203 in part c).
That’s because most processes have already exited, but their creation count remains in this cumulative value.
(f) Number of Context Switches Performed Since Last Bootup for a Particular Process
Command:
First, identify a process ID (say bash):
Sample output:
Now check its context switch statistics:
Sample Output:
Explanation:
-
voluntary_ctxt_switches: Number of times the process yielded CPU voluntarily (e.g., waiting for I/O). -
nonvoluntary_ctxt_switches: Number of times the process was preempted by the scheduler.
Total context switches = sum of both.
Summary Table
| Parameter | Source File | Command | Sample Output |
|---|---|---|---|
| Number of CPU cores | /proc/cpuinfo | grep -c ^processor /proc/cpuinfo | 4 |
| Total & free memory | /proc/meminfo | `grep -E "MemTotal |MemFree" /proc/meminfo` | |
| Number of active processes | /proc dirs | `ls -d /proc/[0-9]*|wc -l` | |
| Running processes | /proc/stat | grep procs_running /proc/stat | 2 |
| Blocked processes | /proc/stat | grep procs_blocked /proc/stat | 1 |
| Total forked processes | /proc/stat | grep processes /proc/stat | 54321 |
| Context switches (per process) | /proc/<pid>/status | grep ctxt /proc/2310/status | 182 / 37 |
Discussion
-
/procprovides dynamic kernel information — all values change as system activity changes. -
The number of forked processes (part e) is cumulative since boot, whereas the number of current processes (part c) is instantaneous.
-
Context switch counts show how often a process was scheduled or voluntarily released the CPU.
Result
Successfully used the /proc filesystem to extract information about:
-
CPU cores
-
Memory usage
-
Process counts and states
-
Fork statistics
-
Context switches
and understood how these values reflect the state of the system and process management in Linux.
Comments
Post a Comment