/proc
- C course
- Expert
- Lesson 4/47
- 5 min
Learn the /proc filesystem.
The /proc virtual filesystem
/proc is a virtual filesystem that exposes kernel and process state. It takes no disk space; content is produced on demand.
Each process has a /proc/<pid>/ directory with files like status, maps, cmdline. System-wide data lives in /proc/cpuinfo, /proc/meminfo, /proc/uptime. C programs open them like ordinary files with fopen. The kernel generates the data on the fly, so each read reflects current values.
/proc/self and dynamic reads
/proc/self is a special link that points to the calling process; you read your own info without getpid().
You read the file line by line with read or fgets, then parse it. The kernel produces formatted text; sscanf extracts fields. For instance /proc/loadavg gives five numbers. Reading too often can hurt performance, so caching is a good habit. /proc is not read-only; writing to some files changes kernel settings.
Practice Print /proc info
Define void print_proc(const char *path, const char *value) with body printf("%s: %s\n", path, value);. In main call it with ("/proc/self/status", "running") and ("/proc/cpuinfo", "x86_64").
Starter code
#include <stdio.h>
// Define print_proc function
// Call it for /proc/self/status and /proc/cpuinfo
int main(void) {
return 0;
}Sample solution
#include <stdio.h>
void print_proc(const char *path, const char *value) {
printf("%s: %s\n", path, value);
}
int main(void) {
print_proc("/proc/self/status", "running");
print_proc("/proc/cpuinfo", "x86_64");
return 0;
}Expected output
/proc/self/status: running /proc/cpuinfo: x86_64
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is a process-specific /proc path?
- /proc/cpuinfo
- /proc/meminfo
- /proc/uptime
- /proc/1234/status Correct answer
Explanation: Process-specific paths use the /proc/<pid>/ form.
2 Match /proc files to content
| Term | Meaning |
|---|---|
/proc/cpuinfo |
CPU info |
/proc/meminfo |
Memory state |
/proc/uptime |
Uptime |
/proc/self/status |
This process |
3 Open /proc/self/status
FILE *f = fopen("/proc/____", "r");
FILE *f = fopen("/proc/self/status", "r");
Explanation: /proc/self/status shows this process's state.