Operating Systems Module 6: Memory Lesson 6.1 12 min

Memory without abstraction

What happens when programs share memory with nothing in between, and how base and bounds registers give every program its own private range.

You have two programs open. Both store the player’s score in a variable. If memory were just one long row of numbered cells and each program used the cell numbers directly, what would stop the second program from writing over the first one’s score?

On the earliest computers, nothing did. This lesson starts there, because the fix is the first step on the road to virtual memory.

Physical addresses and nothing else

Memory (RAM) is a long row of bytes, each with a number: its physical address. The simplest machine lets every program use those numbers directly. When a program says “store 7 at address 100”, cell 100 changes.

The trouble is that programs are written and compiled ahead of time. Two different programs can both be written to keep their data around address 100. Watch what happens when A and B both run. The strip is physical memory, and the chips below it list the cells that hold a value.

Two programs, one address

  1. Programs use physical addresses There is no translation. When A or B uses address N, the memory cell numbered N is touched, whoever else is using it. Nothing stops a program from reaching any cell.
    Faults so far
    0
    Overwrites so far
    0
  2. A writes 7 to address 100 A stores 7 in cell 100.
    Address used
    100
    Physical address
    100
    Faults so far
    0
    Overwrites so far
    0
  3. B writes 9 to address 100 B stores 9 in cell 100. But A had stored 7 there. It is gone: the two programs have overwritten each other, and neither was told. Program A had stored 7 in this very cell a moment ago.
    Address used
    100
    Physical address
    100
    Faults so far
    0
    Overwrites so far
    1
  4. A reads address 100 A reads cell 100 and gets 9, the value B stored there. A program has just read another program's data.
    Address used
    100
    Physical address
    100
    Faults so far
    0
    Overwrites so far
    1

Neither program did anything wrong by its own rules. The machine simply gave both of them the same cell. It gets worse: a buggy program could just as easily write over the operating system itself, since the OS lives in the same memory. This is why early personal computers could be brought down by one bad program.

Giving each program its own range

We want two things at once. Each program should be written as if it owns memory starting at address 0, and no program should be able to reach another’s memory. The simplest hardware to do this is two registers inside the CPU:

  • The base register holds the physical address where the program’s block starts.
  • The bounds register (also called the limit) holds the size of the block.

On every memory access the hardware does two things, in this order. First it checks that the address the program used is below the bounds. If not, the access is refused. If it is, the hardware adds the base and the sum is the physical address. Moving a program’s addresses by a constant like this is called relocation, and it happens on every access, in hardware, with no help from the program.

In the next simulator, A is given the block starting at 1000 and B the block starting at 2000, each 500 bytes long. Both use “their” address 100.

Base and bounds

  1. Each program gets its own block Physical memory is 4096 bytes. The OS has given A the block 1000 to 1499, B the block 2000 to 2499. Every program is written as if its memory starts at address 0; the hardware moves each address into the right block.
    Base register
    (none loaded)
    Bounds register
    (none loaded)
    Faults so far
    0
  2. The OS switches to A Before A runs, the OS loads A's own base (1000) and bounds (500) into the two registers. These registers are what make the same address mean different places for different programs.
    Base register
    1000
    Bounds register
    500
    Faults so far
    0
  3. A writes 7 to its address 100 The program uses address 100. The hardware first compares it with the bounds register: is 100 less than 500? Yes, so the access is allowed.
    Base register
    1000
    Bounds register
    500
    Address used
    100
    Check
    100 < 500: yes
    Faults so far
    0
  4. A reaches physical address 1100 The hardware adds the base: 1000 + 100 = 1100. The value 7 is stored there. The program never saw 1100; it still believes it used address 100.
    Base register
    1000
    Bounds register
    500
    Address used
    100
    Physical address
    1100
    Faults so far
    0
  5. The OS switches to B Before B runs, the OS loads B's own base (2000) and bounds (500) into the two registers. These registers are what make the same address mean different places for different programs.
    Base register
    2000
    Bounds register
    500
    Faults so far
    0
  6. B writes 9 to its address 100 The program uses address 100. The hardware first compares it with the bounds register: is 100 less than 500? Yes, so the access is allowed.
    Base register
    2000
    Bounds register
    500
    Address used
    100
    Check
    100 < 500: yes
    Faults so far
    0
  7. B reaches physical address 2100 The hardware adds the base: 2000 + 100 = 2100. The value 9 is stored there. The program never saw 2100; it still believes it used address 100.
    Base register
    2000
    Bounds register
    500
    Address used
    100
    Physical address
    2100
    Faults so far
    0
  8. The OS switches to A Before A runs, the OS loads A's own base (1000) and bounds (500) into the two registers. These registers are what make the same address mean different places for different programs.
    Base register
    1000
    Bounds register
    500
    Faults so far
    0
  9. A reads its address 100 The program uses address 100. The hardware first compares it with the bounds register: is 100 less than 500? Yes, so the access is allowed.
    Base register
    1000
    Bounds register
    500
    Address used
    100
    Check
    100 < 500: yes
    Faults so far
    0
  10. A reaches physical address 1100 The hardware adds the base: 1000 + 100 = 1100. The read returns 7. The program never saw 1100; it still believes it used address 100.
    Base register
    1000
    Bounds register
    500
    Address used
    100
    Physical address
    1100
    Faults so far
    0
  11. A reads its address 600 The program uses address 600. The hardware first compares it with the bounds register: is 600 less than 500? No, so the access is outside A's block.
    Base register
    1000
    Bounds register
    500
    Address used
    600
    Check
    600 < 500: no
    Faults so far
    0
  12. Fault: A went outside its block The hardware refuses the access and traps into the kernel (Lesson 1.3). The memory at 1600 belongs to someone else or to nobody, and it is never touched. The OS usually ends the program, with the message you know as "Segmentation fault".
    Base register
    1000
    Bounds register
    500
    Address used
    600
    Physical address
    none (fault)
    Faults so far
    1

Step through it twice. The second time, watch three things:

  1. A and B both used address 100, and each one read back its own value. The 7 and the 9 live in different cells (1100 and 2100).
  2. Each time the OS switches to a different program, it loads that program’s base and bounds into the two registers. The registers are part of what the OS saves and restores in a context switch (Lesson 2.5). The same address means a different place for each program because the registers differ.
  3. The last access, address 600, was outside A’s block. The hardware did not touch memory. It raised a fault: the CPU trapped into the kernel (Lesson 1.3), and the kernel usually ends the program. This is the “Segmentation fault” message you may have seen.

Open Try it and change the base, the bounds and the address. Use an address just below the bounds, then exactly the bounds. The last valid address is one less than the bounds, because addresses start at 0.

The kernel can change these registers; a user program cannot, because writing them is a privileged instruction (Lesson 1.3). If a program could change its own base, the whole scheme would mean nothing.

What is wrong with one block per process?

Base and bounds is simple and fast: one compare and one add. But it has limits:

  • The block must be one piece, and its size is fixed when the program starts. A program that needs more memory later may not have room next to it.
  • The block contains everything: code, heap and stack, plus the unused gap between the heap and the stack (Lesson 2.1). All of that space is reserved even though most of it is empty.
  • Two programs cannot share the code of a common library, because each block belongs to one program.

The next lessons fix these problems. First, one block can be split into several pieces, each with its own base and bounds. That gives us segments, and a new problem: holes between blocks.

Quick check

Quick check

  1. What does the base register hold?
  2. A program uses an address that is not below the bounds register. What happens?
  3. Why can two programs both use address 100 with base and bounds, but not without it?
  4. Why is giving every process one block of memory limiting?

Recap

  • With only physical addresses, every program can reach every cell, so programs can overwrite each other and the OS.
  • Base and bounds are two CPU registers. The hardware checks the address against the bounds, then adds the base: that is relocation.
  • An address outside the block is a fault: the CPU traps to the kernel and no memory is touched.
  • The OS loads each program’s registers when it switches to it. Only the kernel may change them.
  • One fixed block per process wastes space and cannot grow easily, which leads to segments and then paging.

Next: one block can be cut into several, each with its own base and bounds. That helps, and it creates a new problem: the gaps between blocks.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion