I am trying to put up the high level description of different stages in program life time from source code to its execution.
Points:
- Preprocessing: Macros, include files and compiler directive are processed in this phase.
- Compilation: Source files are compiled into obj files
- Linker: Different obj files are linked to single executable. At this stage the virtual addresses are assigned to functions,variables, data in executable. For 32 bit machine, each process has 4GB of address space. And 1-2 GB is reserved of OS. So address space in 2-3 GB can be assigned to any process.
- Execution: During program execution loader comes into picture. It basically loads the program from virtual address space to physical memory address. So when process starts executing, OS allocates memory for the process and call its main function.
Questions:
If a program binary image size is 2MB, then is it that complete binary image has to be loaded into physical memory for program execution? My understanding is that the program has to be fully loaded in physical memory for execution. It will not be possible to run a program of size 512MB on a machine with 256 MB of physical memory. Only when memory requirement of program grows then virtual memory and paging are helpful.
When program asks for more memory, i.e. when it allocates heap memory using new/malloc, then memory gets reserved in virtual address space. It will not get committed until it is referenced.
Please point out wherever you feel my understanding is wrong.
Is there any article or blog that could give me a one or two page high-level description of the whole process?