Skip to content
systemcoding.dev
Systems Programming

What Is a Process? PIDs, Address Spaces and the Process Table

A process is a running program plus the kernel's record of it: a PID, a private address space and a table entry. See each part in C on Linux.

8 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, Linux and Rust logos.

A program is a file on disk. A process is what the kernel creates when it runs that file, and the difference matters the first time two copies of the same program behave differently because they hold different data. On Linux 6.12 a process is made of three things you can inspect from C: a process ID, an address space that belongs to it alone, and an entry in the kernel's process table. The rest of this post looks at each one, and the complete guide to Unix processes shows how processes are created and ended.

The examples compile with GCC 14 on Linux 6.12 with glibc 2.40, and the system calls are described in the Linux man-pages project.

A process is a program plus bookkeeping

Two terminals running sleep 60 give you two processes from one program file. They share the same code on disk, and often the same physical pages in memory, but each has its own state. The kernel keeps that state in a structure per process. You can think of it as a row of a table with columns for identity, memory, open files, signals and scheduling.

The identity columns are the ones you meet first. Every process has a process ID, a parent process ID, a user ID and a group ID. C exposes them through small system calls that cannot fail:

identity.cc
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    printf("pid  = %d\n", (int)getpid());
    printf("ppid = %d\n", (int)getppid());
    printf("uid  = %d\n", (int)getuid());
    printf("gid  = %d\n", (int)getgid());
    return 0;
}

The first two lines change on every run because the kernel hands out the next free number. The user and group IDs are those of whoever ran the program:

Text
pid  = 20514
ppid = 20102
uid  = 1000
gid  = 1000

The parent ID 20102 is the shell that started the program. Every process except the first has a parent, and that chain of parents leads back to process 1, which on a modern Linux system is the init program such as systemd. The command pstree -p draws the whole tree.

PIDs are small numbers the kernel reuses

A process ID is a positive integer. Linux assigns them in increasing order and wraps around when it reaches the limit in /proc/sys/kernel/pid_max. The default on a 64-bit system is 4194304 since Linux 4.x kernels raised it, though it can be lower on small machines. After the wrap, numbers freed by ended processes are handed out again.

That reuse has one consequence worth remembering early. A PID names a process only while that process exists. If you store a PID and later call kill(pid, SIGTERM), you may signal a different program that inherited the number. The kernel does not reuse a PID while the old process is still a zombie, which is one reason parents must collect their children. Newer code avoids the problem altogether with pidfd_open, which returns a descriptor that stays tied to one process.

A few IDs have fixed meanings:

IDMeaning
0Not a process you can see; getppid returns 0 for processes whose parent is outside your PID namespace
1The init process, the ancestor of every other process
-1 as an argumentIn kill and waitpid, "every process the caller may signal" or "any child"
0 as an argumentIn kill, "my own process group"

The last two rows catch beginners. kill(-1, SIGKILL) is not an error; it is a very large hammer.

The address space is private

Each process sees its own range of virtual addresses starting near zero. The address 0x7ffd1000 in one process and the same number in another point at different physical memory, or at none. The hardware's memory management unit translates the numbers using page tables that the kernel keeps per process. That is what makes one process unable to read or overwrite another's variables by accident.

You can see the privacy directly. The next program prints the address of a global variable, and the two processes it creates modify the variable differently:

private_memory.cc
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

static int counter = 10;

int main(void)
{
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return EXIT_FAILURE;
    }

    if (pid == 0) {
        counter += 5;
        printf("child : &counter=%p counter=%d\n", (void *)&counter, counter);
        _exit(0);
    }

    waitpid(pid, NULL, 0);
    printf("parent: &counter=%p counter=%d\n", (void *)&counter, counter);
    return EXIT_SUCCESS;
}

The address is identical in both lines because the child starts as a copy of the parent's layout, yet the values differ:

Text
child : &counter=0x5588c0a4d010 counter=15
parent: &counter=0x5588c0a4d010 counter=10

The exact address differs between runs because Linux randomizes where it loads the program (address space layout randomization). What stays the same is that both processes show the same number with different contents behind it. Same virtual address, different physical page: that is the address space at work.

A typical layout, from low to high addresses, has these regions:

RegionHoldsVisible in
TextThe machine code, read-only/proc/<pid>/maps
Data and BSSGlobal and static variables/proc/<pid>/maps
HeapMemory from malloc, grows upward/proc/<pid>/maps as [heap]
MappingsShared libraries, mmap regions/proc/<pid>/maps
StackLocal variables and return addresses/proc/<pid>/maps as [stack]

You can read your own layout from the program itself:

show_maps.cc
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE *f = fopen("/proc/self/maps", "r");
    if (!f) {
        perror("fopen");
        return EXIT_FAILURE;
    }
    char line[256];
    int shown = 0;
    while (fgets(line, sizeof line, f) && shown < 5) {
        fputs(line, stdout);
        shown++;
    }
    fclose(f);
    return EXIT_SUCCESS;
}
Text
5588c0a49000-5588c0a4a000 r--p 00000000 fd:01 1048601   /home/dev/show_maps
5588c0a4a000-5588c0a4b000 r-xp 00001000 fd:01 1048601   /home/dev/show_maps
5588c0a4b000-5588c0a4c000 r--p 00002000 fd:01 1048601   /home/dev/show_maps
5588c0a4c000-5588c0a4d000 r--p 00002000 fd:01 1048601   /home/dev/show_maps
5588c0a4d000-5588c0a4e000 rw-p 00003000 fd:01 1048601   /home/dev/show_maps

Each line is a start and end address, the permissions (r, w, x, and p for private), the file offset, the device, the inode and the path. The r-xp line is the code. The rw-p line holds writable globals, which includes counter in the previous program. The path, device and addresses will differ on your machine.

The process table holds everything else

The kernel keeps one structure per process, struct task_struct in the Linux source. It is the process table entry, though Linux does not store it as a literal flat table. It holds:

  • the identity: PID, parent, user and group IDs, session and process group;

  • the state: running, sleeping, stopped or zombie;

  • a pointer to the address space description and its page tables;

  • the table of open file descriptors, with the current directory and root;

  • the signal dispositions, the pending signals and the blocked mask;

  • the scheduling data and the accounting of CPU time used;

  • the exit status after the process ends.

Almost all of it is visible through /proc/<pid>/status. The state field is the one that answers "what is this process doing":

Shell
grep -E '^(Name|State|Pid|PPid|Uid|VmSize|VmRSS):' /proc/self/status
Text
Name:	grep
State:	R (running)
Pid:	20631
PPid:	20102
Uid:	1000	1000	1000	1000
VmSize:	    8120 kB
VmRSS:	    1664 kB

VmSize is the whole address space the process has mapped. VmRSS is the part currently resident in physical memory. The two differ because most mapped memory is never touched. Both numbers are a snapshot taken from a short-lived grep, so yours will differ.

The states you will meet:

LetterStateMeaning
RRunningOn a CPU or waiting for one
SSleepingWaiting for an event, can be interrupted
DDisk sleepWaiting in the kernel, cannot be interrupted
TStoppedStopped by a signal such as SIGSTOP
ZZombieEnded, status waiting to be collected

Processes and threads share one structure

On Linux a thread is a process that shares its address space and several other resources with its siblings. Each thread has its own entry in the table and its own thread ID. The value getpid returns is the ID of the thread group, which is what you think of as the process, and gettid returns the ID of the individual thread.

thread_ids.cc
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>

static void *worker(void *arg)
{
    (void)arg;
    printf("worker: pid=%d tid=%ld\n", (int)getpid(), (long)syscall(SYS_gettid));
    return NULL;
}

int main(void)
{
    pthread_t t;
    pthread_create(&t, NULL, worker, NULL);
    pthread_join(t, NULL);
    printf("main:   pid=%d tid=%ld\n", (int)getpid(), (long)syscall(SYS_gettid));
    return 0;
}

Compile with gcc -pthread. The thread and the main thread report the same process ID but different thread IDs, and the main thread's thread ID equals the process ID:

Text
worker: pid=21040 tid=21041
main:   pid=21040 tid=21040

That identity is why a crash in one thread takes the whole process down: they share the address space, so there is nowhere separate for the fault to stay. It is also the main reason to choose processes when you want isolation and threads when you want to share data cheaply.

Where the process model is the wrong picture

The description above is Linux and POSIX. A few settings change it.

Containers do not create a new kind of process. A process in a container is an ordinary process in a different PID namespace, so it sees itself as PID 1 while the host sees a larger number. If you print getpid inside a container and compare it with ps outside, the two numbers differ for the same process.

Embedded targets without a memory management unit, such as many microcontrollers, have tasks instead of processes: no private address space, no protection between tasks and often no process table in the Unix sense. Real-time operating systems such as FreeRTOS use tasks that share one flat memory. If you are reading this to understand an RTOS, keep the identity and state ideas but drop the private address space.

Share

Keep reading

Flat illustration of two options weighed side by side, with the macOS, Linux and Apple logos.
Kernels

Mach Traps vs BSD System Calls on macOS

Learn how macOS routes one trap instruction to either a Mach trap or a BSD system call, how the number sign and class decide, and why you should call libSystem.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.