Part 6 of 6 · 2 min read
Exercises
Easy
Easy 1: Find the file (20 minutes)
Name the file for: the command line of process 500, the limits of process 500, total memory of the machine.
Done when: /proc/500/cmdline, /proc/500/limits, /proc/meminfo.
Hint
the table in section 2.
Easy 2: Ticks to seconds (20 minutes)
Convert 105,827 idle ticks to seconds with 100 ticks per second.
Done when: 1,058.27 s.
Hint
divide by 100.
Easy 3: Share (20 minutes)
A machine shows user 300, system 100, idle 600 ticks. What share is idle?
Done when: 600 / 1,000 = 60 per cent.
Hint
idle divided by the total.
Medium
Medium 1: Two readings (45 minutes)
Read the first line of /proc/stat twice, ten seconds apart, and compute the idle share of the interval.
Done when: two lines, the differences of each field, and a share between 0 and 100 per cent.
Hint
subtract the first reading from the second, field by field.
Medium 2: Run the lister (45 minutes)
Compile the program of section 4. Start sleep 100 & and find its line in the output.
Done when: a line with the sleep's PID, state S, a small RSS and the name sleep.
Hint
./minips | grep sleep.
Medium 3: Read yourself (45 minutes)
Run cat /proc/self/status | head -12 and explain Name, State, Pid, PPid, Uid.
Done when: each of the five fields explained in one line.
Hint
see man 5 proc, the section on status.
Hard
Hard 1: Count threads (90 minutes)
Extend the program to print the Threads field and the total number of threads of the machine. Compare with ps -eLf | wc -l minus one.
Done when: a total close to the ps number, with a note on why the two can differ by a few.
Hint
Threads: is a line in status.
Hard 2: Open files per process (90 minutes)
Write a program that, for each process, counts the entries in /proc/PID/fd and prints the five largest counts. Explain any permission failure.
Done when: a list of five processes with counts, and a sentence that reading another user's fd needs the right privilege.
Hint
opendir on /proc/PID/fd and count entries other than . and ...
Hard 3: A load watcher (90 minutes)
Write a shell loop that reads /proc/loadavg and /proc/meminfo every 5 seconds for one minute and prints the three load averages and MemAvailable in MiB. Run it while you start a busy loop.
Done when: 12 lines, and the one-minute load rising during the busy loop.
Hint
use awk '{print $1,$2,$3}' /proc/loadavg and awk '/MemAvailable/ {print $2/1024}' /proc/meminfo.