0% found this document useful (0 votes)
21 views

Lect 5

The document discusses different memory management techniques used in operating systems, including logical vs physical address spaces, swapping, contiguous allocation, paging, segmentation, and segmentation with paging. It provides background on memory management challenges due to limited memory size and the need to load multiple processes into memory concurrently. It describes how different techniques map logical addresses to physical addresses and handle issues like fragmentation.

Uploaded by

George Helmy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lect 5

The document discusses different memory management techniques used in operating systems, including logical vs physical address spaces, swapping, contiguous allocation, paging, segmentation, and segmentation with paging. It provides background on memory management challenges due to limited memory size and the need to load multiple processes into memory concurrently. It describes how different techniques map logical addresses to physical addresses and handle issues like fragmentation.

Uploaded by

George Helmy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Operating System Fundamentals

Lecture 5 : Memory Management


Outline
◼ Background
◼ Logical versus Physical Address Space
◼ Swapping
◼ Contiguous Allocation
◼ Paging
◼ Segmentation
◼ Segmentation with Paging
Background
◼ Program must be brought into memory to be executed.
◼ Input queue – collection of processes on the disk that
are waiting to be brought into memory.
◼ Motivation: Keep several processes in memory to
improve a system’s performance.
◼ Selection of different memory management methods:
❑ Application-dependent.
❑ Hardware-dependent.
◼ Memory – A large array of words or bytes, each with its
own address.
◼ Memory is always too small !
Binding of Instructions and Data to Memory
Address binding can happen at three different stages.
◼ Compile time: If memory location known a priori, must
recompile code if starting location changes.
❑ Rarely used (MSDOS system files).
◼ Load time: If memory location is not known at compile time,
compiler must generate relocatable addresses. Located by
loader. Fixed while a program runs.
❑ Used in old systems (MSDOS applications)
❑ Not used much any more.
◼ Execution time: Binding delayed until run-time if the process
can be moved during its execution from one memory segment
to another.
❑ Currently dominates in multi-tasking systems (Windows

and UNIX).
Logical vs. Physical Address Space
◼ A logical (virtual) address space is the set of
logical addresses generated by a process.

◼ MMU maps from virtual to physical (actual)


addresses.

◼ Different memory mapping schemes need


different MMU’s.

◼ Logical and physical addresses are the same


in compile-time but differ in load-time and
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.
Memory-Management Unit (MMU)
Dynamic Loading
◼ A routine will not be loaded until it is called.
◼ A relocatable loader must be called to load
the desired routine and to change/update the
program’s address tables.
◼ Better memory-space utilization; unused
routine is never loaded.
◼ Useful when large amounts of code are
needed to handle infrequently occurring
cases.
Dynamic Linking (1)
◼ With dynamic linking, frequently used
routines are not linked into the program.
Instead, just a small piece of code called a
stub is linked.
◼ When the routine is called, the stub checks to
see if the real routine is loaded (it may have
been loaded by another program).
❑ If not loaded, load it.
❑ If already loaded, share it. This needs some OS
help so that different jobs sharing the library don't
overwrite each other's private memory.
Dynamic Linking (2)
◼ Advantages of dynamic linking.
❑ Saves space: Routine only in memory once even
when used many times.
❑ Bug fix to dynamically linked library fixes all
applications that use that library, without having
to re-link the application.
◼ Disadvantages of dynamic linking.
❑ New bugs in dynamically linked library infect all
applications.
❑ Applications “change” even when they haven't
changed.
Overlays
◼ Keep in memory only those instructions and
data that are needed at any given time.
◼ Needed when process is larger than amount
of memory allocated to it.
◼ Implemented by user, no special support
needed from operating system.
◼ Programming design of overlay structure is
complex.
Contiguous Allocation (1)
◼ Main memory usually divided into two partitions:
❑ Resident operating system, in low memory with
interrupt vector.
❑ User processes then held in high memory.

◼ Single-partition allocation
❑ Relocation-register scheme used to protect user
processes from each other, and from changing
operating-system code and data.
❑ Relocation (base) register contains value of
smallest physical address; limit register contains
range of logical addresses – each logical address
must be less than the limit register.
Contiguous Allocation (2)
◼ Multiple-partition allocation
❑ Hole – block of available memory; holes of various size
are scattered throughout memory.
❑ When a process arrives, it is allocated memory from a
hole large enough to accommodate it.
❑ Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
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
❑ Compaction is to Shuffle memory contents to place all free
memory together in one large block.
❑ Compaction is possible only if relocation is dynamic, and is
done at execution time.
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 (separate partition) and Windows (Swap file).
Schematic View of Swapping

Should a process be put back into the same


memory space that it occupied previously? Binding
Scheme (execution or load-time scheme) ?!
Paging
◼ Problem: Logical address space of a process
can be noncontiguous (External Fragmentation).

◼ Objective : Users see a logically contiguous


address space although its physical addresses are
throughout physical memory.
Paging : Approach
◼ 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 Example
Implementation of Page Table
◼ Page table is kept in main memory.
◼ Page-table base register (PTBR) points to
the page table.
◼ Page-table length register (PRLR) indicates
size of the page table.
◼ In this scheme every data/instruction access
requires two memory accesses. One for the
page table and one for the data/instruction.
Segmentation
◼ Memory-management scheme that supports
user view of memory.
◼ A program is a collection of segments. A
segment is a logical unit such as:
❑ main program,
❑ procedure,
❑ function,
❑ Local variables, global variables,
❑ common block,
❑ stack,
❑ symbol table, arrays.
Logical View of Segmentation
Segmentation Architecture
◼ Logical address consists of two tuples:
<segment-number, offset>,
◼ Segment table – maps two-dimensional physical
addresses; each table entry has:
❑ base – contains the starting physical address where the
segments reside in memory.
❑ limit – specifies the length of the segment.
◼ Segment-table base register (STBR) points to the
segment table’s location in memory.
◼ Segment-table length register (STLR) indicates
number of segments used by a program;
“segment number s is legal if s < STLR”
Segmentation

◼ Allocation.
❑ First fit/best fit
❑ External fragmentation
◼ Relocation.
❑ Dynamic
❑ By segment table
Segmentation Example
Segmentation Example
Segmentation with Paging – MULTICS
◼ The system solved problems of external
fragmentation and lengthy search times by
paging the segments.

◼ Solution differs from pure segmentation in


that the segment-table entry contains not the
base address of the segment, but rather the
base address of a page table for this
segment.
MULTICS Address Translation Scheme

You might also like