Memory Management Presentation

feastcanadianΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 7 μήνες)

81 εμφανίσεις

The Functions of
Operating Systems

Memory Management

Learning Objectives

Explain how memory is managed in a
typical modern computer system.

Storing a job in the computer's
main memory

Must be done in order for a job to be able to use
the processor.

Jobs can be stored concurrently in main memory
(although only one can be processed at a time)
.


As long as there is enough main memory and they,
and their data, are separated (so they do not interfere
with each other).

Jobs will also need to moved out of main
memory at some point.


Ideally at termination but lack of space due to other
jobs being given priority
(see scheduling in last lesson)
can
also cause this
.

Memory Management

C terminates and
job E, requiring 25k
of memory, is next
in the ready queue.

Job E cannot be
loaded into the
space that job C
has relinquished.

However, there is
20k + 10 k = 30k of
memory free in
total.

Fragmentation of memory

As jobs and files are loaded into memory
they occupy space which when vacated
leaves gaps in memory.

If a larger file is sent to that area.

It has to be broken up to fit.

This splitting of available memory into
discontiguous more manageable pieces is
called fragmentation.

Methods of fragmentation

1. Paging

Paging

Memory is divided into fixed size units
called physical pages.

Jobs are divided into same fixed size units
pages called logical pages.

Logical job pages can then be assigned to
any physical page in memory.

Need to keep an index table recording
which physical page (in memory) is
holding which logical (job) page.

Pages Example

Job B terminates, releasing four pages, and jobs
D and E are ready to be loaded.

The 'hole' consists of four pages into which job D
(three pages) will fit, leaving one page plus the
original one page of free memory.

E consists of two pages, so there is enough
memory for E but the pages are not contiguous,
in other words they are not joined together.

Paging
-

Jobs do not have to occupy
contiguous pages

Address Allocation
-

Paging

Keep a table that shows which memory
pages are used for the job pages.


Then, if each address used in a job consists
of a page number and the distance the
required location is from the start of the page,
a suitable conversion is possible.

Example

Now suppose that an instruction specifies an address as
logical page 5, displacement (from start of page) 46.

The OS will look up the physical page it is currently
residing in, in a Logical


Physical Page Index table.

Then it will look up the physical page basic address, in a
Physical Page Address Table.

The OS checks that the displacement is not greater than
a page size.


If it is, an error is reported.


Otherwise the displacement is added to the base address to
produce the actual address in memory to be used.

Index Table

Physical Page
Number

Logical Page
Number

Base
Address

8

5

3000

Displacement

e.g. 46

Physical Address

e.g. 3046

+

Paging Address Algorithm

1.
Get logical page number and displacement.

2.
Use logical page number to find physical page
number from the Logical


Physical Index
table.

3.
If displacement is greater than a page size
then produce an error message and stop.

4.
Use Physical Page number to find base
address of page from Physical Page Address
table.

5.
Add displacement to base address to produce
physical address.

2. Segmentation

Segmentation

Variable length blocks.

Programmers divide jobs into segments,
possibly of different sizes.

Usually, the segments would consist of
data, or sub
-
routines or groups of related
sub
-
routines.

Segmentation


Address Calculation

Since segments may be of different
lengths, address calculation has to be
carefully checked.

The segment table must contain
beginning, size and contents of each
segment.

This is needed to ensure that an address
does not go out of range.

Programmer split Job A into 4 segments and Job B into
3 segments.

These two jobs, when loaded into memory, took up the
positions shown.

Example

Example Continued …

Now suppose that an instruction specifies an
address as segment 3, displacement (from start
of segment) 132.

The OS will look up, in the process segment
table, the basic address (in memory) of segment
3.

The OS checks that the displacement is not
greater than the segment size.


If it is, an error is reported.


Otherwise the displacement is added to the base
address to produce the actual address in memory to
be used.

Base Address

Segment Size

Segment

3500

1500

3

Segment Index Table

Displacement

e.g. 132

Physical Address

e.g. 3632

+

Segmentation Address Algorithm

1.
Use segment number to find length of
segment from Segment table.

2.
If displacement is greater than segment
size produce error message and stop.

3.
Use segment number to find base
address of segment from Segment table.

4.
Add displacement to base address to
produce physical address.

3. Partitioning

Do confuse this with disk partitioning.

Partitioning

Basically memory is divided but not
programs.

Fixed Partitioning

Equal
-
size partitions


Any process whose size is less than or equal to the
partition size can be loaded into an available partition.


If all partitions are full, the operating system can swap
a process out of a partition.


Main memory use is inefficient. Any program, no
matter how small, occupies an entire partition. This is
called internal fragmentation.


Also programs may not fit in a partition so either
forcing partitions to be large enough to hold the
largest of programs or forcing the use of paging as
well.

Variable Partitioning

Unequal
-
size partitions

Can assign each process to the smallest
partition within which it will fit.

Queue for each partition.

Processes are assigned in such a way as
to minimize wasted memory within a
partition.

Dynamic Variable Partitioning

Partitions are of variable length and
number.

Process is allocated exactly as much
memory as required.

Eventually get holes in the memory. This
is called external fragmentation.

Must use compaction to shift processes so
they are contiguous and all free memory is
in one block.

Dynamic
Variable
Partitioning
Placement Algorithms

Operating system must decide which free block
to allocate to a process

Best
-
fit algorithm


Chooses the block that is closest in size to the
request.


Worst performer overall.


Since smallest block is found for process, the
smallest amount of fragmentation is left.


Memory compaction must be done more often.

Dynamic
Variable
Partitioning
Placement Algorithm

First
-
fit algorithm


Scans memory form the beginning and
chooses the first available block that is large
enough.


Fastest


May have many process loaded in the front
end of memory that must be searched over
when trying to find a free block.

Dynamic
Variable
Partitioning
Placement Algorithm

Next
-
fit


Scans memory from the location of the last
placement.

Partitioning


More research

http://cseweb.ucsd.edu/classes/fa06/cse120/lect
ures/120
-
fa06
-
l10.pdf


https://docs.google.com/viewer?url=https%3A%
2F%2Fpeople.sunyit.edu%2F~sengupta%2FCS
330%2FChapter07.ppt


http://en.wikipedia.org/wiki/Memory_manageme
nt_(operating_systems)


The Loader

The part of the OS which:


Decides where to place code for jobs /
program or procedures in main memory.


Loads / copies the code from storage into
main memory and adjusts the addresses.

1.
Where will pages / segments be
stored if lack of space in main
memory forces the need to
move them out?

2.

What happens if a large
software program which
requires more main memory
than is physically available is
needed?

Virtual Memory

Part of the storage
(usually the hard disk)

is
allocated to act as virtual main memory.

Pages / segments are then imported to
main memory when they are needed.

A whole job does not need to be resident
in memory at the same time but the user
believes whole job is in main memory
simultaneously.

Virtual Memory

Allows a piece of software that is so big
that it will not fit into memory to be used.


By splitting the job before it is loaded into
the main memory.

Jobs must still be transferred to main
memory to be used.


Provides high speed input to main memory

As it is still quicker than opening a file / program from its
original form on storage
.

Example


Virtual Memory Paging

Web browser Page 1

Web Browser Page 2

Spreadsheet Page 1

Presentation Authoring Page 1

Presentation Authoring Page 2

Spreadsheet Page 2

Virtual Memory

Main Memory

Spreadsheet Page 1

Web browser Page 1

Presentation Authoring
Page 1

OS

OS

Example


Virtual Memory Paging

A word processor:


Page 1
-

instructions to allow users to enter text and
to alter the text.


Page 2
-

code for formatting characters.


Page 3
-

code for formatting paragraphs.


Page 4
-

the code for cutting, copying and pasting.

There is not enough space in Main Memory but
to run this word processor only page 1 needs to
be loaded initially into Main Memory.

We first load all the pages into Virtual Memory.

Example


Virtual Memory Paging

Word Processor Page 1

Web browser Page 1

Word Processor Page 2

Word Processor Page 3

Word Processor Page 4

Web Browser Page 2

Spreadsheet Page 1

Presentation Authoring Page 1

Presentation Authoring Page 2

Spreadsheet Page 2

Virtual Memory

Main Memory

Spreadsheet Page 1

Web browser Page 1

Presentation Authoring
Page 1

OS

OS

Example


Virtual Memory Paging

Word Processor Page 1

Web browser Page 1

Word Processor Page 2

Word Processor Page 3

Word Processor Page 4

Web Browser Page 2

Spreadsheet Page 1

Presentation Authoring Page 1

Presentation Authoring Page 2

Spreadsheet Page 2

Virtual Memory

Main Memory

Spreadsheet Page 1

Web browser Page 1

Presentation Authoring
Page 1

OS

OS

Example


Virtual Memory Paging

Word Processor Page 1

Web browser Page 1

Word Processor Page 2

Word Processor Page 3

Word Processor Page 4

Web Browser Page 2

Spreadsheet Page 1

Presentation Authoring Page 1

Presentation Authoring Page 2

Spreadsheet Page 2

Virtual Memory

Main Memory

Spreadsheet Page 1

Web browser Page 1

Word Processor Page 1

Presentation Authoring
Page 1

OS

OS

Example


Virtual Memory Paging

If the user then wants to format some
characters then page 2 will have to be
loaded.

But there is not enough space so we load
page 2 from Virtual Memory over Page 1
in Main Memory.

Example


Virtual Memory Paging

Word Processor Page 1

Web browser Page 1

Word Processor Page 2

Word Processor Page 3

Word Processor Page 4

Web Browser Page 2

Spreadsheet Page 1

Presentation Authoring Page 1

Presentation Authoring Page 2

Spreadsheet Page 2

Virtual Memory

Main Memory

Spreadsheet Page 1

Web browser Page 1

Word Processor Page
2

Presentation Authoring
Page 1

OS

OS

Example


Virtual Memory Paging

Now lets say that the user wants to type
some characters so the computer needs
Page 1 again.

But there is not enough space so we load
page 1 from Virtual Memory over Page 2
in Main Memory.

Example


Virtual Memory Paging

Word Processor Page 1

Web browser Page 1

Word Processor Page 2

Word Processor Page 3

Word Processor Page 4

Web Browser Page 2

Spreadsheet Page 1

Presentation Authoring Page 1

Presentation Authoring Page 2

Spreadsheet Page 2

Virtual Memory

Main Memory

Spreadsheet Page 1

Web browser Page 1

Word Processor Page
1

Presentation Authoring
Page 1

OS

OS

Example


Virtual Memory Paging

When other facilities are needed, the
appropriate page is loaded.

Virtual memory

If there is not enough space in main memory then the
whole job is stored in virtual memory and only needed
pages are copied into main memory.

If a page is required that is not already in main memory
then one of the other job’s pages in main memory is
replaced
(a redundant page)
.

The needed page is loaded into main memory by
copying it from virtual memory over the redundant page.

Remember the redundant page still exists in virtual
memory so it usually
(see next slide)

just erased from main
memory when it is replaced as it can be copied back into
main memory from virtual memory when needed.

Pages which have changed

Some pages contain a job's data that will
change during the running of a program.

So a page to be erased may need to be saved
first if it has been altered
(copied back to virtual
memory)
.


To keep track of this the OS keeps a flag for each
page that can be initially set to zero.


If the content of the page changes, the flag can be set
to 1.


Before overwriting a page, the OS can see if that
page has been changed.


If it has, then the OS will save the page before
loading a new page in its place.

Which pages can be overwritten?

There are many systems that can be used
to decide.


The page that has not been used for the
longest period of time.


First
in first out method.

All of these create extra work for the OS.

Disk Threshing / Thrashing

The process of swapping (loading and
saving) pages.


If the memory is very full this process can use
up a lot of the processor's time.

Note:

Virtual memory can be also be used to describe
the act of address calculation and the use of p
art
of the storage
(usually the hard disk)

which tricks
programs into thinking they are using large
blocks of
contiguous

addresses while in fact it is
physically fragmented and overflows into virtual
memory.

Virtual memory can use segmentation as well as
paging although this can become very complex.

Cache Memory

A type of very fast memory
(faster than main memory)

that is
used to improve the speed of a computer.

Acts as an intermediate store between the processor
(CPU) and the main memory.

Works by storing the most frequently used or recently
used instructions and data so that it will be very fast to
retrieve them again.

When an item of data is required, a whole block of data
will be read into the cache in the expectation that the
next piece of data will be read into the cache in the
expectation that the next piece of data required is likely
to be in the same block.

Cache Memory

Processor


Cache

Memory


Block of data
transferred


Main

Memory


Single word
of data
transferred

Plenary

Explain how memory is managed in a
typical modern computer system.

Plenary

Fragmentation of memory:


As jobs and files are loaded into memory
they occupy space which when vacated
leaves gaps in memory.


If a larger file is sent to that area and others.


It has to be broken up to fit.


This splitting of available memory into
discontiguous pieces is called
fragmentation.

Plenary

Paging:


Memory is divided into regular sized areas
called pages.


Jobs or files are allocated a number of
pages according to size of job.


Pages may be discontinuous.


Index of pages/files kept.


Addresses can be calculated by adding
page address to raw address.

Plenary

Segmentation:


Memory is divided into variable length
blocks called segments.


Jobs or files can consist of many segments,
different number each time taken into
memory.


Segments normally match natural divide in
jobs.


Index of segments stored which must store
base address and length of segment.

Plenary

Virtual memory:


A whole job does not need to be resident in memory
at the same time.


When a new page is required it is loaded into
memory over a redundant page from a reserved
area of the hard drive.


User believes whole job in memory simultaneously.


Erased page may need to be saved first if it has
been altered.


Use of cache memory.


Problem of threshing
(thrashing).