top command
Command: top (Display Linux Processes in Real Time)
๐ Purpose
The top command displays dynamic, real-time information about system processes, including:
-
Process IDs (PIDs)
-
CPU and memory usage
-
User ownership
-
System uptime
-
Load average
and much more.
It’s essentially a live dashboard for your system.
๐งฉ Basic Syntax
or simply:
๐ Example:
Typical Output:
๐งฉ Explanation of Each Section
๐ข 1. System Summary (Header)
| Field | Meaning |
|---|---|
10:41:07 | Current system time |
up 2 days, 2:21 | System uptime |
2 users | Number of logged-in users |
load average | Average number of processes waiting for CPU over 1, 5, and 15 minutes |
๐ง Load average of 1.00 on a single-core CPU means fully utilized CPU.
Values above 1.00 indicate CPU congestion.
๐ข 2. Task Summary
| Field | Meaning |
|---|---|
total | Total number of processes |
running | Currently executing processes |
sleeping | Idle processes waiting for an event |
stopped | Suspended (SIGSTOP) processes |
zombie | Defunct processes waiting to be cleaned up |
๐ง A zombie process means the process has finished execution but its parent hasn’t collected its exit status.
๐ข 3. CPU Usage
| Field | Meaning |
|---|---|
us | User mode time |
sy | System (kernel) mode time |
ni | Time with adjusted nice priority |
id | Idle time |
wa | I/O wait (time waiting for disk I/O) |
hi | Hardware interrupt time |
si | Software interrupt time |
st | Stolen time (time used by hypervisor for another VM) |
๐ง Ideal systems have high id (idle) and low wa (I/O wait).
๐ข 4. Memory Usage
| Field | Meaning |
|---|---|
total | Total physical memory (RAM) |
free | Currently unused memory |
used | Memory actively used by processes |
buff/cache | Memory used for buffers and cache (can be freed if needed) |
swap | Disk space used as virtual memory when RAM is full |
๐ง A common misconception is that high memory usage is bad — Linux uses free memory efficiently for caching.
๐ข 5. Process List (Main Table)
Each line represents one process.
| Column | Description |
|---|---|
| PID | Process ID |
| USER | Process owner |
| PR | Priority |
| NI | Nice value (used to adjust process priority) |
| VIRT | Virtual memory used (includes code + data + shared libraries) |
| RES | Resident memory (actual RAM used) |
| SHR | Shared memory with other processes |
| S | Process state (R=Running, S=Sleeping, T=Stopped, Z=Zombie) |
| %CPU | CPU usage percentage |
| %MEM | Memory usage percentage |
| TIME+ | Total CPU time used by the process |
| COMMAND | Name of the command or process |
⚙️ Interactive Keyboard Shortcuts in top
When top is running, you can interact with it using single-letter commands:
| Key | Action |
|---|---|
q | Quit |
h | Help screen |
k | Kill a process (enter PID) |
r | Renice a process (change its priority) |
P | Sort by CPU usage (default) |
M | Sort by memory usage |
T | Sort by time |
1 | Show CPU usage for each core separately |
u | Filter processes by username |
Shift + W | Save current configuration |
Shift + H | Show threads as separate processes |
Shift + F | Customize columns displayed |
๐ Example 2: Sort by Memory Usage
While in top, press:
Processes will be sorted by memory consumption, highest first.
๐ Example 3: Filter by User
To see only your processes:
Then type your username (e.g., student).
๐ Example 4: Kill a Process
In top:
Enter the PID and press Enter.
You can specify a signal number (e.g., 9 for SIGKILL).
๐ Example 5: Show All CPU Cores
Press:
This displays CPU usage for each core — useful on multi-core systems.
๐งพ Common Options (Command-Line)
| Option | Description |
|---|---|
-u <user> | Show only processes for a specific user |
-n <num> | Display for <num> iterations and exit |
-b | Batch mode (non-interactive, for logging) |
-d <delay> | Delay (in seconds) between refreshes |
-p <PID> | Monitor only specific PID |
-i | Ignore idle and zombie processes |
Example:
→ Shows processes owned by student, updates every 2 seconds, and runs 5 cycles before exiting.
๐งช Simple Lab Exercise
Aim:
To study and analyze system process information using the top command.
Procedure:
-
Run the
topcommand. -
Observe:
-
CPU usage
-
Memory usage
-
Load average
-
-
Press:
-
M→ to sort by memory -
P→ to sort by CPU usage -
1→ to show all CPU cores -
u→ to display only your processes
-
-
Kill a process:
-
Press
k→ enter PID
-
-
Exit using
q.
Sample Observation Table
| Parameter | Observation |
|---|---|
| Load Average | 0.15, 0.10, 0.05 |
| Running Tasks | 1 |
| Sleeping Tasks | 190 |
| CPU Usage (User/System) | 3.2% / 1.0% |
| Memory Usage | 2.0 GB used, 5.9 GB free |
๐ง Learning Outcomes
Students will:
-
Understand real-time process monitoring
-
Identify CPU-bound and memory-bound processes
-
Learn how to sort, filter, and kill processes
-
Understand system load, memory usage, and process states
-
Relate the output to process scheduling and resource allocation concepts in OS theory
๐งพ Quick Reference
| Command | Purpose |
|---|---|
top | Start real-time monitoring |
top -u user | Show processes of a user |
top -n 5 -d 1 | Run 5 updates, 1s apart |
M | Sort by memory usage |
P | Sort by CPU usage |
k | Kill a process |
1 | Show all CPU cores |
q | Quit top |
Comments
Post a Comment