Lab 4.1 - Process Visualization
Objectives
By the end of this lab, you will be able to:
- Visualize processes with ps, top, htop
- Understand the displayed information
- Filter and sort processes
- Monitor processes in real-time
Prerequisites
- Ubuntu 24.04 installed
- Terminal open
- htop installed (optional but recommended)
Exercise 1: ps (Process Status)
Step 1: Basic Commands
# View all processes
ps aux
# View current user's processes
ps
# With more details
ps auxf # Process tree
# View a specific process
ps aux | grep firefox
Step 2: Useful Options
# Custom format
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -10
# Processes by user
ps -u $USER
# Processes by PID
ps -p 1
# Processes with tree
pstree
pstree -p # With PID
Step 3: Analysis
# Top 10 by CPU
ps aux --sort=-%cpu | head -11
# Top 10 by memory
ps aux --sort=-%mem | head -11
# Count processes
ps aux | wc -l
# By user
ps aux | awk '{print $1}' | sort | uniq -c | sort -rn
Exercise 2: top
Step 1: Basic Usage
# Launch top
top
# Commands in top:
# - 'q': Quit
# - 'k': Kill a process (enter PID)
# - 'r': Renice a process
# - 'M': Sort by memory
# - 'P': Sort by CPU
# - 'f': Change displayed columns
# - '1': View all CPUs
Step 2: Batch Mode
# Run top in batch mode (once)
top -bn1 | head -20
# Save to a file
top -bn1 > top-output.txt
# View top processes
top -bn1 | grep -A 20 "PID USER"
Exercise 3: htop (Enhanced)
Step 1: Installation
# Install htop
sudo apt update
sudo apt install htop
# Launch
htop
Step 2: htop Features
# htop offers:
# - Colorful and intuitive interface
# - Arrow key navigation
# - Real-time filtering (F4)
# - Search (F3)
# - Kill processes (F9)
# - Renice (F7/F8)
# - Process tree (F5)
Step 3: Configuration
# In htop:
# - F2: Setup (configuration)
# - F10: Quit
Exercise 4: Detailed Information
Step 1: /proc
# Information about a process (replace PID)
PID=$(pgrep -f firefox | head -1)
echo "PID: $PID"
# Status
cat /proc/$PID/status
# Open files
ls -l /proc/$PID/fd/
# Working directory
readlink /proc/$PID/cwd
# Executable
readlink /proc/$PID/exe
Step 2: lsof (if installed)
# Install
sudo apt install lsof
# Open files by a process
sudo lsof -p $PID
# Processes using a file
sudo lsof /var/log/syslog
# Network connections
sudo lsof -i
Exercise 5: Real-Time Monitoring
Step 1: watch
# Monitor processes
watch -n 1 'ps aux --sort=-%cpu | head -10'
# Monitor a specific process
watch -n 1 'ps -p $PID -o pid,%cpu,%mem,cmd'
Step 2: Monitoring Script
# Create a script
cat > ~/monitor-processes.sh << 'EOF'
#!/bin/bash
while true; do
clear
echo "=== $(date) ==="
echo
echo "Top 5 CPU:"
ps aux --sort=-%cpu | head -6
echo
echo "Top 5 Memory:"
ps aux --sort=-%mem | head -6
echo
echo "Total processes: $(ps aux | wc -l)"
sleep 2
done
EOF
chmod +x ~/monitor-processes.sh
# Run: ./monitor-processes.sh
Exercise 6: Advanced Filtering
Step 1: By Name
# Find all processes containing "python"
ps aux | grep python
# Exclude the grep process itself
ps aux | grep python | grep -v grep
# With pgrep
pgrep -a python
Step 2: By User
# User's processes
ps -u root
# All except root
ps aux | grep -v "^root"
Step 3: By State
# Running processes
ps aux | grep " R "
# Sleeping processes
ps aux | grep " S "
# Zombie processes
ps aux | grep " Z "
Verification
Checklist
Ensure you can:
- Use ps to view processes
- Use top for real-time monitoring
- Use htop (enhanced interface)
- Filter and sort processes
- View detailed information (/proc)
- Monitor in real-time
Reflection Questions
-
What is the difference between ps and top?
- ps: snapshot
- top: real-time, interactive
-
What do the process states (R, S, Z) mean?
- R: Running
- S: Sleeping
- Z: Zombie
-
Why is htop preferred over top?
- More intuitive interface
- Easier navigation
- More visual information
Next Steps
- Lab 4.2: Process Control
- Lab 4.3: Priorities and Nice Values
- Chapter 4.2: Process Visualization
Lab created on: December 2024 - Tested on Ubuntu 24.04