Process Management

Understanding Processes: What They Are and How Linux Manages Them
a process is software which has been loaded into memory
currently executed by the CPU

Linux is a multitasking system
a single CPU core can only do one thing at a time
ilusion of multi-tasking is created by the Scheduler
switches billions of times per second

The Process ID (PID)
kernel assigns unique sequential number to each process
IDs start with 1 on boot

User-Space vs. Kernel-Space
processes generally live in the user space
have limited powers
interacts with the kernel to talk with hardware
a crash in the user space does no affect the kernel or hardware

Viewing Running Processes: ps, top, htop
The Snapshot: ps
ps command stands for Process Status
takes a static photo of what is happening
short list from a user's terminal
 PID TTY          TIME CMD
1234 pts/0    00:00:00 bash
1255 pts/0    00:00:00 ps
only shows processes running in the current terminal session
usually want to see everything running on the system
use a standard combination of option aux
  • a - show processes for all users
  • u - display user/owner of the process
  • x - show processes not attached to a terminal

returns massive list
the columns in the list
  • USER - account running the process
  • PID - process ID
  • %CPU - how much processing power being used
  • %MEM - how much RAM s being used
  • VSZ / RSS - technical memory metricss
  • TTYthe terminal connected to
    if not connected to a terminal a ? is displayed
  • STAT - state of the process
  • START - the time started
  • TIME - total CPU time consumed
  • COMMAND - name of the program
The Dashboad: top
top is a video feed of same information in real-time
updates every ~3 seconds
display has two parts
  • The Header - shows uptime, load average and total memory usage
  • The List - list of processes sorted by CPU usage
navigation in top
  • Shift + m to sort by memory usage
  • Shift + p to sort by CPU usage
  • k to kill a process
  • q to quit

The Friendly Dashboard: htop
htop is a modern alternative to top
to install
sudo apt install htop
Understanding Process Hierarchy: Parent and Child Processes
every process is created by another process
creates a family tree

The Ancestor: PID 1
when booted the kernel starts one process systemd with PID 1
PID 1 the starts other essential services
  1. starts network manager, login screen, etc.
  2. login screen starts the desktop environment
  3. desktop environment starts your terminal
  4. terminal starts the Bash shell
  5. Bash shell starts Firefox

Bash is the parent of Firefox Firefox is the child of Bash

Why Hierarchy Matters
when a child process finishes its task, it notifies its parent
parent then cleans up the memory the child was using
when a parent terminates usually its children will be terminated as well

Process States: Running, Sleeping, Stopped, Zombie
process is not always using the CPU
has different states
  • R (Running or Runnable) - is currently using CPU or is in the queue
    is active and healthy
  • S (Sleeping) - waiting for something to happen
  • T (Stopped) - process has been paused
    the scheduler ignores process untilit resumes
  • Z (Zombie) - technical term for an defunct process
    taks has been completed entry is still in process table
    child is finished but parent has yet to acknowledge and release the process' memory
    can't kill a zombie because it has no parent
    usually have to restart the parent process
Managing Processes: kill, killall, pkill
The Precision Strike (kill)
utility for sending signals to processes
need to us PID
kill <process ID>
The Name Strike (killall)
just know the name of the program
killall firefox
will terminate every process named 'firefox'

The Pattern Strike (pkill)
use a partial name
pkill fire
kills every process which contains 'fire' in its name

Understanding Signals: SIGTERM, SIGKILL, and Others
kill command sends a signal to the process
the process determines how to handle the message
Linux has dozens of standard signals
signals referenced by name or number
below firefox is assigned PID 4521
essential signals

SIGTERM (Signal 15) - The Polite Request
the default signal sent by the kill command
when a process receives SIGTERM then has a chance to save its work, close its open files, disconnect from the network, and exit cleanly
kill 4521
defaults to -15

SIGKILL (Signal 9) - The Ultimate Sanction
sometimes a process can't hear the SIGTERM request
stuck in a loop and ignoring the world
use SIGKILL
SIGKILL is not sent to the process
sent to the Linux Kernel to focre process shutdown
it can lead to data loss or corrupted files
kill -9 4521
SIGHUP (Signal 1) - The Reload
SIGHUP stands for 'hang up'
tell a background service to reload its configuration
no need to stop and restart the service to apply changes
kill -1 4521
SIGINT (Signal 2) - The Interrupt
sends a stop what you're doing to a foreground command
Ctrl+C

Foreground and Background Processes: &, fg, bg, jobs
when a process runs in the terminal it runs in the foreground
the terminal is 'occupied' until the process completes
Starting in the Background (&)
to start a process in the background add an ampersand & to the end of the command
cp massive_video.mp4 /backup/ &
the copy runs in the background and the prompt returns immediately

Pausing and Moving (Ctrl-Z, bg, fg)
  1. to pause a foreground process press Ctrl-Z
    terminal returns
    <job ID> Stopped <command>
    process state becomes Stopped (T)
    process is paused
  2. to move process to the background enter bg
    system adds &
    process wakes up and starts running in the background
  3. to move process to the foreground enter fg
    if multiple background processes are running use the job number to identify the proper process
    fg %<job ID>

Listing Jobs (jobs)
to see what is running in the background of current shell use the jobs command
[1]- Running cp massive_video.mp4 /backup/ & 
[2]+ Stopped nano notes.txt
shows the job ID for each process

background processes created from a terminal are attached to the shell
if the terminal is closed, its background processes will all terminate

Process Priority: nice and renice
CPU switches between tasks
doesn't always treat tasks equally
more attention to important tasks than to unmportant ones
niceness value determines priority
a nice process lets others go first
a not nice process pushes to the front of the line

The Niceness Scale
runs from -20 to 19
  • -20 : least nice
    high priority
    demands CPU time
  • 0 : default
  • +19 : most nice
    low priority
    only runs when nothing else wants CPU time
Setting Priority at Start (nice)
should run script which will slow down computer with a high niceness value
nice -n 10 tar -czf backup.tar.gz /home/anna
tells system to run the backup but be nice

Changing Priority of Running Processes (renice)
if a process is running and slowing down the system, can make it nicer without stopping it
need the PID
renice 10 -p 4521
changes the process' niceness to 10

The Root Restriction
standard users can increase the niceness of their own processes
only the root user can decrease niceness

Real-World Scenario: The Runaway Script
shared server is sluggish
delay experienced while typing in terminal
  1. Diagnosis - run top
    process named foobar is at top of list with 99% CPU
    note PID is 8899 and the user is Mike
  2. Analysis - is this a critical production job or a bug causing an infinite loop
    need to act because system is unstable
  3. The Polite Approach - try to pause the process to see if system recovers
    kill -STOP 8899
    system speeds up confirming the process is at fault
  4. The Decision> - confirm with Mike the script id broken
    try the polite kill sending SIGTERM
    kill 8899
    run top again
    if process is still running proceed to next step
  5. The Force - send SIGKILL to kernel
    kill -9 8899
    the process terminates immediately and system returns to normal
Summary
covered
  • every process has a unique PID
  • observation - use top andd htop to monitor system
  • signals - kill doesn't end processes
    send messages link SIGTERM and SIGKILL
  • backgrounding - &, Ctrl-Z, bg and fg allow within a single terminal window
  • priority - niceness

key points
  • PID - a process' unique ID
  • top / htop - essential monitoring tools
  • ps aux - list every running process on the system
  • SIGTERM - polite way to ask a process to stop
  • SIGKILL - forceful was to stop a process
  • background (&) - run a command without blocking a terminal
  • jobs / fg - manage background tasks
  • niceness - high priority (-20) to low priority (+19)
index