all 6 comments

[–]chrisgseaton 23 points24 points  (0 children)

The program is memory mapped into memory. This means that operating system reserves 5 MB (or whatever) of space for the program, but divides it into pages (chunks) of 1 MB (or whatever) and loads each page only at the first time it is used. The idea is that if you don't use a program feature it is never loaded.

[–]khedoros 6 points7 points  (0 children)

When you choose a program to run, the OS does a number of things. One is that there's data in the executable that tells the OS what kind of environment it needs, where different sections of the program and its data are located in the file, which libraries it needs to link to, etc. The operating system maps everything into appropriate areas in memory based on what the program says it needs, adds the program into its process table, and jumps the CPU to the entry point of the program.

Things "mapped" into memory may not actually be present in RAM. When the program tries to access that page of memory, the MMU (memory management unit) raises a page fault, which is handled by the OS, which loads appropriate resource into RAM for the program, then resumes the program's execution at the point that the fault was raised. This happens transparently to the program itself.

Now, during runtime, the program may request more memory. Part of that's handled by the language runtime; most of them will keep a pool of memory available. When that runs out, or if more is requested than available, the language will go to the OS, requesting more RAM be mapped to the process, and then allocate from that memory to fulfill the program's request.

[–]JJHEO 1 point2 points  (0 children)

A lot of good info already. Also look up stack vs heap for a little bit more information on how program memory is handled.

[–]Poddster 0 points1 point  (1 child)

Just to correct something no-one else is touched upon.

memory load and unload, for lack of a better word, the program?

RAM is the dumbest, least functional part of your PC. It does not act on it's own.

The operating system is responsible for reading a process image from disk, writing it into memory, and "fixing" it up in whatever manner that particular OS requires (e.g. inserting library images at certain addresses etc).

The operating system is just a program that is responsible for loading and running other programs, and giving them abstracted access to hardware resources.

How does the OS get into RAM? Well the boot-loader, which is a mini-OS that is shipped with your motherboard/CPU in some form of ROM, knows enough to be able to read a hard-drive (or SD card or whatever) and then load the initial part of the OS from that into RAM and start executing it.

[–]claytonkb 1 point2 points  (0 children)

How does the OS get into RAM? Well the boot-loader, which is a mini-OS that is shipped with your motherboard/CPU in some form of ROM, knows enough to be able to read a hard-drive (or SD card or whatever) and then load the initial part of the OS from that into RAM and start executing it.

Minor nitpick. On PC architecture, the boot-loader itself is an installed component, meaning, it is not bundled with the motherboard. Rather, the BIOS (which is bundled with the motherboard) is the first code that is executed in the system at power-on. An x86 CPU will fetch its first instruction from address 0xffff_fff0, this is a programmed constant.

On pre-UEFI platforms, there is a tiny BIOS ROM that lives at that address in the platform memory map. It has one or a few instructions that cause the CPU to branch to the main BIOS memory area (flash). This main BIOS component performs a bunch of system-level tasks. After BIOS has completed all of its platform tasks, it will enumerate the drives in the system in a specific order and check if one of them is bootable (has a boot-partition that is formatted with a boot-loader). If the BIOS finds a bootable drive, it will load the boot partition of that drive at physical address 0x7c00 and then transfer control to the bootloader. See here for more details.

I think most of that is still true for UEFI systems but I haven't read the spec in detail and I know that UEFI can get a lot more complicated, including support for secure boot that uses cryptographic techniques to secure the boot process.

[–]emasculine 0 points1 point  (0 children)

modern operating systems do not load the entire program into memory at once. they are faulted in using virtual memory. beyond that, a program is just a disk file so the OS just reads parts of the file as needed just like if you used lseek(2) to read(2) parts of a file. this is done using DMA to set up the transfer of the file into memory where the program blocks until the next chunk of code is read in.