![](https://static.wixstatic.com/media/64c1ee_5284c704d5dd4a6987d0af9e681f86d1~mv2.jpg/v1/fill/w_665,h_665,al_c,lg_1,q_85/64c1ee_5284c704d5dd4a6987d0af9e681f86d1~mv2.jpg)
Procfs, short for process filesystem, is a virtual filesystem in Linux that provides access to kernel and process information. In this article, we'll explore the inner workings of procfs, its structure, common conventions, and practical examples to demonstrate its usage.
1. Understanding Procfs:
- Procfs is mounted at `/proc` and acts as a window into the kernel's internal data structures, exposing information about running processes, system configuration, and kernel parameters.
- It presents a hierarchical view of system and process-related information as directories and files, allowing users and applications to query and manipulate kernel state.
2. Structure of Procfs:
- Procfs organizes information into a tree-like structure, with each directory representing a process or system entity.
- Directories typically have numeric names corresponding to process IDs (PIDs), while certain files contain system-wide information or configuration parameters.
3. Common Procfs Entities:
- Process Information: Details about running processes, including PID, command name, memory usage, and CPU utilization.
- System Information: Configuration parameters, kernel version, memory statistics, and hardware information.
- Kernel Parameters: Access to kernel configuration and runtime parameters, such as network settings, scheduler parameters, and filesystem options.
4. Example: Retrieving Process Information from Procfs
- Let's demonstrate how to retrieve information about a specific process (e.g., PID 1234) using procfs in a Bash script:
#!/bin/bash
pid="1234"
procfs_path="/proc/$pid"
if [ -d "$procfs_path" ]; then
echo "Process Information (PID $pid)"
cat "$procfs_path/cmdline" # Command line
cat "$procfs_path/status" # Process status
cat "$procfs_path/statm" # Memory usage
cat "$procfs_path/stat" # Process statistics
else
echo "Process not found: PID $pid"
fi
5. Example: Modifying Kernel Parameters via Procfs
- Let's demonstrate how to modify a kernel parameter (e.g., swappiness) using procfs in a Bash script:
#!/bin/bash
parameter="vm.swappiness"
value="60"
echo "Setting $parameter to $value"
echo "$value" > "/proc/sys/$parameter"
- Procfs serves as a valuable tool for accessing kernel and process information in Linux systems.
- By interacting with procfs, users and applications can query process details, system configuration, and kernel parameters, as well as modify runtime settings.
- With practical examples, we've demonstrated how to leverage procfs in Bash scripts to retrieve process information and adjust kernel parameters, showcasing its utility and versatility in system administration and development tasks.
#linuxdevicedrivers #ldd #linuxlovers
![](https://static.wixstatic.com/media/64c1ee_5284c704d5dd4a6987d0af9e681f86d1~mv2.jpg/v1/fill/w_100,h_100,al_c,q_80,usm_0.66_1.00_0.01,blur_2,enc_auto/64c1ee_5284c704d5dd4a6987d0af9e681f86d1~mv2.jpg)