1
$\begingroup$

Why are programs kept in main memory for execution? As per my understanding, we need a memory management scheme to manage several processes in the main memory. These things can be done in secondary memory as well (I think). Just to improve performance, we need main memory or there is something more to it?

$\endgroup$
  • $\begingroup$ Because accessing stuff from disc is orders of magnitude slower? $\endgroup$ – David Richerby Nov 20 '17 at 17:15
  • 2
    $\begingroup$ As a matter of fact, it is very common in embedded systems to execute the code from the flash (so it is not copied to the ram, this saves a lot of space). This is usually called Execute-in-place. It works because the flash is fast enough. $\endgroup$ – Amaury Pouly Nov 20 '17 at 18:02
2
$\begingroup$

They are not. On modern desktop/server OSes, the program generally mmapped (or equivalent) into virtual memory and the currently running parts are paged in as needed. However, the remainder of the program can sit on secondary storage and never be accessed.

The reason for the currently executing parts of memory being paged in as opposed to running directly from secondary storage is performance. In general, any read access to secondary storage tends to be cached in memory, either directly through virtual memory or indirectly through something like a disk cache. In fact, in the main memory there's an extensive cache hierarchy (typically 3 layers on x86 Intel / AMD processors) so even the idea that the program is store in "main" memory is mostly a convenient simplification, not a complete description of reality. What actually happens is that some parts of the program are paged into main memory (by the OS) and some parts of the the program from main memory is cached into the L3 cache (by the CPU) and some parts of that are cached into the L2 cache(by the CPU) and some parts of that are cached into the L1 instruction cache (by the CPU).

On embedded systems and more exotic architectures, deviations from this pattern definitely exist. Execute-in-place was already mentioned but another example is Harvard architecture processors, where instruction and data memory are completely separate and code runs from the instruction memory which isn't addressable as data. Small embedded microcontrollers like PIC and AVR are examples of such processors.

$\endgroup$
1
$\begingroup$

For every processor that I have seen in the last 30 years, code must be in a location that can be accessed by the memory-read hardware of the processor in order to be executed. Accessible to that hardware is RAM, ROM and variants, and the hardware could be designed to make things like flash memory accessible that way.

Typically, software is actually stored on a hard drive or flash drive, it is mapped to memory locations using virtual memory, so when the processor tries to read an instruction from main memory, and the code isn't there, an interrupt will interrupt that program execution, run code that reads the data from hard drive or flash drive and stores it somewhere in main memory, and then continues program execution.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.