Memory Management - MetaLab

clippersdogheartedSoftware and s/w Development

Dec 14, 2013 (3 years and 8 months ago)

83 views

CSNB334 Advanced Operating
Systems

5. Memory Management


Review of basic concepts


What are the requirements of memory
management?


Relocation


For managing the available memory in a multiprogramming
environment.


Protection


Must be satisfied by the hardware (processor) rather than the
OS.


Permissibility of a memory reference by an instruction can only be
checked at the time of execution of the instruction.


Sharing


Physical Organization


Moving information between the main memory and secondary
memory.

Relocation


Phenomenon by which a process may occupy
different partitions during the course of its life.


Three types of addresses


Logical Address


Reference to a memory location independent of the current
assignment of the process to memory.


Relative Address


Type of logical address


address is expressed as a location
relative to some known point.


Physical Address/ Absolute Addres


Actual location in main memory.

Loading


Absolute Loading


Decision of where to load a module in the memory is made at
compile time.


Thus, a given module is always loaded into a specific location in main
memory.


Relocatable Loading.


Decision is made at load time.


Thus a module can be loaded anywhere in the main memory.


But, is swapped back to the same memory.


Dynamic Run
-
Time loading


Decision is made at run
-
time.


Therefore, we can swap a process image into different locations at
different times.


Done by special processor hardware rather than software.


Base Register, Bounds Register, Adder, Comparator.

Memory Management Techniques


Fixed partitioning


Main memory is divided into a number of static partitions at
system generation time.


Equal
-
size partitions


Unequal
-
size partitions


Pros


Simple to implement; little OS overhead


Cons


Inefficient : internal fragmentation


Maximum number of active processes


fixed.


Placement Algorithm


One process queue per partition.


Single queue.

Fixed partitioning Placement
Algorithm



Memory Management Techniques


Dynamic partitioning


Partitions are created dynamically


Each process is loaded into a partition of exactly the same size as
that process.


Pros


No internal fragmentation.


Cons


External fragmentation.


Counteract : by compaction.


But an overhead for the processor.


Placement Algorithm : (because compaction is time consuming)


Best
-
fit


First
-
fit.


Next
-
fit.


Dynamic partitioning


Dynamic partitioning

Memory Management Techniques


Paging


Main memory is divided into a number of equal
-
sized, relatively
small frames.


Each process is divided into a number of equal
-
sized pages


same length as a frame.


A process is loaded by loading all of its pages into available
frames.


Not necessarily be contiguous.


Possible thru the use of a page table for each process.


Logical address (page number, offset)
---

Physical Address (frame
number, offset).


Pros


No external fragmentation


Cons


A small amount of internal fragmentation.

Address Translation in a Paging System

Program

Paging

Main Memory

Logical address

Register

Page Table

Page

Frame

Offset

P#

Frame #

Page Table Ptr

Page #

Offset

Frame #

Offset

+

Memory Management Techniques


Segmentation


Each process is divided into number of segments.


Need not be of same size.


A process is loaded by loading all of its segments into dynamic
partitions.


Need not be contiguous


Use segment table.


Difference with dynamic partitioning


A process may occupy more than one partition.


Partitions need not be contiguous.


Pros


No internal fragmentation.


Cons


External fragmentation : though less severe than dynamic partitioning
because of the small size of the segments.

Hardware support

Base + d

Program

Segmentation

Main Memory

Virtual Address

Register

Segment Table

Segment

d

S#

Length Base

Seg Table Ptr

Seg #

Offset = d

Segment Table

+

+

Memory Management Techniques


Virtual memory


Similar to paging/segmentation except that it is not necessary to
load all of the segments/pages of a process into main memory.


Nonresident pages/segments that are needed are brought in later
automatically.


May require writing a page/segment out to disk if the memory is full.


Pros:


Large virtual address space.


More processes may be maintained in main memory.


A process may be larger than all of main memory.


Cons


Overhead of complex memory management.


Thrashing : The system spends most of its time swapping pieces rather
than executing instructions.

Translation Lookaside Buffer


Every virtual memory reference causes two
physical memory access:


To fetch the appropriate page table entry.


To fetch the desired data.


Thus, the memory access time is doubled. To
overcome this:


Use a special high
-
speed cache for page table
entries


Translation Lookaside Buffer


Similar to a memory cache.


Contains those page table entries


most recently used.


Use of a Translation Lookaside Buffer

Virtual Memory


Virtual memory can be based on


Paging only


Virtual Address : Page Number + Offset


Page table entry : P(bit)+M(bit)+ Frame Number


Segmentation


Virtual Address : Segment Number + Offset


Segment table entry : P(bit)+M(bit)+ Length + Segment Base


Or, a combination of the two.


Virtual Address : Segment Number + PageNumber + Offset


Segment table entry : Length + Segment Base


Page table entry : P(bit)+M(bit)+ Frame Number

Memory Management in Linux


Linux uses
demand paged virtual memory
for memory
management design.


It's a dynamic memory allocation technique that consists of
deferring page frame allocation until the last possible moment, for
example, when a process attemps to access a page that is not
present in RAM.


Basic unit of memory allcation


page.


Page size : 2
12

(4096 bytes or 4KB).


Allocation
of blocks in physical memory is as page frames


Protection
mechanism is page by page


Sharing is
also based on pages


Swapping controls

automatic movement through the memory
hierarchy.

Getting the Page size


The standard POSIX method


#include <unistd.h>


long sysconf (int name);


long page_size = sysconf(_SC_PAGESIZE);


Linux also provides


int getpagesize (void);


Returns the page size in bytes.


PAGE_SIZE macro


int page_size = PAGE_SIZE


Retrieves the page size at compile time.

Abstract view of memory management

Managing the Virtual Address Space in
Linux


Each process : its own virtual address space.


In i386 arch, the virtual address is 32
-
bits wide.


Therefore, the total virtual memory that a virtual address can
reference = 2
32

= 4GB.


Page Size = 2
12
. Therefore, number of pages that a virtual
address can reference = 2
20
.


Assuming that each PTE is 4 bytes, how many pages are
needed to store the page table?

Two Level Page Table


The amount of memory devoted to page tables alone is
quite high.


Therefore, page tables are stored in virtual memory
rather than main memory. This is achieved thru the use
of a two
-
level hierarchical page table.


Root Page
Table (4KB)

User Page
Table (4MB)

User Address
Space (4 GB)

Two
-
Level Page
-
Table Scheme

Hierarchical paging


Address translation scheme:

Virtual
-
> physical address translation

……is a three level process in Linux

Virtual address


4 parts:



Page directory offset
j.pgd



Page middle director offset
j.pmd



Page table offset
j.pte



Offset within page
j.offset


The physical address i for a virtual address j
is :


i = PTE(PMD(PGD(j.pgd)+j.pmd)+j.pte)+j.offset.


The x86(32
-
bit addressing) only supports a two level conversion of the address.


This is dictated by the hardware’s MMU…


This is accomplished by reducing each page middle directory to only a
single entry.


Segmentation in Linux


Linux uses the segmentation model in a limited way.


Each virtual address space is divided into
two

segments:


User segment (3
Gb
)

to contain the applications code and
data. Addressable by the user.


Unmapped virtual addresses are simply not used.


Kernel segment (1
Gb
)

permanently mapped and associated
with fixed physical memory addresses used by the kernel.


System calls execute in kernel segment(mode).

4 GB

3 GB

2 GB

1 GB

ox00000000

Kernel

Tasks

Kernel Space (Code + Data)

User Space (Code + Data)

Per
-
Process Virtual Memory Layout


Code (also called text)
segment


Static Data segments


Initialized global (and C
static) variables


Uninitialized global
variables (zeroed when
initializing the process, also
called bss)


Stack segment: function
calls, local variables (also
called automatic in C)


Heap segment (malloc())


Page Table Flags


Each entry in the theoretical page table contains the
following information:


Valid flag. This indicates if this page table entry is valid,


The physical page frame number that this entry is describing,


Access control information. This describes how the page may
be used. Can it be written to? Does it contain executable
code?


Flags in the page table entry indicate


The legal access modes into the page.


The page’s status.


A page’s status can give vital information for
how memory management is performed.

Page Table Flags


PAGE_NONE


No physical memory page
associated with entry.


PAGE_SHARED


All types of access permitted.


PAGE_READONLY


No writing. “Copy
-
on
-
Write”
can be used.


PAGE_KERNEL


kernel segment only allowed
access.


PAGE_KERNEL_RO


kernel read
-
only access.