top and htop (live monitoring) in Linux CLI - Time & Space Complexity
When using live monitoring tools like top and htop, it's important to understand how their performance changes as the system load grows.
We want to know how the cost of updating the display scales with the number of running processes.
Analyze the time complexity of the following simplified command loop from top or htop:
while true; do
ps -eo pid,comm,pcpu,pmem --sort=-pcpu | head -n 20
sleep 1
done
This loop fetches and displays the top 20 CPU-consuming processes every second.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Listing all processes and sorting them by CPU usage.
- How many times: This happens once every second, repeatedly.
As the number of running processes increases, the system must sort more items before showing the top 20.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Sorting 10 processes |
| 100 | Sorting 100 processes |
| 1000 | Sorting 1000 processes |
Pattern observation: The sorting work grows as the number of processes grows, making updates slower with more processes.
Time Complexity: O(n log n)
This means the time to update the display grows a bit faster than the number of processes because sorting takes more work as the list grows.
[X] Wrong: "The update time stays the same no matter how many processes run."
[OK] Correct: Sorting all processes takes more time as the list grows, so updates slow down with more processes.
Understanding how live monitoring tools scale helps you think about performance in real systems, a useful skill for many tech roles.
What if the tool only scanned the top 20 processes without sorting all? How would the time complexity change?
Practice
top command in Linux?Solution
Step 1: Understand the function of
Thetoptopcommand shows real-time information about system processes and resource usage.Step 2: Compare with other options
Options B, C, and D relate to different commands or tools, nottop.Final Answer:
To display live system and process information -> Option AQuick Check:
top = live system info [OK]
- Confusing top with text editors like nano
- Thinking top manages users
- Assuming top compiles code
htop in the terminal?Solution
Step 1: Identify correct syntax to run
The commandhtophtopruns the program; adding&runs it in background.Step 2: Check other options for correctness
Options B, C, and D are not valid Linux commands to starthtop.Final Answer:
htop & -> Option CQuick Check:
Run htop with 'htop &' [OK]
- Using invalid flags like -start
- Typing 'start htop' which is Windows style
- Using 'run htop' which is not a Linux command
top and see a process using 95% CPU. What does this indicate?Solution
Step 1: Interpret CPU usage in
High CPU percentage means the process is actively using CPU resources.topStep 2: Understand other states
Sleeping or waiting processes use little CPU; stopped processes do not use CPU.Final Answer:
The process is using most of the CPU power -> Option DQuick Check:
High CPU% = active process [OK]
- Thinking high CPU means sleeping
- Confusing waiting with stopped
- Assuming stopped processes use CPU
htop but get: command not found. What should you do?Solution
Step 1: Understand 'command not found' error
This meanshtopis not installed or not in PATH.Step 2: Fix by installing
Use package manager likehtopsudo apt install htoporsudo yum install htopto install.Final Answer:
Install htop using the package manager -> Option BQuick Check:
command not found = install missing tool [OK]
- Restarting without installing
- Ignoring the error and running top
- Not checking spelling before install
htop. What steps should you follow?Solution
Step 1: Sort processes by memory usage in
PresshtopF6or click the memory column to sort descending by memory.Step 2: Select the top memory-consuming process and kill it
Use arrow keys to select process, then pressF9to kill it.Final Answer:
Sort by memory, select process, press F9 to kill -> Option AQuick Check:
htop kill = sort mem + F9 [OK]
- Using wrong function keys to kill
- Sorting by CPU instead of memory
- Trying to kill by PID without selecting
Ready to prove it?
Pass the test and this pattern moves unknown
