Memory

You might also like

You are on page 1of 53

Memory

Background

Program must be brought into memory and placed within a process for it to
be run.

Input queue collection of processes on the disk that are waiting to be


brought into memory to run the program.

User programs go through several steps before being run.

Binding of Instructions and Data to


Memory
Address binding of instructions and data to memory addresses can
happen at three different stages.
Compile time: If memory location known a priori, absolute
code can be generated; must recompile code if starting location
changes.
Load time: Must generate relocatable code if memory
location is not known at compile time.
Execution time: Binding delayed until run time if the process
can be moved during its execution from one memory segment
to another. Need hardware support for address maps (e.g., base
and limit registers).

Multistep Processing of a User


Program

Logical vs. Physical Address


Space

The concept of a logical address space that is bound to a separate physical


address space is central to proper memory management.
Logical address generated by the CPU; also referred to as virtual
address.
Physical address address seen by the memory unit.

Logical and physical addresses are the same in compile-time and load-time
address-binding schemes; logical (virtual) and physical addresses differ in
execution-time address-binding scheme.

Memory-Management Unit
(MMU)

Hardware device that maps virtual to physical address.

In MMU scheme, the value in the relocation register is added to every


address generated by a user process at the time it is sent to memory.

The user program deals with logical addresses; it never sees the real
physical addresses.

Dynamic relocation using a


relocation register

Swapping

A process can be swapped temporarily out of memory to a backing store, and


then brought back into memory for continued execution.

Backing store fast disk large enough to accommodate copies of all memory
images for all users; must provide direct access to these memory images.

Roll out, roll in swapping variant used for priority-based scheduling


algorithms; lower-priority process is swapped out so higher-priority process
can be loaded and executed.

Major part of swap time is transfer time; total transfer time is directly
proportional to the amount of memory swapped.

Modified versions of swapping are found on many systems, i.e., UNIX,


Linux, and Windows.

Schematic View of Swapping

Memory allocation

OS keeps a table indicating which parts of memory are available and which
are occupied. Initially, all memory is available for user processes, and is
considered as one large block of available memory, a hole.

When a process arrives and needs memory, we search for a hole large
enough for this process.

As processes enter the system, they are put into an i/p queue. The OS takes
into account the memory requirements of each processes and the amount of
available memory space in determining which processes are allocated
memory.

In general, a set of holes, of various sizes, is scattered through out memory


at any given time. When a process arrives and needs memory, the system
searches for a hole that is large enough for this process.
When a process terminates, it releases its block of memory, which is then
placed back in the set of holes.
If new hole is adjacent to other holes, these adjacent holes are merged to
form one larger hole.
At this point, the system may need to check whether there are processes
waiting for memory and whether this newly freed and recombined memory
could satisfy the demands of any of these waiting processes.
This procedure is a particular instance of the general Dynamic Storage
Allocation problem, which is how to satisfy a request of size n from a list
of free holes. There are many solutions to this problem.

Dynamic Storage-Allocation
Problem
How to satisfy a request of size n from a list of free holes.

First-fit: Allocate the first hole that is big enough.


Best-fit: Allocate the smallest hole that is big enough; must
search entire list, unless ordered by size. Produces the
smallest leftover hole.
Worst-fit: Allocate the largest hole; must also search entire
list. Produces the largest leftover hole.

First-fit and best-fit better than worst-fit in terms of


speed and storage utilization.

Fragmentation

External Fragmentation total memory space exists to satisfy a request,


but it is not contiguous.

Internal Fragmentation allocated memory may be slightly larger than


requested memory; this size difference is memory internal to a partition,
but not being used.

Reduce external fragmentation by compaction

External Fragmentation- as processes are loaded and removed from


memory, the free memory space is broken into little pieces. External
fragmentation exists when enough memory total memory space exists to
satisfy a request, but it is not contiguous; storage is fragmented into a large
no. of small holes.
Internal fragmentation- consider a multiple partition allocation scheme
with a hole of 18,467 bytes. Suppose next request is 18,465 bytes. If we
allocate exactly the request block, we are left with a hole of 2 bytes.

What is Paging?

Paging is a memory-management scheme that permits the physical address


space of a process to be non-contiguous.

Paging avoids the problem of fitting the varying-sized memory chunks


onto the backing store, from which most of the previous memorymanagement schemes suffered.

Logical address space of a process can be noncontiguous; process is


allocated physical memory whenever the latter is available.

Divide physical memory into fixed-sized blocks called frames (size is


power of 2, between 512 bytes and 8192 bytes).

Divide logical memory into blocks of same size called pages.

Keep track of all free frames.

To run a program of size n pages, need to find n free frames and load
program.

Set up a page table to translate logical to physical addresses.

Internal fragmentation

Address Translation Scheme

Address generated by CPU is divided into:


Page number (p) used as an index into a page table which contains
base address of each page in physical memory.
Page offset (d) combined with base address to define the physical
memory address that is sent to the memory unit.

Address Translation
Architecture

Paging Model

Paging Example

What is Segmentation?

An important aspect of memory management that became unavoidable


with paging is the separation of the users view of memory and the actual
physical memory.

The users view is mapped onto physical memory. The mapping allows
differentiation between logical memory and physical memory.

Consider, how you think of a program when you are writing it. Think of it
as a main program with a set of subroutines, procedures, functions, or
modules. There may also be various data structures : tables, arrays, stacks,
variables, and so on. Each of these modules is referred to by name. you
talk about the symbol table, function sqrt, the main program, without
caring what address in memory these elements occupy.
You are not concerned with whether the symbol table is stored before or
after the sqrt fn. Each of these segments is of variable length.

Users View of a Program

Segmentation is a memory-management scheme that supports this user


view of memory. A logical address space is a collection of segments.

Normally, the user program is compiled, and the compiler automatically


constructs segments reflecting the i/p program.

The construct:

<segment-number, offset>
Elements within a segment are identified by their offset from the beginning
of the segment. Each segment is of variable length.

Hardware

Although the user can now refer to objects in the program by a twodimensional address, the actual physical memory is still, of course, a onedimensional sequence of bytes. Thus, we must define an implementation to
map two dimensional user-defined address into one dimensional physical
addresses. This mapping is affected by a segment table. Each entry of the
segment table has a segment base and a segment limit.

Base contains the starting physical address whereas the limit specifies the
length of the segment.

S- Segment Number and d-offset


Segment number is used as an index into the segment table. The offset d
must be between 0 and the segment limit. If it is not, we trap to the OS. If
its legal it is added to the segment base to produce the address in physical
memory of the desired byte.

Segmentation Hardware

Consider, we have five segments numbered from 0 to 4. the segments are


stored in physical memory as shown. The segment table has a separate
entry for each segment, giving the beginning address of the segment in
physical memory (or base) and the length of that segment (or limit).

For example, segment 2 is 400 bytes long and begins at location 4300.
thus, a reference to byte 53, of segment 2 is mapped onto location
4300+53=4353. a reference to segment 3, byte 852, is mapped to 3200 (the
base of segment 3)+852=4052. a reference to byte 1222 of segment 0
would result in a trap to the OS, as this segment is only 1000 bytes long.

Example of Segmentation

Thrashing

If a process does not have enough pages, the page-fault rate is very high.
This leads to:
low CPU utilization.
operating system thinks that it needs to increase the degree of
multiprogramming.
another process added to the system.

Thrashing a process is busy swapping pages in and out.

Thrashing

Page Replacement

Prevent over-allocation of memory by modifying page-fault service routine


to include page replacement.

Use modify (dirty) bit to reduce overhead of page transfers only modified
pages are written to disk.

Page replacement completes separation between logical memory and


physical memory large virtual memory can be provided on a smaller
physical memory.

Need For Page Replacement

Basic Page Replacement


1. Find the location of the desired page on disk.
2. Find a free frame:
- If there is a free frame, use it.
- If there is no free frame, use a page replacement
select a victim frame.

algorithm to

3. Read the desired page into the (newly) free frame. Update the page and
frame tables.
4. Restart the process.

Page Replacement

Page Replacement Algorithms

Want lowest page-fault rate.


Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page faults on
that string.
In all our examples, the reference string is
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.

Graph of Page Faults Versus The


Number of Frames

First-In-First-Out (FIFO) Algorithm

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
3 frames (3 pages can be in memory at a time per process)

4 frames

FIFO Replacement Beladys Anomaly


3 3
more frames less page faults2

9 page faults

10 page faults

FIFO Page Replacement

FIFO Illustrating Beladys


Anamoly

Optimal Algorithm

Replace page that will not be used for longest period of time.
4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1

6 page faults

3
4

How do you know this?


Used for measuring how well your algorithm performs.

Optimal Page Replacement

Least Recently Used (LRU)


Algorithm

Reference string: 1, 2, 3, 4, 11, 2,5 5, 1, 2, 3, 4, 5


2

Counter implementation
Every page entry has a counter; every time page is referenced through
this entry, copy the clock into the counter.
When a page needs to be changed, look at the counters to determine
which are to change.

LRU Page Replacement

LRU Algorithm (Cont.)

Stack implementation keep a stack of page numbers in a double link


form:
Page referenced:
move it to the top
requires 6 pointers to be changed
No search for replacement

Use Of A Stack to Record The Most Recent


Page References

LRU Approximation Algorithms

Reference bit
With each page associate a bit, initially = 0
When page is referenced bit set to 1.
Replace the one which is 0 (if one exists). We do not know the
order, however.
Second chance
Need reference bit.
Clock replacement.
If page to be replaced (in clock order) has reference bit = 1. then:
set reference bit 0.
leave page in memory.
replace next page (in clock order), subject to same rules.

Second-Chance (clock) PageReplacement Algorithm

Counting Algorithms

Keep a counter of the number of references that have been made to each
page.

LFU Algorithm: replaces page with smallest count.

MFU Algorithm: based on the argument that the page with the smallest
count was probably just brought in and has yet to be used.

Allocation of Frames

Each process needs minimum number of pages.


Example: IBM 370 6 pages to handle SS MOVE instruction:
instruction is 6 bytes, might span 2 pages.
2 pages to handle from.
2 pages to handle to.
Two major allocation schemes.
fixed allocation
priority allocation

Fixed Allocation
Equal allocation e.g., if 100 frames and 5 processes, give
each 20 pages.
Proportional allocation Allocate according to the size of
process.
si size of process pi
S si
m total number of frames
s
ai allocation for pi i m
S
m 64
si 10
s2 127
10
64 5
137
127
a2
64 59
137
a1

Priority Allocation

Use a proportional allocation scheme using priorities rather than size.

If process Pi generates a page fault,


select for replacement one of its frames.
select for replacement a frame from a process with lower priority
number.

Global vs. Local Allocation

Global replacement process selects a replacement frame from the set of


all frames; one process can take a frame from another.

Local replacement each process selects from only its own set of allocated
frames.

You might also like