How is the code memory managed?

I recommend http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory (and the other memory related articles) if you're interested in finding out more about the details of process' memory management.

code memory = Text segment

Notice how the address space is 4GB. When the kernel creates a process it gives it virtual memory. Below is an example of a 32 bit OS. The kernel manages what addresses get mapped to actual RAM via the processor's MMU. So, the kernel and the MMU manage code memory, just as they manage the entire address space of a process.

Borrowed from duartes.org
(source: duartes.org)


It is managed by the operation system. When a program is run, it's code is loaded from an executable file to some memory address. Depending on the nature of the program, some changes are applied to the code sections, e.g. jumps to dynamically linked libraries are resolved.

As proposed by Space_C0wb0y, check out en.wikipedia.org/wiki/Dynamic_linker for details on what is going on.


The code memory doesn't need managing because it doesn't change. When an executable is loaded into the address space, the code is just read from the executable file into memory. In fact, on modern operating systems, the code segment is just memory mapped to the executable file.