ps (list processes) in Linux CLI - Time & Space Complexity
When we use the ps command, it lists all running processes on the system. Understanding how its execution time grows helps us know how it behaves as more processes run.
We want to find out how the time to list processes changes when the number of processes increases.
Analyze the time complexity of this command:
ps -e
This command lists every process currently running on the system.
Look at what repeats as the command runs:
- Primary operation: Reading and displaying each process's information.
- How many times: Once for each process running on the system.
As the number of processes grows, the command takes longer because it must handle more data.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 process reads and displays |
| 100 | About 100 process reads and displays |
| 1000 | About 1000 process reads and displays |
Pattern observation: The work grows directly with the number of processes; double the processes, double the work.
Time Complexity: O(n)
This means the time to run ps -e grows linearly with the number of processes running.
[X] Wrong: "The ps command runs instantly no matter how many processes there are."
[OK] Correct: The command must read each process's info, so more processes mean more work and longer time.
Knowing how commands like ps scale helps you understand system behavior and performance. This skill shows you can think about how tools work under the hood, which is valuable in many tech roles.
What if we added options to ps that filter processes before listing? How would that affect the time complexity?