Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 95 ยท Processes and the System

Processes: ps, top and /proc

Understand Linux processes: PIDs and parents, ps options and custom columns, process states (S, R, T, Z), the /proc filesystem, threads, and top and htop.

Whenever you run a program, Linux creates a process: a running instance of that program with its own memory, its own open files, its own working directory and an identity number. Your shell is a process. Each command it starts is a new process. A busy server runs thousands of them. Knowing how to list them, read their state and see how they relate to each other is the foundation for everything in this part of the course: background jobs, signals, services, performance and security.

In this lesson you will learn what a process is made of, how PID and PPID build a family tree, how to list and format processes with ps, what the one-letter states mean, how to look inside a process through the /proc filesystem, what threads are, and how top and htop fit in. The examples run in a real Linux sandbox, but it is a tiny one: you will only see your own script and its children, which is perfect for learning.

Process IDs are not stable. A PID changes on every run, so our examples print relationships ("the parent of A is B") and not raw numbers. In a real terminal your numbers will be different anyway. Never hard-code a PID in a script; ask for it with $$, $! or pgrep.

Program versus process

A program is a file on disk (/bin/ls). A process is that program loaded into memory and running. You can run the same program many times at once: ten processes, one file. Each process has:

  • a PID (process ID), a number unique at that moment;
  • a PPID, the PID of the parent that started it;
  • an owner (user and group), which decides what it may access;
  • a working directory, environment variables and a table of open files (file descriptors 0, 1, 2 are stdin, stdout and stderr);
  • a state, such as running or sleeping.

The special shell variables $$ (the PID of the current shell) and $PPID (its parent) let a script ask about itself.

Bash Who am I, and who is my parent?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

(ps -o ppid= -p $$ selects process $$ and prints only the ppid column; the trailing = removes the header.)

Parents and children: fork and exec

New processes are born in two steps. The parent forks: the kernel makes a nearly identical copy of it. Then the copy execs: it replaces itself with a different program. When you type ls, your shell forks a child and the child execs /bin/ls; the shell waits until the child exits. Every process except the very first (PID 1, the init process) has a parent, so processes form a tree.

TEXT
 PID 1  init / systemd
   |
   +-- sshd (login session)
   |      |
   |      +-- bash   <- your shell
   |            |
   |            +-- sleep 60   (a command you started)
   |            +-- ls | grep  (a pipeline: one process per stage)
   |
   +-- cron, nginx, ... (services)

When a process ends it returns an exit status to its parent (Exit Codes). If a parent dies first, its children are adopted by PID 1 (or another "subreaper"), so they are never left without a parent.

Bash A subshell is a new process

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In a subshell $$ still holds the PID of the original shell, while $BASHPID is the real PID of the current process. That difference is a classic trap, explained further in Subshells, Groups and Environment.

Listing processes with ps

ps takes a snapshot of the process table. Used without options it shows only the processes of your current terminal session. The options that matter:

Command Shows
ps Processes of the current terminal
ps -e or ps -A Every process
ps -f Full format (adds user, PPID, start time, full command)
ps aux BSD style: all users, with CPU and memory columns
ps -p PID,PID Only the given PIDs
ps -u user Processes of one user
ps --forest ASCII tree of parents and children
ps -o col1,col2 Choose your own columns

The mixing of dashed (UNIX) options and undashed (BSD) ones in ps is a historical accident: ps -aux and ps aux are different commands. Stick to one style within a command.

Bash ps --forest shows the process tree

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The tree shows bash at the top, with the sleep commands, the ps command itself and the sed it is piped into as its children (indented with \_). The subshell appears as another bash with its own sleep below it. Whatever you pipe ps into becomes a process too, so ps | grep x lists grep itself; the classic trick grep '[x]' avoids that, and pgrep (kill, Signals and Process Control) does it correctly.

Choosing columns with -o

The format option -o gives precise control and is the right way to use ps in scripts, because the default output changes between systems. Frequently useful columns:

Column Meaning
pid, ppid Process and parent IDs
user, uid Owner
stat State code (next section)
comm Command name only
args (or cmd) Full command line with arguments
etimes Seconds since the process started
rss Resident memory in kilobytes
pcpu, pmem CPU and memory percentage
nlwp Number of threads (light-weight processes)
ni, pri Nice value and priority

Adding = to a column (-o pid=) removes its header. Sorting uses --sort=-rss (descending memory) or --sort=etimes.

Bash Custom columns and sorting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Process states

The STAT column (or state in /proc) is a one-letter code, sometimes followed by extra characters:

Code Name Meaning
R Running On a CPU or waiting for one
S Sleeping Waiting for an event (input, a timer, a child): most processes are here
D Uninterruptible sleep Waiting on I/O (disk, network file system); cannot be killed until it returns
T Stopped Paused by a signal (Ctrl-Z, SIGSTOP)
Z Zombie Finished, but the parent has not yet read its exit status
I Idle An idle kernel thread

Extra flags after the letter: s session leader, + in the foreground job, < high priority, N low priority, l multi-threaded.

Two of these states are worth seeing in action. A stopped process is frozen but alive until you send SIGCONT. A zombie is a process that has already ended: its memory is freed, but the kernel keeps a small record of its exit status until the parent calls wait. A zombie cannot be killed (it is already dead); you remove it by making the parent collect it, or by ending the parent so that PID 1 adopts and reaps it.

Bash Stopped and zombie processes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The short sleep finished after 0.1 s, but its parent (now running a different program that does not know about it) never collected its status, so ps shows it as Z with the name sleep <defunct>. A few zombies are normal and disappear quickly. Thousands of them mean a buggy parent program.

Looking inside: the /proc filesystem

Linux exposes process (and kernel) information as files under /proc. It is a virtual filesystem: nothing is stored on disk, and reading a file asks the kernel for the answer right now. This is where ps and top get their data. Each process has a directory /proc/PID/, and /proc/self is a shortcut for "the process that is looking" (not your shell: the program doing the reading, like cat).

File Contents
/proc/PID/status Human-readable summary: name, state, PPid, memory, threads
/proc/PID/cmdline Command line, arguments separated by NUL bytes
/proc/PID/environ Environment (NUL-separated; readable only by the owner)
/proc/PID/cwd Symlink to the working directory
/proc/PID/exe Symlink to the executable file
/proc/PID/fd/ One symlink per open file descriptor
/proc/PID/limits Resource limits
/proc/PID/stat, /proc/PID/statm Machine-readable numbers used by ps
Bash Reading a process through /proc

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The file descriptors are the same 0, 1, 2 you met in Standard Streams and Redirection. (Here 3 is the directory handle that ls itself holds while listing, and 255 is a descriptor Bash keeps for reading the script.) On a server, ls -l /proc/PID/fd is the quickest way to see which files and sockets a process has open.

Kernel-wide information in /proc

Files directly under /proc describe the whole system. Reading them is how uptime, free and top work. Their numbers differ on every machine and every second, so here is only the structure:

CONSOLE
$ head -3 /proc/loadavg
0.13 0.16 0.24 2/338 16
$ grep -E '^(MemTotal|MemAvailable)' /proc/meminfo
MemTotal:       12192000 kB
MemAvailable:   11360000 kB
$ cat /proc/cpuinfo | grep -c ^processor
8

(These are read-only example values, not output of our editor.) The three load averages are the number of runnable processes averaged over 1, 5 and 15 minutes; on a 4-core machine a load of 4.0 means all cores are fully busy. The 2/338 is "running now / total", and the last number is the most recent PID. You will parse /proc/meminfo in Disks, Mounts, Logs and Monitoring.

Threads

A process may contain several threads: parallel paths of execution that share the same memory and open files. A web server might run one process with 50 threads. The kernel schedules threads individually; ps shows the thread count with nlwp, ps -L lists the threads one per line, and /proc/PID/task/ has a directory for each. Our shell is single-threaded, which /proc confirms:

Bash How many threads?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

All three numbers say 1. Multi-threaded programs would show larger values. Processes are isolated from each other (a crash in one does not damage another), whereas threads share memory (cheaper to create, but a bug in one thread can corrupt the others).

top and htop (read-only)

top is a live, refreshing view of the process table with CPU and memory totals at the top, sorted by CPU use. It is interactive, so it cannot run in our editor, but its batch mode prints one frame and exits, which is fine in scripts. The first lines of a frame look like this (a read-only example, your numbers will differ):

CONSOLE
$ top -b -n 1 | head -5
top - 09:27:25 up  3:17,  1 user,  load average: 0.08, 0.14, 0.24
Tasks: 214 total,   1 running, 212 sleeping,   0 stopped,   1 zombie
%Cpu(s):  1.2 us,  0.4 sy,  0.0 ni, 98.3 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  11906.1 total,  10716.6 free,    602.1 used,    587.4 buff/cache
MiB Swap:   3072.0 total,   3071.9 free,      0.1 used.  11098.7 avail Mem

The Tasks line counts processes by state, exactly the letters from the table above. In interactive top:

  • M sorts by memory, P by CPU, q quits, k kills a process, 1 shows each CPU core, h shows help.
  • High wa (I/O wait) means the machine waits for disks, high us or sy means the CPU is busy, and a high load with a low CPU use points at processes stuck in state D.

htop is a friendlier alternative (colours, mouse, tree view with F5, search with F3), but it must be installed separately. For one-off questions in scripts ps -e -o pid,pcpu,pmem,args --sort=-pcpu | head gives you the same top list without any interactivity.

Process priority: nice, renice and ionice

When several processes want the CPU at once, the scheduler has to choose. Every process carries a niceness value from -20 to 19. The name is meant literally: a nice process lets others go first. The default is 0; a higher number means a lower priority, and a lower (negative) number means a higher one. Niceness is a hint about share, not a guarantee: on an idle machine even a niceness-19 job gets all the CPU it wants.

The rules are simple:

  • Anyone may raise the niceness of their own processes (be nicer).
  • Only root may lower it (be less nice), including starting a job at a negative value, and an ordinary user cannot undo their own increase.
  • A child inherits the niceness of its parent, so nice -n 10 ./build.sh also slows every compiler that script starts.
Command What it does
nice -n 10 command Start command with niceness 10 (plain nice command adds 10)
nice With no command, print the current niceness
renice -n 5 -p PID Change a running process to niceness 5 (-n is the new value; older systems also accept the bare number)
renice -n 5 -u alice Change all processes of user alice (-g for a group)
ps -o pid,ni,pri,comm Show niceness (ni) and the kernel's priority number (pri)
ionice -c 3 command Run with the idle I/O class: it touches the disk only when nobody else does
ionice -c 2 -n 7 command Best-effort class, lowest level (0 is highest, 7 lowest)

ionice works the same way for the disk: class 1 is real-time (root only), class 2 best-effort, class 3 idle. CPU niceness does not limit disk use, so a backup should usually run as nice -n 19 ionice -c 3 backup.sh. Inside /proc/PID/stat the kernel shows the number as priority = 20 + nice for normal processes, which is why a niceness of 0 appears as 20 in some tools.

To find the PID you want to change, ask by name instead of scanning ps by eye: pgrep -x sleep lists the PIDs of processes named exactly sleep, pgrep -u alice -l python adds the names, pidof sshd does a similar job, and pkill sends a signal by name. They print only PIDs, which makes them ideal arguments for renice -p $(pgrep ...).

Bash nice, renice, pgrep and ionice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The permission failure is the safeguard that stops a user from reclaiming CPU time an administrator took away. If a runaway job is starving a server, renice -n 19 -p PID is a gentler first step than kill (see Kill and Signals), and for hard limits (CPU share, memory) you use cgroups, as in Containers from Scratch.

Finding and stopping processes by name

Looking up a PID by hand and then calling kill is slow and risky. Four tools do both steps in one command. They differ in how they match:

Command What it does Match rule
pgrep name Prints matching PIDs The process name (comm); -f matches the full command line, -x demands an exact name, -u user restricts to a user
pidof name Prints the PIDs of a program Exact program name; handy in old scripts (kill $(pidof nginx))
pkill name Sends a signal (default SIGTERM) The same rules as pgrep
killall name Sends a signal to every process with that exact name Exact name; -s SIG chooses the signal, -i asks first, -w waits for them to die, -q stays quiet when nothing matched
Terminal
$ pgrep -u alice -l python       # PIDs and names of alice's python processes
$ pgrep -f "manage.py runserver" # match anywhere in the command line
$ pkill -f "manage.py runserver" # ...and stop it
$ pkill -u alice                 # everything alice runs (careful!)
$ killall -s HUP nginx           # ask all nginx processes to reload
$ killall -w -s TERM worker      # signal and wait until they are gone
$ pidof sshd                     # the PIDs of the sshd daemon(s)

Two warnings. On Solaris and some older Unix systems killall kills everything the user owns: the name is the same, the meaning is not, so never type it on an unfamiliar machine. And pkill without -x matches substrings: pkill ssh also hits sshd and ssh-agent. Use pgrep first to see what would match, then switch to pkill.

The exit code tells scripts what happened: pgrep, pkill and killall exit 0 when at least one process matched and 1 when none did. The lab uses a copy of sleep under another name so that the match is unique.

Bash pgrep, pkill and killall on a copy of sleep

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

More ways to inspect and tune a process

A short tour of tools that answer specific questions about a running process:

  • pmap PID prints the memory map: every region the process has mapped (program code, heap, stack, shared libraries) with address, size and permissions. pmap -x PID adds resident size (RSS) and dirty pages per region, and its last line is the total. A growing [heap] or [anon] total over time points at a leak. The same data is in /proc/PID/maps and /proc/PID/smaps.
  • taskset -p PID shows the CPU affinity: the set of CPUs the process may run on. taskset -c 0,1 command starts a command on CPUs 0 and 1 only; taskset -cp 2 PID moves a running process to CPU 2.
  • chrt reads and sets the scheduling policy. Normal programs run under SCHED_OTHER (the fair scheduler, steered by nice). Real-time programs use SCHED_FIFO or SCHED_RR with a priority of 1 to 99, which beats every normal process; a runaway real-time loop can freeze the machine, so this needs root and care. SCHED_BATCH and SCHED_IDLE are for work that should only use leftover CPU. chrt -m lists the policies and their priority ranges; chrt -p PID shows one process; sudo chrt -f -p 50 PID makes it FIFO at priority 50.
  • prlimit --pid PID shows or changes the resource limits (open files, memory, CPU time) of a running process; ulimit does the same for the current shell.
  • accton and lastcomm belong to process accounting: after sudo accton /var/log/account/pacct, the kernel appends a record for every process that ends (name, user, CPU time, exit status), and lastcomm or sa reads them. It is a cheap audit trail on shared machines, and accton with no argument turns it off.
Bash Scheduling policy and CPU affinity of the shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Process or program? Common questions

  • Why do I see bash main.sh and not just bash? args shows the full command line; comm shows only the name of the executable.
  • How do I find a process by name? pgrep -a name (see kill, Signals and Process Control); avoid ps | grep.
  • Where do processes come from in a pipeline? Each stage of a | b | c is its own process, all started at about the same time (Pipes and Pipelines).
  • What about background jobs? They are ordinary processes started with &; see Jobs and Background Processes.
  • Does ps show all users? Yes with -e, but you can only read details like /proc/PID/environ for your own processes unless you are root.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the special variable $$ contain?

    $$ is the PID of the shell. $PPID is its parent, $? is the last exit status and $! is the PID of the most recent background job.
  2. 2. Which ps option prints a tree of parent and child processes?

    --forest draws the process hierarchy with ASCII lines. ps -f is the full format, not a tree.
  3. 3. A process shows Z in the STAT column. What does that mean?

    A zombie has already ended and holds no memory, only an entry in the process table until the parent calls wait. You cannot kill it; the parent has to reap it.
  4. 4. Where does Linux expose per-process information such as the command line and open file descriptors?

    /proc is a virtual filesystem generated by the kernel. Each process has a directory /proc/PID with files such as cmdline, status, cwd and fd/.
  5. 5. Why is ps -o pid=,args= better in scripts than parsing the plain output of ps?

    Default ps output differs between systems and options. With -o you choose the columns, and a trailing = removes the header, so the output is easy to parse.

Summary

  • A process is a running program with a PID, a parent (PPID), an owner, a working directory, an environment and open files; they form a tree rooted at PID 1.
  • New processes are made by fork (copy) and exec (replace with another program); a child returns an exit status to its parent, which collects it with wait.
  • $$ is the shell's PID, $PPID its parent, $BASHPID the real PID in a subshell.
  • ps -e, ps aux, ps --forest and especially ps -o pid,ppid,stat,args list processes; drop the header with col=.
  • States: R running, S sleeping, D uninterruptible I/O wait, T stopped, Z zombie.
  • /proc/PID/ exposes status, cmdline, cwd, exe and fd; /proc/loadavg and /proc/meminfo describe the system. Threads share a process's memory (nlwp).
  • top and htop are interactive monitors (read-only here); top -b -n 1 and ps --sort work in scripts.
  • nice -n N cmd starts a job with niceness N (-20 to 19, higher is nicer); renice -n N -p PID changes a running one; only root can lower it; ionice -c 3 does the same for disk access.

Next: Jobs and Background Processes