Using top in Linux
The top command in Linux is a powerful tool for monitoring system performance and managing processes. It provides a real-time view of the system's resource usage, including CPU, memory, and process information.Starting and Stopping top
To start top, simply open a terminal and type:
Code:
top
To stop top, press:
Code:
q
Understanding Process States
- Running: These processes are actively using the CPU.
- Sleeping: These processes are not currently using the CPU but are waiting for an event (e.g., I/O operations).
- Stopped: These processes have been stopped, usually by receiving a signal.
- Zombie: These are processes that have completed execution but still have an entry in the process table. They occur when the parent process hasn't read the exit status of the terminated process.
User Processes vs. System Processes
- User Processes: These are processes initiated by users. They typically run in user space and have lower priority compared to system processes.
- System Processes: These are processes initiated by the system, often running in kernel space. They handle core system functions and usually have higher priority.
Columns in top
- PID: Process ID, a unique identifier for each process.
- USER: The user who owns the process.
- PR: Priority of the process.
- NI: Nice value, which affects the priority.
- VIRT: Virtual memory used by the process.
- RES: Resident memory (physical memory) used by the process.
- SHR: Shared memory used by the process.
- S: Process state (e.g., R for running, S for sleeping).
- %CPU: Percentage of CPU usage.
- %MEM: Percentage of memory usage.
- TIME+: Total CPU time the process has used since it started.
- COMMAND: The command that started the process.
How CPU Usage Can Exceed 100% on Multicore Systems
On multicore CPUs, the top command can show a process exceeding 100% CPU usage because it reports CPU usage relative to the total number of cores available.How CPU Usage is Calculated
Each core in a multicore CPU can be considered as having 100% capacity. So, if you have a quad-core CPU, the total capacity is 400%. Here's how it works:- Single-Core CPU: The maximum CPU usage for any process is 100%.
- Dual-Core CPU: The maximum CPU usage for any process is 200%.
- Quad-Core CPU: The maximum CPU usage for any process is 400%.
Example
If you see a process using 150% CPU on a quad-core system, it means the process is using 1.5 cores' worth of CPU time. This could be one core fully utilized and another core half utilized, or any combination that totals to 150%.Why This Happens
Modern operating systems and applications are designed to take advantage of multiple cores. When a process is multithreaded, it can run multiple threads in parallel across different cores. This parallel execution allows the process to use more than 100% of a single core's capacity, effectively distributing its workload across multiple cores.Visualizing in top
In top, the CPU usage column (%CPU) shows the percentage of total CPU capacity being used by each process. On a multicore system, this can exceed 100% because it's summing up the usage across all cores.Summary
- Single-Core: Max 100% per process.
- Dual-Core: Max 200% per process.
- Quad-Core: Max 400% per process.