Processes & the Process Abstraction

the text (code) segment

When the recipe is being cooked, the printed instructions themselves do not change as you cook: you read them, but you do not scribble over the steps. The text segment of a process is exactly that: the part of the address space that holds the program's machine instructions, the compiled code the CPU executes, sitting in memory to be read but normally never rewritten.

When the OS loads a program, it places the executable's instructions into the text segment, usually at the low end of the address space, and marks those pages as read-only and executable. The CPU's program counter points into this region and steps through the instructions one after another, jumping around for loops and function calls. Marking it read-only is a safety feature: a bug or an attacker that tries to overwrite code triggers a fault instead of silently changing what the program does. Because the instructions are read-only and identical for every instance, the OS can share one physical copy of the text segment among all processes running the same program, saving memory.

So the text segment is the stable, executable heart of a process. It matters for protection (writable-but-not-executable and executable-but-not-writable are key defenses against code injection) and for efficiency (shared, read-only code keeps many processes lightweight). A subtle point: the text segment holds the instructions, while the changing values they work on live in the data, heap, and stack, separating the unchanging recipe from the ingredients being transformed.

Run the same compiled tool a hundred times; the OS keeps one read-only copy of its text segment in physical memory and maps it into all hundred processes, instead of duplicating the code a hundred times.

Read-only code can be shared safely across all processes running it.

Read-only is the norm, not a law. Some systems allow self-modifying code or just-in-time compilation, which needs writable-then-executable memory and is handled with extra care.

Also called
code segmenttext section程式碼區段code 段