Why every program gets its own address space
Video 038 of 140 in Virtualization Foundations, in the unit “Memory: RAM, addresses and virtual memory”. 4:28, with English and Hindi audio.
Two programs can run on the same computer, and each one behaves as if it owns the whole memory, with its own addresses. That can only work if the kernel keeps them apart. This article explains why shared real addresses would cause trouble, how a virtual address is looked up in a table that the kernel keeps for each program, how two programs can use the same address for different data, and how to look at a process's address map on Ubuntu.
What you will learn
- Each program has its own set of virtual addresses, called its address space.
- The kernel and processor translate each virtual address into a real place in RAM.
- A program cannot reach memory outside its own table.
- Programs can share memory, but only when the kernel maps it in on purpose.
Why programs cannot share real RAM addresses
- Programs trust each other, so a bug in one program could write over another program's data.
- Two programs could want the same address for different things, with no way to keep them apart.
- Every program would see the same physical memory addresses.
So the kernel, which is the core of the operating system, gives each program its own set of addresses, called virtual addresses. Real RAM addresses stay hidden behind them. The set of addresses a program may use is called its address space.
How one virtual address is looked up
A program uses a virtual address, which is a number that means something only inside its own address space. The processor's memory management unit, or MMU, looks the address up in the table that the kernel keeps for this program. Each program has one such table, and the kernel keeps all of them.
The table gives a real location in RAM, and the read or write happens there. If the program asks for an address with no entry, the access is refused, so the program cannot reach that memory.
How two programs use the same address, and why that is safe
Program A and program B can both use the virtual address 4096. Their tables send it to two different places in RAM. Each program's data stays apart, although both believe they hold the same address. Neither can reach the other's memory.
Programs can also share memory on purpose, such as a shared library, which is common code used by many programs. The kernel maps the same real memory into several tables.
Take a warehouse system, like the one Harbor Point runs. Its PostgreSQL database runs inside a virtual machine, which is a whole computer simulated by software. Each process inside, meaning one running program such as the database or a backup job, gets its own address space. The backup job cannot write into the database's cache, so if the backup job has a bug and crashes, the database keeps running.
What goes wrong when a program reaches outside its map
- A program touches memory outside its map, so the kernel refuses it and the program can be stopped.
- A leak: a program that never gives memory back keeps growing, until the whole system runs short.
You notice a program that crashes on a bad address, or a process whose memory keeps climbing. Keeping programs apart is the reason one server can run many services side by side. A faulty program can still fail on its own, but it cannot reach into the memory of the others.
How to look at a process's address map on Ubuntu
On Ubuntu, each process describes its own address space in a file named maps, under its own entry in the proc folder. The kernel fills that folder with live information about every process.
cat /proc/self/mapsThe stack is where a program keeps function calls and local variables. It is one of the ranges in the file, and you can pick it out with grep.
grep stack /proc/self/mapsTry it yourself
You need an Ubuntu machine. You will read the address ranges that the command itself may use, then pick out the stack from that list.
Print the address ranges that this process may use. The word self means the process running this command.
cat /proc/self/mapsWhat you should see: The address ranges this process may use, which the kernel lists for it.
Pick out the stack from the same list.
grep stack /proc/self/mapsWhat you should see: The stack, where a program keeps function calls and local variables.
Terms
- Virtual address
- The address a program uses, which the processor and kernel translate into a real place in RAM.
- Address space
- The set of virtual addresses that one program may use.
- Memory management unit (MMU)
- The part of the processor that looks up each virtual address in the kernel's table.
- Shared library
- Common code kept in one place in RAM and mapped into several programs that use it.
Check yourself
Why can two programs both use address 4096 without clashing?
Each program has its own table, so the same virtual address is sent to two different places in RAM.
What happens when a program asks for an address that has no entry in its table?
The access is refused, so the program cannot reach that memory.
Why can a backup job not write into a database's cache?
The backup job has its own addresses, and its table has no entry that leads to the database's memory.
Previous: ECC memory: catching flipped bits. Next: Pages and page tables.