student@ubuntu:~$
c-foundations Lesson 2 9 min read

Processes

Programs vs processes, PIDs, job control, signals, and exit status

Reading: Shotts, The Linux Command Line: pp. 131–144 (processes & job control)

Quick check before you start: Do you know the difference between a program and a process? If not, read on. If you can explain what a PID is, skip to Job Control.

Practice this topic: Processes skill drill

After this lesson, you will be able to:

  • Explain the difference between a program (file on disk) and a process (running instance)
  • Use ps and ps aux to view running processes
  • Manage background jobs with &, fg, bg, and jobs
  • Send signals with Ctrl+C, Ctrl+Z, and kill
  • Check exit status with echo $?

Programs vs Processes

A program is a file on disk — a recipe sitting in a cookbook. A process is that recipe being executed — ingredients measured, oven on, timer running.

which ls
# /usr/bin/ls — this is the program (a file)

ls
# running ls creates a process — it gets a PID, uses memory, runs, and exits

Every process has:

Attribute Meaning
PID Process ID — unique number assigned by the kernel
PPID Parent PID — the process that started this one
UID User ID — who owns this process
State Running, sleeping, stopped, or zombie

You can run the same program multiple times simultaneously. Each run creates a separate process with its own PID.


Viewing Processes

ps — Process Snapshot

ps shows your processes:

ps
#   PID TTY          TIME CMD
#  1234 pts/0    00:00:00 bash
#  1267 pts/0    00:00:00 ps

ps aux shows every process on the system:

ps aux | head -5
# USER       PID %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMAND
# root         1  0.0  0.1 169436 11568 ?    Ss   09:00   0:01 /sbin/init
# root         2  0.0  0.0      0     0 ?    S    09:00   0:00 [kthreadd]
# student   1234  0.0  0.0  21476  5192 pts/0 Ss  09:05   0:00 -bash

Key columns: PID (process ID), STAT (state), COMMAND (what is running). Pipe through grep to find specific processes:

ps aux | grep firefox

Job Control

Background and Foreground

Append & to run a command in the background:

sleep 60 &
# [1] 5678

The shell prints the job number [1] and PID 5678, then gives you your prompt back.

Command Effect
jobs List background jobs
fg %1 Bring job 1 to foreground
bg %1 Resume stopped job 1 in background

Ctrl+C and Ctrl+Z

These two key combinations are fundamentally different:

Keys Signal Effect
Ctrl+C SIGINT (interrupt) Terminates the process
Ctrl+Z SIGTSTP (stop) Suspends the process — it is still alive but paused

A suspended process sits in memory doing nothing. Resume it with fg (foreground) or bg (background):

sleep 300          # starts in foreground
# (press Ctrl+Z)
# [1]+  Stopped     sleep 300
fg                 # bring it back to foreground

Signals and kill

The kill command sends a signal to a process by PID:

kill 5678          # sends SIGTERM (polite request to exit)
kill -9 5678       # sends SIGKILL (forced termination, no cleanup)

Common signals:

Signal Number Meaning
SIGTERM 15 Terminate gracefully (default)
SIGKILL 9 Force kill — cannot be caught or ignored
SIGINT 2 Interrupt (same as Ctrl+C)
SIGTSTP 20 Stop (same as Ctrl+Z)

Always try kill PID first. Use kill -9 only when a process refuses to terminate.


Exit Status

Every process returns an exit status when it finishes — an integer between 0 and 255:

ls /etc/passwd
echo $?
# 0 — success

ls /nonexistent
echo $?
# 2 — failure (file not found)

The convention: 0 means success, anything else means failure. The specific non-zero value sometimes indicates what went wrong. You will use exit status extensively when writing shell scripts and C programs — return 0; in main() follows this same convention.


Check Your Understanding
You are running a long compile and press Ctrl+Z. What happens?
AThe process is terminated and removed from memory
BThe process is sent to background and continues running
CThe process is suspended (paused) but remains in memory
DThe terminal closes
Answer: C. Ctrl+Z sends SIGTSTP, which suspends the process. It stays in memory but stops executing. You can resume it with fg (foreground) or bg (background). This is different from Ctrl+C, which sends SIGINT and terminates the process.

What Comes Next

You now understand how Unix runs and manages programs. Next, you will write your first C program and learn how gcc compiles source code into an executable.