System processes and memory usage

4.3 Where Data is Stored (Weight: 3)

📘Linux Essentials (LPI 010-160)


1. What is a Process?

  • A process is basically a program that is running.
  • Examples of processes:
    • Running a text editor like gedit or nano.
    • A web server process like apache2.
    • A backup script running automatically at night.
  • Each process has:
    • A unique ID called PID (Process ID).
    • Memory usage: How much RAM it is using.
    • CPU usage: How much processor power it is using.
    • State: Running, sleeping, stopped, etc.

2. Viewing Processes

Linux provides commands to see which processes are running:

  1. ps (Process Status)
    • Shows current running processes.
    • Example: ps aux
      • a – shows all users’ processes
      • u – shows user/owner
      • x – includes background processes
  2. top
    • Real-time monitoring of processes.
    • Shows CPU and memory usage for each process.
    • Example output columns:
      • PID – Process ID
      • USER – Owner of process
      • %CPU – CPU usage
      • %MEM – Memory usage
      • COMMAND – Name of program
  3. htop (if installed)
    • Better, interactive version of top.
    • You can scroll, sort, and kill processes easily.

3. Process States

A process can be in different states:

StateMeaning
R (Running)Actively using CPU
S (Sleeping)Waiting for input or a resource
T (Stopped)Paused (manual or system signal)
Z (Zombie)Finished but still in process table

IT example: A web server process may sleep while waiting for a request from a user.


4. Memory Usage in Linux

Memory is where data is stored temporarily while processes are running. In Linux, memory usage is split into:

  1. RAM (Random Access Memory)
    • Stores active processes and data temporarily.
    • Faster than storage drives.
  2. Swap Space
    • Extra memory on disk used when RAM is full.
    • Slower than RAM.
  3. Cached Memory
    • Linux keeps frequently used data in memory for faster access.
    • It’s free for processes if needed.

5. Checking Memory Usage

Linux provides tools to see memory usage:

  1. free
    • Shows RAM and swap usage.
    • Example: free -h
      • -h makes it human-readable (GB/MB)
    • Output: total used free shared buff/cache available
      8G 3G 2G 0.1G 3G 4.5G
  2. top or htop
    • Also shows memory per process.
    • Useful for spotting processes using too much memory.
  3. vmstat
    • Shows detailed memory, CPU, and process stats.
    • Example: vmstat 2 5
      • Updates stats every 2 seconds, 5 times.

6. Managing Processes

Sometimes you may need to stop or restart a process:

  1. kill
    • Stop a process using its PID.
    • Example: kill 1234
      • 1234 is the PID of the process.
  2. killall
    • Stop all processes by name.
    • Example: killall firefox
  3. nice and renice
    • Control CPU priority for processes.
    • Example: Giving a backup script lower CPU priority: nice -n 10 ./backup.sh

7. IT Examples of Process & Memory Management

  • Web server (Apache/Nginx):
    • Each request can start a new process/thread.
    • System memory must be monitored to avoid crashes.
  • Database server (MySQL/PostgreSQL):
    • Heavy queries may consume a lot of RAM.
    • Swap may be used if RAM is full, slowing down performance.
  • Automated backup scripts:
    • Scheduled via cron.
    • Low CPU priority using nice to avoid slowing other services.

8. Key Exam Points

For LPI 010-160, you need to know:

  1. What a process is and PID.
  2. How to view processes (ps, top, htop).
  3. Process states (R, S, T, Z).
  4. Memory types: RAM, swap, cache.
  5. Commands to check memory (free, vmstat, top).
  6. How to manage processes (kill, killall, nice, renice).

Summary Table for Quick Revision

TopicCommand / Info
View processesps aux, top, htop
Process IDPID
Process statesR, S, T, Z
Memory usagefree -h, top, vmstat
Stop processkill PID, killall name
Change CPU prioritynice, renice
Buy Me a Coffee