Build Your Own Top
This challenge is to build your own version of the command line tool top.
The Unix command line tools are a great metaphor for good software engineering and they follow the Unix Philosophies of:
- Writing simple parts connected by clean interfaces - each tool does just one thing and provides a simple CLI that handles text input from either files or file streams.
- Design programs to be connected to other programs - each tool can be easily connected to other tools to create incredibly powerful compositions.
Following these philosophies has made the simple unix command line tools some of the most widely used software engineering tools - allowing us to create very complex text data processing pipelines from simple command line tools.
Top is both an example of this and a chance to further dig into and understand the "Everything is a file" is an approach to interface design in Unix and its derivatives.
The Challenge - Building Top
The high-level functional requirements for top
are concisely described by it’s man page - give it a go in your local terminal now:
% man top
NAME
top – display sorted information about processes
DESCRIPTION
The top program periodically displays a sorted list of system processes.
The default sorting key is pid, but other keys can be used instead.
Various output options are available.
Step Zero
Like all good software engineering we’re zero indexed! In this step you’re going to set your environment up ready to begin developing and testing your solution.
I’ll leave you to setup your IDE / editor of choice and programming language of choice. After that if you’re running on Windows or Mac OS X you’ll need either a Linux VM, Docker, or WSL so that you can test your version of top.
Step 1
In this step your goal is to create a cctop
program that can be started and quit by pressing q. For now just have it start up and create a heading at the top of the terminal, with the current time in the top right hand corner.
The program should:
- Read from stdin unbuffered - so you’re not waiting for the user to press return.
- Disable the default echoing of input to stdout.
- Disable output processing by the terminal.
This is commonly known as setting the terminal into raw mode as opposed to cooked mode - you can read more about terminal modes here.
This is similar to the build a text editor coding challenge, though as it’s not a key part of this coding challenge you could also look into a library that handles it for you, i.e. ncurses or the equivalent for your programming language.
Step 2
In this step your goal is to show the current CPU and memory usage.
You can find the CPU info in /proc/cpuinfo
but to get the usage you’ll need to parse /proc/stat
you’ll need to read through the man page for /proc/stat
to understand it.
You’ll find the memory usage in /proc/meminfo
the relevant man page is here.
As you’ll need to take multiple readings to get the values, now is a great time to make your UI refresh periodically with the new values - don’t forget the time.
Step 3
In this step your goal is to show the breakdown of CPU and memory usage, for example:
Load Avg: 4.86, 5.20, 5.44 CPU usage: 6.74% user, 10.1% sys, 83.23% idl
PhysMem: 15G used (2650M wired, 7711M compressor), 71M unused.
Step 4
In this step your goal is to list the running processes, for example:
PID COMMAND %CPU TIME #TH #WQ #PORT MEM PURG CMPRS PGRP
0 kernel_task 109. 145 hrs 639/8 0 0 77M 0B 0B 0
75367 Google Chrom 28.2 12:11:16 44 1 9476- 1642M+ 0B 1186M- 75367
14857 top 17.6 00:07.54 1/1 0 29 11M+ 0B 3712K- 148
By now I’m sure you’ll have guess that all this information is in the /proc
filesystem and detail in the man pages.
Step 5
In this step you goal is to allow the user of your cctop
to toggle the units used to display the memory - this is the equivalent to the -M
command line option or pressing e on some systems.
Going Further
If you want to take this further, work through the features of top, or look at more advanced alternatives like htop
and add the features that seem useful and interesting to you.
Personally I’ve often used the ability to show a single process and the threads related to it, so I’d start there.
Help Others by Sharing Your Solutions!
If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message on the Discord Server, via Twitter or LinkedIn or just post about it there and tag me. Alternately please add a link to it in the Coding Challenges Shared Solutions Github repo.
Get The Challenges By Email
If you would like to receive the coding challenges by email, you can subscribe to the weekly newsletter on SubStack here: