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

top [options]

or simply:

top

๐Ÿ“˜ Example:

top

Typical Output:

top - 10:41:07 up 2 days, 2:21, 2 users, load average: 0.23, 0.14, 0.09 Tasks: 198 total, 1 running, 197 sleeping, 0 stopped, 0 zombie %Cpu(s): 3.2 us, 1.0 sy, 0.0 ni, 95.6 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st MiB Mem : 7989.7 total, 2567.4 free, 1834.1 used, 3588.2 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 5332.1 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1254 root 20 0 700484 8236 5852 S 1.7 0.1 0:04.76 systemd 2305 user1 20 0 159720 6508 5152 S 0.3 0.1 0:00.92 bash 2410 user1 20 0 242348 8652 7068 R 0.3 0.1 0:00.45 top

๐Ÿงฉ Explanation of Each Section

๐ŸŸข 1. System Summary (Header)

top - 10:41:07 up 2 days, 2:21, 2 users, load average: 0.23, 0.14, 0.09
FieldMeaning
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

Tasks: 198 total, 1 running, 197 sleeping, 0 stopped, 0 zombie
FieldMeaning
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

%Cpu(s): 3.2 us, 1.0 sy, 0.0 ni, 95.6 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
FieldMeaning
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

MiB Mem : 7989.7 total, 2567.4 free, 1834.1 used, 3588.2 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 5332.1 avail Mem
FieldMeaning
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.

ColumnDescription
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:

KeyAction
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:

M

Processes will be sorted by memory consumption, highest first.


๐Ÿ“˜ Example 3: Filter by User

To see only your processes:

u

Then type your username (e.g., student).


๐Ÿ“˜ Example 4: Kill a Process

In top:

k

Enter the PID and press Enter.
You can specify a signal number (e.g., 9 for SIGKILL).


๐Ÿ“˜ Example 5: Show All CPU Cores

Press:

1

This displays CPU usage for each core — useful on multi-core systems.


๐Ÿงพ Common Options (Command-Line)

OptionDescription
-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:

top -u student -n 5 -d 2

→ 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:

  1. Run the top command.

    top
  2. Observe:

    • CPU usage

    • Memory usage

    • Load average

  3. Press:

    • M → to sort by memory

    • P → to sort by CPU usage

    • 1 → to show all CPU cores

    • u → to display only your processes

  4. Kill a process:

    • Press k → enter PID

  5. Exit using q.


Sample Observation Table

ParameterObservation
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

CommandPurpose
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

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command