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:

grep -c ^processor /proc/cpuinfo

Explanation:

  • /proc/cpuinfo lists details of each logical CPU.

  • Each CPU entry starts with the line processor : <number>.

  • grep -c counts how many lines match processor, i.e., how many cores/logical CPUs are available.

Sample Output:

4

➡️ Means the system has 4 CPU cores.


(b) Total Memory and Fraction of Free Memory

Command:

grep -E "MemTotal|MemFree" /proc/meminfo

Sample Output:

MemTotal: 8123456 kB MemFree: 2345678 kB

To calculate the fraction of free memory:

awk '/MemTotal/ {total=$2} /MemFree/ {free=$2} END {print "Free Fraction = " free/total}' /proc/meminfo

Sample Output:

Free Fraction = 0.2886

➡️ About 28.86% of memory is currently free.


(c) Number of Processes Currently Running

Command:

ps -e --no-headers | wc -l

or using /proc directly:

ls -d /proc/[0-9]* | wc -l

Explanation:

  • Each active process has a directory /proc/<PID>.

  • Counting these directories gives the total number of active processes.

Sample Output:

203

➡️ There are 203 processes currently active.


(d) Number of Processes in Running and Blocked States

Command:

grep "procs_running" /proc/stat grep "procs_blocked" /proc/stat

Sample Output:

procs_running 2 procs_blocked 1

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:

grep "processes" /proc/stat

Sample Output:

processes 54321

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):

ps -C bash

Sample output:

PID TTY TIME CMD 2310 pts/0 00:00:03 bash

Now check its context switch statistics:

cat /proc/2310/status | grep ctxt

Sample Output:

voluntary_ctxt_switches: 182 nonvoluntary_ctxt_switches: 37

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

ParameterSource FileCommandSample Output
Number of CPU cores/proc/cpuinfogrep -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/statgrep procs_running /proc/stat        2
Blocked processes/proc/statgrep procs_blocked /proc/stat        1
Total forked processes/proc/statgrep processes /proc/stat        54321
Context switches (per process)/proc/<pid>/statusgrep ctxt /proc/2310/status    182 / 37

Discussion

  • /proc provides 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.

Bash Script

#!/bin/bash

echo "---------------------------------------------"
echo " System Information Using /proc File System"
echo "---------------------------------------------"

echo ""
echo " (a) Number of CPU cores:"
cores=$(grep -c ^processor /proc/cpuinfo)
echo "     CPU Cores: $cores"

echo ""
echo " (b) Total memory and fraction of free memory:"
total_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
free_mem=$(grep MemFree /proc/meminfo | awk '{print $2}')
available=$(grep MemAvailable /proc/meminfo | awk '{print $2}')

echo "     Total Memory     : $total_mem kB"
echo "     Free Memory      : $free_mem kB"
echo "     Available Memory : $available kB"
fraction=$(echo "scale=2; $free_mem/$total_mem" | bc)
echo "     Fraction Free    : $fraction"

echo ""
echo " (c) Number of processes currently running:"
running=$(ps -e --no-headers | wc -l)
echo "     Total Processes: $running"

echo ""
echo " (d) Number of processes in Running and Blocked states:"
running_state=$(ps -eo stat | grep -c "^R")
blocked_state=$(ps -eo stat | grep -c "^D")
echo "     Running Processes : $running_state"
echo "     Blocked (Uninterruptible Sleep) Processes: $blocked_state"

echo ""
echo " (e) Number of processes forked since last boot:"
forks=$(grep processes /proc/stat | awk '{print $2}')
echo "     Processes Forked Since Boot: $forks"


echo ""
echo " (f) Number of context switches for a particular process:"
echo -n "     Enter PID: "
read pid

if [ -d "/proc/$pid" ]; then
    ctxt=$(grep ctxt /proc/$pid/status)
    echo "     $ctxt"
else
    echo "     Invalid PID — process does not exist."
fi

echo ""
echo "---------------------------------------------"
echo " Script Completed."
echo "---------------------------------------------"

Output
---------------------------------------------
 System Information Using /proc File System
---------------------------------------------

 (a) Number of CPU cores:
     CPU Cores: 16

 (b) Total memory and fraction of free memory:
     Total Memory     : 16106600 kB
     Free Memory      : 414276 kB
     Available Memory : 14090608 kB
     Fraction Free    : .02

 (c) Number of processes currently running:
     Total Processes: 360

 (d) Number of processes in Running and Blocked states:
     Running Processes : 1
     Blocked (Uninterruptible Sleep) Processes: 0

 (e) Number of processes forked since last boot:
     Processes Forked Since Boot: 83505071
    

 (f) Number of context switches for a particular process:
     Enter PID: 1
     voluntary_ctxt_switches:   9936111
     nonvoluntary_ctxt_switches:     40526

---------------------------------------------
 Script Completed.
---------------------------------------------

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

ps command