linux-sysprog · intermediate · ~15 min
Reuse the labelled-field parsing pattern.
Reuse the find-the-label-then-parse-the-number pattern on the same /proc/<pid>/status text — this time for the thread count.
Implement int parse_threads(const char *status) that finds the Threads: line and returns its integer value.
status: the full text of a /proc/<pid>/status file (lines like Threads:\t7).The integer after Threads: (skip any spaces/tabs), or -1 if there is no Threads: field.
parse_threads("Name:\tcat\nVmRSS:\t10 kB\nThreads:\t7\n") -> 7
parse_threads("Name:\tx\n") -> -1
Threads: absent: return -1.status: text of a /proc/
The Threads value, or -1 if the field is absent.
Skip whitespace after the label before parsing the number.
#include <string.h>
#include <stdlib.h>
int parse_threads(const char *status) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.