Main Memory: Basic Hardware
Main Memory: Basic Hardware
Main Memory: Basic Hardware
Main Memory
Basic Hardware
• It should be noted that from the memory chips point of view, all memory
accesses are equivalent. The memory hardware doesn't know what a particular
part of memory is being used for, nor does it care. This is almost true of the OS
as well, although not entirely.
• The CPU can only access its registers and main memory. It cannot, for example,
make direct access to the hard drive, so any data stored there must first be
transferred into the main memory chips before the CPU can work with it
• Memory accesses to registers are very fast, generally one clock tick, and a CPU
may be able to execute more than one machine instruction per clock tick.
• Memory accesses to main memory are comparatively slow, and may take a
number of clock ticks to complete. This would require intolerable waiting by
the CPU if it were not for an intermediary fast memory cache built into most
modern CPUs. The basic idea of the cache is to transfer chunks of memory at a
time from the main memory to the cache, and then to access individual memory
locations one at a time from the cache.
• User processes must be restricted so that they only access memory locations
that "belong" to that particular process. This is usually implemented using a
base register and a limit register for each process, as shown in Figures 8.1 and
8.2 below.
2
• The address generated by the CPU is a logical address, whereas the address
actually seen by the memory hardware is a physical address.
o In this case the logical address is also known as a virtual address, and
the two terms are used interchangeably by our text.
o The set of all logical addresses used by a program composes the logical
address space, and the set of all corresponding physical addresses
composes the physical address space.
• The run time mapping of logical to physical addresses is handled by
the memory-management unit, MMU.
o The MMU can take on many forms. One of the simplest is a
modification of the base-register scheme described earlier.
o The base register is now termed a relocation register, whose value is
added to every memory request at the hardware level.
• Note that user programs never see physical addresses. User programs work
entirely in logical address space, and any memory references or manipulations
are done using purely logical addresses. Only when the address gets sent to the
physical memory chips is the physical memory address generated.
Swapping
➢ Standard Swapping
• The system shown in Figure below allows protection against user programs
accessing areas that they should not, allows programs to be relocated to
4
different memory starting addresses as needed, and allows the memory space
devoted to the OS to grow or shrink dynamically as needs change
➢ Memory Allocation
• Simulations show that either first or best fit are better than worst fit in terms of
both time and storage utilization. First and best fits are about equal in terms of
storage utilization, but first fit is faster.
➢ Fragmentation
Segmentation
➢ Basic Method
➢ Segmentation Hardware
Paging
➢ Basic Method
• The basic idea behind paging is to divide physical memory into a number of
equal sized blocks called frames, and to divide a programs logical memory
space into blocks of the same size called pages.
• Any page ( from any process ) can be placed into any available frame.
• The page table is used to look up what frame a particular page is stored in at
the moment. In the following example, for instance, page 2 of the program's
logical memory is currently stored in frame 3 of physical memory:
8
• A logical address consists of two parts: A page number in which the address
resides, and an offset from the beginning of that page. ( The number of bits in
the page number limits how many pages a single process can address. The
number of bits in the offset determines the maximum size of each page, and
should correspond to the system frame size. )
• The page table maps the page number to a frame number, to yield a physical
address which also has two parts: The frame number and the offset within that
frame. The number of bits in the frame number determines how many frames
the system can address, and the number of bits in the offset determines the size
of each frame.
• Page numbers, frame numbers, and frame sizes are determined by the
architecture, but are typically powers of two, allowing addresses to be split at a
certain number of bits. For example, if the logical address size is 2^m and the
page size is 2^n, then the high-order m-n bits of a logical address designate the
page number and the remaining n bits represent the offset.
• Note that paging is like having a table of relocation registers, one for each page
of the logical memory.
• There is no external fragmentation with paging. All blocks of physical memory
are used, and there are no gaps in between and no problems with finding the
right sized hole for a particular chunk of memory.
• There is, however, internal fragmentation. Memory is allocated in chunks the
size of a page, and on the average, the last page will only be half full, wasting
on the average half a page of memory per process
• Larger page sizes waste more memory, but are more efficient in terms of
overhead. Modern trends have been to increase page sizes, and some systems
even have multiple size pages to try and make the best of both worlds.
• When a process requests memory ( e.g. when its code is loaded in from disk ),
free frames are allocated from a free-frame list, and inserted into that process's
page table.
• Processes are blocked from accessing anyone else's memory because all of their
memory requests are mapped through their page table. There is no way for
them to generate an address that maps into any other process's memory space.
• The operating system must keep track of each individual process's page table,
updating it whenever the process's pages get moved in and out of memory, and
applying the correct page table when processing system calls for a particular
process. This all increases the overhead involved when swapping processes in
and out of the CPU. ( The currently active page table must be updated to reflect
the process that is currently running. )
11
➢ Hardware Support
• Page lookups must be done for every memory reference, and whenever a
process gets swapped in or out of the CPU, its page table must be swapped in
and out too, along with the instruction registers, etc. It is therefore appropriate
to provide hardware support for this operation, in order to make it as fast as
possible and to make process switches as fast as possible also.
• An option is to store the page table in main memory, and to use a single register
( called the page-table base register, PTBR ) to record where in memory the
page table is located.
o Process switching is fast, because only the single register needs to be
changed.
o However memory access just got half as fast, because every memory
access now requires two memory accesses - One to fetch the frame
number from memory and then another one to access the desired
memory location.
o The solution to this problem is to use a very special high-speed memory
device called the translation look-aside buffer, TLB.
• The benefit of the TLB is that it can search an entire table for a key value in
parallel, and if it is found anywhere in the table, then the corresponding lookup
value is returned.
12
• The TLB is very expensive, however, and therefore very small. ( Not large
enough to hold the entire page table. ) It is therefore used as a cache device.
• Addresses are first checked against the TLB, and if the info is not there ( a TLB
miss ), then the frame is looked up from main memory and the TLB is updated.
• The percentage of time that the desired information is found in the TLB is
termed the hit ratio.
➢ Protection
• The page table can also help to protect processes from accessing memory that
they shouldn't, or their own memory in ways that they shouldn't.
• A bit or bits can be added to the page table to classify a page as read-write,
read-only, read-write-execute, or some combination of these sorts of things.
Then each memory reference can be checked to ensure it is accessing the
memory in the appropriate mode.
• Valid / invalid bits can be added to "mask off" entries in the page table that are
not in use by the current process, as shown by example in Figure below.
• Note that the valid / invalid bits described above cannot block all illegal
memory accesses, due to the internal fragmentation. ( Areas of memory in the
last page that are not entirely filled by the process, and may contain data left
over by whoever used that frame last. )
13
➢ Shared Pages
• Paging systems can make it very easy to share blocks of memory, by simply
duplicating page numbers in multiple page frames. This may be done with
either code or data.
• If code is reentrant, that means that it does not write to or change the code in
any way ( it is non self-modifying ), and it is therefore safe to re-enter it. More
importantly, it means the code can be shared by multiple processes, so long as
each has their own copy of the data and registers, including the instruction
register.
• In the example given below, three different users are running the editor
simultaneously, but the code is only loaded into memory ( in the page frames )
one time.
• Some systems also implement shared memory in this fashion.
14
➢ Hierarchical Paging
• Most modern computer systems support logical address spaces of 2^32 to 2^64
• One option is to use a two-tier paging system, i.e. to page the page table.
15
• One common data structure for accessing data that is sparsely distributed over a
broad range of possible values is with hash tables. Figure 8.16 below illustrates
a hashed page table using chain-and-bucket hashing:
• Another approach is to use an inverted page table. Instead of a table listing all
of the pages for a particular process, an inverted page table lists all of the pages
currently loaded in memory, for all processes. ( I.e. there is one entry
per frame instead of one entry per page. )
• Access to an inverted page table can be slow, as it may be necessary to search
the entire table in order to find the desired page ( or to discover that it is not
there. ) Hashing the table can help speedup the search process.
16