THINK FIRST·CODE LATER

← All labs

A tiny `top`: summarize a process snapshot

Problem

Linux tools such as ps and top read process information from the kernel. Write a small summary tool for an EdgeCampus edge server.

Input: first line totalMemMB. Then one process per line: PID name state cpuPercent memMB, where state is a Linux state letter: R (running/runnable), S (sleeping), D (uninterruptible sleep, usually waiting for disk), Z (zombie), T (stopped).

Print:

Processes: n (R=a S=b D=c Z=d T=e)
CPU total: x.x%
Memory used: m MB of total MB (u.u%)
Top CPU: name(PID) c.c%, name(PID) c.c%, name(PID) c.c%

"Top CPU" lists up to three processes with the highest CPU (ties: smaller PID first). Then warnings, each on its own line, in this order:

  • WARNING: zombie process name(PID) for every zombie (in input order);
  • WARNING: memory above 90% if memory use exceeds 90%;
  • WARNING: n processes waiting for disk if at least 2 processes are in state D.

If there is no warning print Status: OK.

Input:

16000
1 systemd S 0.1 12
812 nginx S 1.5 80
1204 arInference R 85.0 6200
1311 videoRecorder D 12.0 900
1402 backup D 3.0 150
1500 oldWorker Z 0.0 0

Output:

Processes: 6 (R=1 S=2 D=2 Z=1 T=0)
CPU total: 101.6%
Memory used: 7342 MB of 16000 MB (45.9%)
Top CPU: arInference(1204) 85.0%, videoRecorder(1311) 12.0%, backup(1402) 3.0%
WARNING: zombie process oldWorker(1500)
WARNING: 2 processes waiting for disk

(On a multicore machine, total CPU can exceed 100%: top counts 100% per core.)

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.