MCS Lecture # 1

Embed Size (px)

Citation preview

  • 8/3/2019 MCS Lecture # 1

    1/27

    Memory Management Sadaqat Ali Khan Bangash 1

    Memory ManagementLecture

    Sadaqat Ali Khan BangashInstitute of Information Technology,University of Science & Technology Bannu

  • 8/3/2019 MCS Lecture # 1

    2/27

    Memory Management Sadaqat Ali Khan Bangash 2

    Memory Hierarchies

    Like everything else in computers, Computer Memory is alsoorganized into a hierarchy. This is as follows:

    At 1st Level (closest to Processor) are the registers At 2nd Level, one or two Levels of Cache Memory

    At 3rd Level, Main Memory (also known as primary memory)

  • 8/3/2019 MCS Lecture # 1

    3/27

    Memory Management Sadaqat Ali Khan Bangash 3

    Memory Hierarchies .

    All above are considered part of InternalMemory (i.e. Internal to Computer System).Then the Hierarchy continues onto External

    Memory

    At 4th Level, Secondary Storages (Hard disks,tapes etc)

    At 5th Level, other forms of memory comessuch as Zip Disks, Floppy Drives, etc

  • 8/3/2019 MCS Lecture # 1

    4/27

    Memory Management Sadaqat Ali Khan Bangash 4

    Register,Cache,

    Main Memory

    Hard disk.

    CD-Rom,DVD,

    Internal Storage

    Internal

    Memory

    ExternalMemory

  • 8/3/2019 MCS Lecture # 1

    5/27

    Memory Management Sadaqat Ali Khan Bangash 5

    Memory Hierarchies .

    Registers

    These temporary locations within the CPU areextremely fast, very small, very expensive & volatile.

    Cache

    Buffer memory regions between CPU & Main

    Memory.

  • 8/3/2019 MCS Lecture # 1

    6/27

    Memory Management Sadaqat Ali Khan Bangash 6

    Memory Hierarchies .

    Main Memory Main Memory also called Primary Memory, Random

    Access Memory (RAM). Hundreds of Megabytes of

    Medium Speed & Volatile

    Secondary Memory

    Hundreds of Gigabytes of memory with Slow speed

    We may recall the factors of Cost, Size, and Speed ofaccess that are the orders in which Memory

    components are divided into this hierarchy.

  • 8/3/2019 MCS Lecture # 1

    7/27

    Memory Management Sadaqat Ali Khan Bangash 7

    Purpose of Memory Management

    To ensure fair use of memory for processes

    To ensure secure access, i.e., space allocatedfor process is not allocated for another process,

    or a process does not have access to anythingbeyond its "process address space

    To ensure orderly memory access, i.e., whenmemory is to be allocated, when de-allocated,etc. etc.

    To ensure efficient use of memory. Sincememory is expensive, it should be usedefficiently

  • 8/3/2019 MCS Lecture # 1

    8/27

    Memory Management Sadaqat Ali Khan Bangash 8

    Uni-programming environment

    In a Uni-programming environment, main

    memory is divided into two parts:

    1. One part for the Operating System

    2. One part for the Program currently beingexecuted. (or the User Part)

  • 8/3/2019 MCS Lecture # 1

    9/27

    Memory Management Sadaqat Ali Khan Bangash 9

    Multi-programming environment

    In a multi-programming environment, the user part of thememory is further divided to accommodate multipleprocesses. This task of dividing and allocation is carried outdynamically by the Operating System and is known as

    memory management. Memory Manager has the following basic responsibilities:

    1. Keep track of used & free memory spaces (Also knownas Free Space Management)

    2. When, where and how much memory to allocate or de-

    allocate

    3. Swapping processes in and out of main memory tosecondary memory.

  • 8/3/2019 MCS Lecture # 1

    10/27

    Memory Management Sadaqat Ali Khan Bangash 10

    Logical & Physical Addresses

    Logical Address

    An address generated by the CPU

    This normally refers to an instruction or datawithin the process address space

    Physical Address

    An address for a main memory location where instructionor data resides

  • 8/3/2019 MCS Lecture # 1

    11/27

    Memory Management Sadaqat Ali Khan Bangash 11

    Logical & Physical Addresses

    Since the Process is already loaded in memory, the logicaladdresses of the process need to be translated to itscorresponding physical addresses.

    The CPU generates all logical Addresses for a particular process.A set of these logical addresses constitute the "Logical AddressSpace". Correspondingly, all the physical addresses thattranslate back to the logical addresses are known as the"Physical Address Space" for that process.

    This will be explained in detail in paging & segmentation. Thelogical address is also known as the Offset Address. So in

    simpler words,

  • 8/3/2019 MCS Lecture # 1

    12/27

    Memory Management Sadaqat Ali Khan Bangash 12

    Logical & Physical Addresses

    Logical Address Space

    The set of all logical addresses generated by a process

    Physical Address SpaceThe set of all physical addresses corresponding to those

    logical addresses

    This translation or mapping from Logical toPhysical address is done by a piece of CPUhardware known as the Memory ManagementUnit.

  • 8/3/2019 MCS Lecture # 1

    13/27

    Memory Management Sadaqat Ali Khan Bangash 13

    CPU

    MMU

    Base orRelocation

    Register Value:14000

    LogicalAddress

    346

    PhysicalAddress

    14346

    Process

    14000

    The MMU will check if the logical address is within the limitspecified by the limit register. If yes, it will add the value of therelocation register. If no, it will give an addressing error or atrap.

  • 8/3/2019 MCS Lecture # 1

    14/27

    Memory Management Sadaqat Ali Khan Bangash 14

    Using Memory Efficiently

    Dynamic Loading

    With dynamic loading, a routine is not loaded into main memoryuntil it is called. All these routines are kept on the disk in a re-locatable format. The main program is loaded into memory &executed.

    The best example for explaining this would be MS-Word. Wordhad thousands of features. At any given time, a user will only beusing a couple of these features. The routines for thoseadditional features are kept on disk whereas only the mainprogram for MS-Word is loaded.

    Dynamic Loading is also used in handling exceptions or errors.Routines for handling exceptions or errors are only loaded whenthey occur, otherwise they will remain on disk.

  • 8/3/2019 MCS Lecture # 1

    15/27

    Memory Management Sadaqat Ali Khan Bangash 15

    Using Memory Efficiently

    Advantages:

    Less time needed to load a program

    Less memory space needed for program

    Disadvantages:

    More run-time activity. Lot of I/O requests are made

    which is time consuming.

  • 8/3/2019 MCS Lecture # 1

    16/27

    Memory Management Sadaqat Ali Khan Bangash 16

    Dynamic Linking

    Advantages:

    Less time needed to load a program

    Less memory space needed Less disk space needed to store binaries

    Updated libraries are used without

    recompiling a program

    Disadvantages: More run-time activity resulting in slower

    program execution

  • 8/3/2019 MCS Lecture # 1

    17/27

    Memory Management Sadaqat Ali Khan Bangash 17

    Overlays

    Overlays allow a process to be larger than theamount of memory allocated to it. It works bykeeping only those instructions in memorythat are needed at any given time. I.e., lets

    suppose our Main Memory is 100 MB, will anexecutable of 110 MB will be able to run? Theanswer is yes if the underlying OperatingSystems employs Virtual Memory techniques.This is the concept behind Virtual Memory.

    It is essential to understand Overlays becauseit gives a flavor of Virtual Memory which wewould be cover later on.

  • 8/3/2019 MCS Lecture # 1

    18/27

    Memory Management Sadaqat Ali Khan Bangash 18

    Overlays

    When other instructions & data are needed,they are loaded into the space occupiedpreviously by instructions that are no longer

    needed. Overlays are implemented solely bythe programmer. If the program does nothave support for Virtual Memory or Overlays,then the Operating System cannot do anything

    about it.

  • 8/3/2019 MCS Lecture # 1

    19/27

    Memory Management Sadaqat Ali Khan Bangash 19

    Swapping

    A process can be swapped temporarily out of memory to abacking store, and then brought back into memory forcontinued execution.

    Backing store fast disk large enough to accommodatecopies of all memory images for all users; must providedirect access to these memory images

    Roll out, roll in swapping variant used for priority-basedscheduling 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 isdirectly proportional to the amount of memory swapped

  • 8/3/2019 MCS Lecture # 1

    20/27

    Memory Management Sadaqat Ali Khan Bangash 20

    Swapping

    Main Memory

    Secondary Memory

    The area where swapped processes are kept in secondary

    memory is known as Swap Space

    Memory Management Techniques Multi-Programming with Fixed Number of Tasks (MFT)

    Multi-Programming with Variable Number of Tasks (MVT) Paging

    Segmentation

    Virtual Memory

  • 8/3/2019 MCS Lecture # 1

    21/27

    Memory Management Sadaqat Ali Khan Bangash 21

    SwappingModified versions of swapping are found on many systems (i.e.,UNIX, Linux, and Windows)

    System maintains a ready queue of ready-to-run processes whichhave memory images on disk

  • 8/3/2019 MCS Lecture # 1

    22/27

    Memory Management Sadaqat Ali Khan Bangash 22

    Contiguous Allocation Main memory usually divided into two partitions:

    Resident operating system, usually held in lowmemory with interrupt vector

    User processes then held in high memory

    Relocation registers used to protect user processesfrom each other, and from changing operating-system code and data

    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

    MMU maps logical address dynamically

  • 8/3/2019 MCS Lecture # 1

    23/27

    Memory Management Sadaqat Ali Khan Bangash 23

    HW address protection with base and limit registers

  • 8/3/2019 MCS Lecture # 1

    24/27

    Memory Management Sadaqat Ali Khan Bangash 24

    Contiguous Allocation (Cont.) Multiple-partition allocation

    Hole block of available memory; holes of various sizeare scattered throughout memory

    When a process arrives, it is allocated memory from ahole large enough to accommodate it

    Operating system maintains information about:a) allocated partitions b) free partitions (hole)

    OS

    process 5

    process 8

    process 2

    OS

    process 5

    process 2

    OS

    process 5

    process 2

    OS

    process 5

    process 9

    process 2

    process 9

    process 10

  • 8/3/2019 MCS Lecture # 1

    25/27

    Memory Management Sadaqat Ali Khan Bangash 25

    Dynamic Storage-Allocation Problem

    First-fit: Allocate thefirsthole that is big enough

    Best-fit: Allocate the smallesthole 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

    How to satisfy a request of size n from a list of free holes

    First-fit and best-fit better than worst-fit in terms of speed andstorage utilization

  • 8/3/2019 MCS Lecture # 1

    26/27

    Memory Management Sadaqat Ali Khan Bangash 26

    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 sizedifference is memory internal to a partition, but notbeing used

    Reduce external fragmentation by compaction

    Shuffle memory contents to place all free memorytogether in one large block

    Compaction is possible only if relocation is

    dynamic, and is done at execution time I/O problem

    Latch job in memory while it is involved in I/O

    Do I/O only into OS buffers

  • 8/3/2019 MCS Lecture # 1

    27/27

    Memory Management Sadaqat Ali Khan Bangash 27

    End of Lecture

    Questions