Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 66 ยท Processes

What Is a Process?

Learn what a process is, how it differs from a program, what its memory layout (text, data, heap, stack) looks like, and inspect a live process in Python.

A program is a file on disk: a passive list of instructions. A process is a program in execution: the instructions plus everything the OS needs to run them, such as the current position in the code, the variables in memory, the open files and the CPU registers. If you double-click a text editor twice, you start one program but two processes.

Processes are the basic unit of work for the OS. Almost everything in this course (scheduling, synchronization, memory management) is about running many processes at once on limited hardware. This lesson defines a process, shows how its memory is laid out and which resources belong to it, and lets you inspect a real one.

Program vs process

Program A file. Passive. Stored on disk.
Process A running instance. Active. Uses CPU and memory.

One program can have many processes at the same time.

1 / 2

Memory layout of a process

The OS gives each process its own address space: a private range of memory addresses that looks like it belongs to that process alone. Inside, the memory is split into regions:

flowchart TB
    S["Stack: function calls, local variables (grows down)"]
    F["Free space"]
    H["Heap: dynamic memory, malloc / new (grows up)"]
    D["Data: global and static variables"]
    T["Text: machine code (read-only)"]
    S --- F --- H --- D --- T

High addresses at the top; the stack and heap grow toward each other.

  • Text (code) segment: the compiled instructions, usually read-only and shareable between processes running the same program.
  • Data segment: global and static variables, split into initialized data and zero-initialized (BSS).
  • Heap: memory requested at run time (malloc, new, Python object allocation). It grows upward.
  • Stack: one stack frame per function call, holding arguments, local variables and the return address. It grows downward, which is why unbounded recursion ends in a stack overflow.

Why does it matter? Because the OS (using the hardware's memory-management unit) enforces the boundaries. If one process writes to an address outside its own space, the CPU raises a fault and the OS usually terminates it (the Memory Management chapter explains how).

What identifies a process

Every process has a unique process ID (PID), a parent PID (the process that created it), an owner (user ID) and a current state such as running or waiting (process states). The OS stores all of this in a data structure called the process control block.

Here is a real look at the process your Python code is running in. The exact PID will differ each time; what matters is that every field exists and the relationships hold.

Python Inspecting this 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 input()), it gets the next line from this box.

Seeing the stack and heap grow

The stack holds one frame per active function call. Recursion makes that visible. The program below counts how many frames Python allows before it protects itself with an error (a language-level guard that stands in for the OS's hard stack limit). It also shows that heap objects live independently of the call stack.

Python Stack frames and the heap

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 input()), it gets the next line from this box.

In a compiled language the OS enforces the stack limit (typically 8 MB on Linux). Exceeding it triggers a segmentation fault instead of a Python exception.

One program, many processes

Starting the same program twice gives two processes with the same text segment but different data, heap, stack and PIDs. The OS often shares the read-only text pages between them to save memory.

Python Two processes, one program

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 input()), it gets the next line from this box.

Process versus thread, in one sentence

A process is the unit that owns resources (address space, files). A thread is a unit of execution inside a process. The next chapter covers threads in detail.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between a program and a process?

    A program is code stored on disk. A process is that code plus the state needed to run it.
  2. 2. Which region of a process's memory holds local variables and return addresses of function calls?

    Each function call pushes a stack frame with its locals, arguments and return address.
  3. 3. Which region is normally read-only and may be shared between processes running the same program?

    The code does not change while running, so its pages can be marked read-only and shared.
  4. 4. You open the same editor twice. How many processes do you have?

    Each launch creates a new process with its own PID and address space.

Summary

  • A process is a program in execution: code, current position, memory, open files and identity.
  • Its address space has text, data, heap and stack regions; the stack and heap grow toward each other.
  • Each process has a PID and parent PID, and the OS isolates address spaces from each other.
  • Many processes can run the same program with different data.

Next: Process States and the PCB