Quiz 2

Process Management — Running and Controlling Programs

1098 words
5 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Process Management — Running and Controlling Programs ## 🎯 Learning Objectives - View processes with ps, top, htop - Control processes with kill, pkill - Run jobs in foreground/background - Manage process priority with nice/renice - Keep processes running after logout with nohup ## 1. What is a Process?

Process Management — Running and Controlling Programs

🎯 Learning Objectives

  • View processes with ps, top, htop
  • Control processes with kill, pkill
  • Run jobs in foreground/background
  • Manage process priority with nice/renice
  • Keep processes running after logout with nohup

1. What is a Process?

A process is a running instance of a program. Each process has:
  • PID (Process ID) — unique numeric identifier
  • PPID (Parent PID) — who started it
  • State: running, sleeping, stopped, zombie
  • Owner: which user started it
  • Priority: how much CPU time it gets

2. Viewing Processes

bash
# ps — snapshot
ps                           # Current shell's processes
ps aux                       # All processes (detailed)
ps -ef                       # All processes (Unix format)
ps aux --sort=-%mem          # Sort by memory usage
ps aux --sort=-%cpu          # Sort by CPU usage
ps -u username               # Processes for a user
ps -p 1234                   # Specific PID
# top — real-time view
top                          # Interactive process viewer
# In top:
#   q = quit, k = kill process, r = renice
#   M = sort by memory, P = sort by CPU
#   u = filter by user, 1 = show CPU cores
# htop — improved top (may need install)
htop                         # Colorful, scrollable, mouse-friendly

3. Background and Foreground Jobs

bash
# Run command in background (append &)
sleep 30 &                   # Runs sleep in background
./long_script.sh &           # Run script in background
# Manage jobs
jobs                         # List background jobs
jobs -l                      # Show PIDs
# Bring to foreground
fg %1                        # Bring job 1 to foreground
fg %2                        # Bring job 2 to foreground
# Send to background
bg %1                        # Resume job 1 in background
# Suspend current foreground job
Ctrl+Z                       # Suspends current job (stops it)
# Then: bg to resume in background
# Or: fg to resume in foreground
# Example workflow
$ sleep 100
^Z                           # Suspend
[1]+  Stopped    sleep 100
$ bg %1                      # Resume in background
$ jobs                       # Check
[1]+  Running   sleep 100 &
$ fg %1                      # Bring back to foreground

4. Killing Processes

bash
# Basic kill (SIGTERM — request termination)
kill 1234                    # Gracefully kill process 1234
kill -15 1234                # SIGTERM (same as above)
# Force kill (SIGKILL — cannot be caught/ignored)
kill -9 1234                 # Force kill
# Kill by name
killall firefox              # Kill all firefox processes
pkill node                   # Kill all node processes
pkill -u username            # Kill all processes for user
# Send other signals
kill -HUP 1234               # SIGHUP — reload configuration
kill -STOP 1234              # SIGSTOP — pause process
kill -CONT 1234              # SIGCONT — resume paused process

5. Process Priority — nice and renice

Linux uses niceness (-20 to 19). Higher = lower priority (nicer to other processes). Default = 0.
bash
# Start with specific priority
nice -n 19 ./slow_script.sh     # Very low priority (nicest)
nice -n -10 ./urgent_task.sh    # Higher priority (needs root)
# Change priority of running process
renice +5 -p 1234                # Lower priority of PID 1234
renice -5 -u alice               # Higher priority for all alice's processes
# View priority
ps -l -p 1234                    # Shows NI (nice) column
top                              # Shows NI column

6. Keeping Processes Running — nohup and disown

bash
# nohup — ignore HUP signal (process survives logout)
nohup ./long_script.sh &
nohup ./backup.sh > backup.log 2>&1 &
# disown — remove job from shell's job table
./script.sh &
disown                         # Shell won't send SIGHUP on exit
# screen/tmux — terminal multiplexers (most robust)
screen -S mysession            # Create session
# Run your command, then Ctrl+A, D to detach
screen -r mysession            # Re-attach
tmux new -s mysession          # Create tmux session
# Ctrl+B, D to detach
tmux attach -t mysession       # Re-attach

7. Process States

bash
# From ps output (STAT column):
# R — Running or runnable
# S — Sleeping (interruptible wait)
# D — Uninterruptible sleep (I/O wait)
# T — Stopped (by signal or job control)
# Z — Zombie (finished, waiting for parent to reap)
# + — In foreground process group
# < — High priority
# N — Low priority
# l — Multi-threaded

8. Practice Questions

Q1: How do you run a command in the background?
Answer: Append & to the command: sleep 30 &. Or run the command, press Ctrl+Z to suspend, then bg to resume in background. jobs lists all background jobs. Q2: What's the difference between SIGTERM (kill) and SIGKILL (kill -9)?
Answer: SIGTERM (15) requests graceful termination — the process can catch it, clean up resources, save state. SIGKILL (9) force kills immediately — the process cannot catch or ignore it. Always try kill PID first. Q3: How do you view all processes running on the system?
Answer: ps aux (all users, detailed) or ps -ef (all processes, Unix format). top or htop for real-time interactive view. Q4: What does nice do?
Answer: Sets process priority. Range: -20 (highest priority) to 19 (lowest, "nicest"). Default is 0. Higher nice value = lower CPU priority (more "nice" to other processes). sudo nice -n -10 command for higher priority. Q5: How do you keep a process running after you log out?
Answer: nohup command & (ignores SIGHUP), disown (removes from shell job control), or better: screen or tmux (terminal multiplexers that survive disconnection). Q6: What does Ctrl+Z do?
Answer: Suspends (pauses) the current foreground process. The process is stopped (SIGSTOP) and can be resumed with fg (foreground) or bg (background). Not to be confused with Ctrl+C (terminates the process). Q7: What is a zombie process?
Answer: A process that has completed execution but still has an entry in the process table because its parent hasn't read its exit status (via wait()). Zombies consume no resources except a PID. They're cleaned up when the parent calls wait() or exits. Q8: How do you find and kill all processes owned by a user?
Answer: pkill -u username kills all processes for that user. Or: ps -u username | awk '{print $1}' | xargs kill. As root, you can kill any user's processes.

📐 Key Concepts

CommandPurposeExample
psList processesps aux
topReal-time monitortop
killSend signalkill PID, kill -9 PID
pkillKill by namepkill firefox
&Backgroundcommand &
fg/bgForeground/backgroundfg %1
niceSet prioritynice -n 19 command
nohupSurvive logoutnohup command &

🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.