Process anatomy & process states
Describe what a process owns and how it moves between scheduler states.
By Invicly Tech, , in Linux System Programming. About 12 minutes to read.
In short
A process is a running program plus everything the kernel tracks for it: an address space, open descriptors, credentials, signal state and one or more threads. The scheduler moves each task between states such as running, sleeping and stopped. ps and /proc show these states as letters like R, S, D, T and Z.
Explained simply
A program is a file on disk, like a recipe in a book. A process is that program while it runs: the recipe being cooked right now, with its own pots, ingredients and timer. You can run the same program ten times and get ten separate processes, each with its own memory and its own number.
That number is the PID (process ID). Every process also remembers its parent, the process that started it, as the PPID. When you type ls in a terminal, the shell is the parent and ls is the child. Follow the PPIDs upwards and you always end at PID 1, which is usually systemd.
The kernel keeps a record for each process: which memory it uses, which files it has open, which user it runs as, and what it is doing right now. Most of the time a process is not running at all. It is sleeping, waiting for a key press, a network packet or a disk read.
You can read this record yourself. Every process has a folder in /proc named after its PID, full of small text files. Tools like ps and top just read those files and print them nicely, so learning /proc means you understand what every monitoring tool shows you.
Press Play on a diagram to walk through it one step at a time.
- 1Runnable (R)
Ready and waiting for a CPU.
- 2Running (R)
Executing until it blocks or its time slice ends.
- 3Sleeping (S or D)
Waiting for I/O, a lock or a timer.
- 4Woken
The event arrives and the task is runnable again.
- Back to step 1
- Virtual memory and heap
- File descriptor table
- Signal handlers
- PID (TGID) and credentials
- Current directory
- Thread ID (TID)
- CPU registers and stack
- Signal mask
- errno value
- Scheduling state (R, S, D)
- 1fork
Kernel picks a free PID for the child.
- 2Runs
Moves between R, S and D many times.
- 3Exits
Memory is freed, state becomes Z.
- 4Parent waits
Exit status collected, entry removed.
- 5PID is free
Can be reused after the counter wraps.
In depth
What a process actually owns
Inside the kernel, each task is described by a large structure (task_struct). A process holds pointers to several resources: an address space (its virtual memory), a table of open file descriptors, a current and root directory, credentials, signal handlers and a signal mask, resource limits (ulimit), a cgroup and a set of namespaces.
Threads of the same process share most of these: the memory, the descriptor table, the signal handlers and the credentials. Each thread keeps its own registers, stack, signal mask and thread ID. This is why one thread can close a file that another thread is reading, and why a crash in one thread kills the whole process.
- ls -l /proc/PID/fd shows the descriptor table
- cat /proc/PID/limits shows its resource limits
- ls -l /proc/PID/cwd and /proc/PID/exe show its directory and binary
- ls -l /proc/PID/ns shows its namespaces
- Memory
- Code, data, heap
- Stacks and mmap regions
- Files
- Descriptor table
- Current and root directory
- umask
- Identity
- UIDs and GIDs
- Supplementary groups
- Capabilities
- Signals
- Handlers per signal
- Pending signals
- Limits and grouping
- Resource limits
- cgroup
- Namespaces
- Threads
- Registers and stack each
- Own signal mask each
Credentials: real, effective and saved IDs
A process has more than one user ID. The real UID says who started it. The effective UID is what the kernel checks for permissions. The saved set-user-ID remembers an earlier effective UID so a program can drop privileges and pick them up again later. Groups work the same way, plus a list of supplementary groups.
Normally all three UIDs are equal. They differ when you run a setuid program such as passwd: the real UID stays yours, but the effective UID becomes 0 (root), so it may write the password file. In /proc/PID/status the Uid line shows four numbers: real, effective, saved and the file-system UID. On modern Linux, root's power is also split into capabilities, such as CAP_NET_BIND_SERVICE for binding ports below 1024.
- getuid() returns the real UID, geteuid() the effective one
- Permission checks on files use the effective (file-system) UID
- grep -E '^(Uid|Gid|Groups|Cap)' /proc/self/status
All the state letters, and the load average
The explainer covers R, S, D, T and Z. You will also meet t (stopped by a debugger or strace), I (an idle kernel thread, shown since Linux 4.14) and, very rarely, X (dead, being removed). ps adds extra letters after the state: s for a session leader, + for the foreground process group, l for multithreaded, and < or N for high or low priority.
One Linux detail surprises people: the load average counts tasks in state R and in state D. A server with idle CPUs can still show a load of 30 if many tasks are stuck in D waiting for a slow disk or a dead NFS server. So a high load does not always mean the CPU is busy. Check top: if the CPU idle percentage is high and the wa (I/O wait) value is high, look for D tasks.
- ps -eo pid,stat,wchan:32,comm shows what each task waits on
- Ss+ means sleeping, session leader, foreground group
- cat /proc/loadavg shows 1, 5 and 15 minute averages
How ps and top get their data
ps does not have special access to the kernel. It opens /proc, lists the numeric folders, and reads files such as /proc/PID/stat, status and cmdline. The status file is the friendliest: Name, State, Tgid, Pid, PPid, Uid, Gid, Threads, VmRSS (memory in RAM), and the hex masks SigBlk, SigIgn and SigCgt for blocked, ignored and caught signals.
A few files hold binary-ish data: cmdline and environ separate their strings with NUL bytes, so use tr '\0' ' ' to read them. The counters voluntary_ctxt_switches and nonvoluntary_ctxt_switches tell you whether a process mostly waits (voluntary) or keeps being pushed off the CPU (nonvoluntary). Reading another user's files may need root, because some entries are protected.
- 1ps, top, htop
Format and sort the numbers.
- 2procps library
Opens and parses the text files.
- 3/proc/PID/*
Files generated on every read.
- 4procfs code
Kernel code that fills each file.
- 5task_struct
The real per-task data in kernel memory.
PIDs, thread IDs and PID reuse
Inside the kernel every thread has its own ID. What user space calls the PID is really the thread group ID (TGID), the ID of the first thread. getpid() returns the TGID and gettid() returns the thread's own ID; for a single-threaded program they are equal. You can see the threads in /proc/PID/task/.
PIDs are recycled. The kernel hands out numbers up to kernel.pid_max and then wraps around. The kernel default is 32768, but systemd raises it to 4194304 on 64-bit systems. Because of reuse, a script that saves a PID and later runs kill on it can hit a different process. Modern code uses a pidfd (pidfd_open, Linux 5.3), a descriptor that always refers to one exact process. Containers add PID namespaces, so the same process can be PID 1 inside the container and PID 48211 on the host.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1List what a process owns~45m
- 2Read PIDs, PPIDs and credentials~45m
- 3Trace running, sleeping and stopped states~45m
- 4Inspect processes via /proc/PID/status~45m
Key points
- Each process has a PID, a parent PID, user and group IDs and a working directory.
- R is running or runnable, S is interruptible sleep, D is uninterruptible sleep, usually disk or NFS I/O.
- T means stopped by a signal such as SIGSTOP or Ctrl-Z.
- Z is a zombie: finished, but the parent has not yet collected its exit status.
Key terms
Tap a card to flip it. 0/5 known
Examples
ps -eo pid,ppid,stat,comm | head
grep -E "^(Name|State|Pid|PPid|Uid)" /proc/self/status
sleep 100 &
ps -o pid,stat,comm -p $!
kill -STOP $!; ps -o stat= -p $!
kill -CONT $!Watch a process move from S to T and back.
Why is the load 25 when the CPU is idle?
A monitoring alert says load average 25 on a 4-core server, but top shows 90% idle. Step 1: remember that Linux counts D-state tasks in the load. Step 2: list tasks in D and see what kernel function they wait in (wchan). Step 3: many processes waiting in an NFS or block-layer function point to storage, not CPU. Step 4: check the mount or disk they use, for example with df on the NFS mount, which itself may hang if the server is down.
cat /proc/loadavg
top -bn1 | head -5 # look at id (idle) and wa (I/O wait)
ps -eo pid,stat,wchan:32,comm | awk '$2 ~ /^D/'
sudo cat /proc/<PID>/stack # kernel stack of one stuck task (root only)
mount -t nfs,nfs4 # which network mounts existA program that inspects itself
This small C program prints its own IDs with the getter functions, then reads two lines from /proc/self/status. /proc/self always points to the process that opens it. Compile with gcc -Wall ids.c -o ids and run it normally, then with sudo, and compare the UID lines. The State line says R because the process is running while it reads the file.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(void) {
printf("pid=%d ppid=%d uid=%d euid=%d\n",
(int)getpid(), (int)getppid(), (int)getuid(), (int)geteuid());
FILE *f = fopen("/proc/self/status", "r");
if (!f) { perror("fopen"); return 1; }
char line[256];
while (fgets(line, sizeof line, f))
if (!strncmp(line, "State:", 6) || !strncmp(line, "Threads:", 8)) fputs(line, stdout);
fclose(f);
return 0;
}Quick reference
- ps -eo pid,ppid,user,stat,comm --forest shows the process tree with states
- pstree -p shows parents and children with PIDs
- /proc/PID/status is the readable summary; /proc/PID/stat is the compact one
- tr '\0' ' ' < /proc/PID/cmdline prints the full command line
- Load average counts R and D tasks, so slow I/O can raise it
- getpid() returns the thread group ID; gettid() returns the thread ID
- ls /proc/PID/task lists every thread of a process
- kernel.pid_max sets the highest PID before numbers wrap around
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Start sleep 300 in the background and find its PID, PPID, state and UID using only files under /proc.
- Exercise 2medium
Write a one-line command that prints, for every process, its PID, thread count and command name, sorted by thread count with the largest last.
- Exercise 3hard
Write a C program that starts 3 threads, then lists /proc/self/task and prints each thread ID together with its State line.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between a program and a process?
- A program is a file on disk. A process is one running instance of it, with its own memory, open files and PID; one program can run as many processes at once.
- Why can't I kill a process in state D?
- A task in uninterruptible sleep only handles signals when its kernel wait finishes, so even SIGKILL waits. Fix the I/O it is waiting for, such as an unreachable NFS server.
- Is /proc stored on disk?
- No. /proc is a virtual file system; the kernel builds each file's content at the moment you read it.
- What is PID 1?
- The first user-space process the kernel starts, usually systemd. It adopts orphaned processes and is never allowed to exit while the system runs.