Part 5 of 6 · 4 min read
A lister in C
A process lister in C
Think of a census taker who walks along a street and writes down who lives in each house. The taker needs no special list, only the houses. The analogy stops in one place: houses stay, and processes can end between the knock and the answer.
In plain words: The program below walks the directory /proc, keeps the entries whose names are numbers (the processes), reads each status file and prints the state, resident memory and name. A process may end between the directory listing and the open, so a failed open is skipped.
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
int main(void) {
DIR *d = opendir("/proc");
struct dirent *e;
printf("%6s %-6s %8s %s\n", "PID", "STATE", "RSS_kB", "NAME");
while ((e = readdir(d))) {
if (!isdigit((unsigned char)e->d_name[0])) continue; /* only numeric = processes */
char path[300], line[256], name[64] = "?", state[16] = "?";
long rss = 0;
snprintf(path, sizeof path, "/proc/%s/status", e->d_name);
FILE *f = fopen(path, "r");
if (!f) continue; /* process ended meanwhile */
while (fgets(line, sizeof line, f)) {
sscanf(line, "Name: %63s", name);
sscanf(line, "State: %15s", state);
sscanf(line, "VmRSS: %ld", &rss);
}
fclose(f);
printf("%6s %-6s %8ld %s\n", e->d_name, state, rss, name);
}
closedir(d);
return 0;
}
Output on the test machine (first lines):
PID STATE RSS_kB NAME
1 S 5684 process_api
2 S 0 kthreadd
3 S 0 pool_workqueue_release
The program found 68 lines, including the header, and ps -e printed 68 lines too (a header and 67 processes), so the two agree. Processes 2 and 3 have RSS 0: they are kernel threads, which have no user memory (Level 1), and the line VmRSS does not exist for them, so the value stays at its start value 0. The name kthreadd is the kernel thread that creates the others.
The key lines. This is the whole idea of ps: list the numbers in /proc and read files. A tool that fails can often be replaced by ls, cat and grep on the same files.
Kernel thread
A kernel thread is a task that runs only in kernel mode and has no user address space. It appears in the process list with a name in brackets in some tools, has no resident user memory, and does background work for the kernel.
Example: kthreadd (process 2) creates other kernel threads.
Common mistake
Parsing the output of ps in a script and depending on its column positions or the spaces in a command name. Read the specific file in /proc or use ps -o with named fields and = to remove headers.
Check yourself:
1. How does the program tell a process directory from other entries?
2. Why can the open of a status file fail?
3. Why is the RSS of kthreadd zero?
Words you will meet
| Word | Meaning |
|---|---|
| procfs / sysfs | Special file systems for processes and system state, and for devices. |
/proc/self |
A link to the reading process's own directory. |
| Tick | 1/100 s unit of CPU time counters. |
| Kernel thread | A task with no user memory that works for the kernel. |
MemAvailable |
An estimate of memory that new programs can use. |
Quick notes
/procand/syshold no data on disk. Each read makes the content from kernel tables./proc/PID/:status,cmdline,cwd,exe,fd/,limits,maps,io,ns/,cgroup.- System files:
meminfo,loadavg,stat,interrupts,mounts. - CPU time counters are in ticks of 1/100 s and only grow. A share needs two readings.
ps,freeanduptimeread these files.- A short C program can list processes from
/proc.