Docstoc

Operating Systems - users.cs.cf.ac.uk users.cs.cf

Document Sample
Operating Systems - users.cs.cf.ac.uk users.cs.cf Powered By Docstoc
					    CMT603

     Lecture 8
Memory Management 1
                      Contents
•   Addressing an Address binding
•   Logical and Physical Addressing
•   Memory Management Unit
•   Contiguous Addressing
    – Fixed Partition
    – Variable Partition
• Non-Contiguous Addressing
    – Paging
    – Segmentation
               Background
• Processes are loaded in
  to main memory              OS


• Who decides where they      P1
  are placed?
                              P2
• How do we ensure we         P3
  are accessing the correct
  process memory?             P4
           Address Binding
• Can happen at Compile Time
  – MS-DOS .com files


• Can happen at Load Time
  – Compiler generates a relocatable address


• Can happen during Execution Time
  – Address binding is set at run time
                Addressing

• Symbolic address
• Absolute address
• Relocatable Address

• Relocatable     -14 from the
                  bytes beginning of the module

• Symbolic        -count (a variable)

• Absolute        -74186 bytes from the
                  beginning of the memory
  Logical and Physical Addressing
• Physical address is             0
  the absolute address                 OS   Base
  within the main               1000        1000
  memory                      1112          112
  – This is the address the            P1
    hardware address bus                    Limit
    uses                        3000        2000
• Logical address is the               P2
  address within the            4000
  process relative to the              P3
  start of the process          4500

  – This is the address the            P4
    CPU uses                    5500
Basic MMU Addressing Hardware
                                           Trap to OS
                                         Addressing error
                Relocation                                  Memory
                 Register
      Logical     1000       Physical
      Address                Address
        112                    1112
CPU                 +                    >=        <




                                        1000     3000

                                        Base   Base+Limit
        Contiguous Addressing
           - Fixed Partition
• Memory broken into           0

                                   OS
  fixed size partitions     1000

                                   P1
• Each partition contains   2000

  one process                      P2
                            3000

                                   P3
                            4000

                                   P4
                            5000
       Contiguous Addressing
          - Fixed Partition
• Advantage
  – Simple
• Disadvantage
  – The degree of multiprogramming is
    constrained.
  – The size of each process is bounded.
  – Suffers internal fragmentation
    • Memory that is internal to a partition but is not
      being used
       Contiguous Addressing
         - Variable Partition
• Processes are stored in   0

                                 OS
  memory in variable     1000
  sized partitions
                                 P1
                          3000

                                 P2
                          4000
                                 P3
                          4500

                                 P4
                          5500
        Contiguous Addressing
          - Variable Partition
• Initially, all the memory is considered as
  one large block of available memory, a
  hole
• When a process needs memory, a hole
  large enough for the process is allocated
  for it
• A free memory list is used to track
  available memory.
                Contiguous Addressing
                  - Variable Partition
      P1 - 100MB
      P2 - 50MB                               OS
      P3 - 32MB
      P4 - 80MB          Free Memory List
      P5 - 32MB
                                            P1 100Mb
                         a    300MB
Into which hole should                      P2 50Mb
P5 be put?
                                            P3 32Mb

                                            P4 80Mb
         Placement Strategies
• First-fit
   – allocate the first hole large enough
• Best-fit
   – allocate hole with the smallest leftover space
• Worst-fit
   – allocate the largest hole
        Placement Strategies
First Fit Best Fit Worst Fit
                                     OS
        P5 - 32MB
    Free Memory List
                                   P1 100Mb
   a      38MB
   b      50MB                     P5 32Mb
                               b
                                   P3 32Mb

                                   P4 80Mb

                               a   P5 32Mb
                       Exercise
• Assume that we have 2560k of memory available and a
  resident OS of 400k. Given the input queue in the table,
  using FCFS, how would the First-Fit algorithm place the
  processes in the input queue (The newly freed memory
  hole is appended to the end of the free memory list)?

               Process Memory    Burst Time
                  P1      600K       10
                  P2     1000K        5
                  P3      300K       40
                  P4      500K       35
                  P5      200K       20
                                                   Process     Memory         Burst Time
                                                     P1           600K           10
  NB External fragmentation
                                                     P2         1000K             5
                                                     P3           300K           40
                                                     P4           500K           35
                                                     P5           200K           20
 0                0                0                 0                   0
         OS               OS              OS                 OS                 OS
400k             400k             400k             400k               400k
                          P1               P1                P1
                 1000k            1000k            1000k              1000k
                                                             P4                 P4
                                                   1500k              1500k
                          P2

                 2000k            2000k            2000k              2000k
                          P3               P3                P3                 P3
                 2300k            2300k            2300k              2300k
                                                   2500k
                                                             P5       2500k
                                                                                P5
2560k            2560k            2560k            2560k              2560k
        Time:0           Time:0           Time:5             Time:5           Time:10
       Contiguous Addressing
         - Variable Partition
• Suffers external fragmentation
  – the available memory space is broken into
    chunks, and the largest contiguous chunk is
    insufficient for a request.
 Reducing External Fragmentation
• Coalescing – merge adjacent holes to form a
  single, larger hole.
• Memory compaction (also referred to as
  garbage collection) – relocate all occupied areas
  of memory to one end of main memory. This
  leaves a single large free memory hole.
• Non-contiguous memory allocation:
  – Paging
  – Segmentation
Non Contiguous Memory Allocation
• A program is divided into blocks or
  segments that the system may place in
  nonadjacent slots in main memory

                   Paging
• Basic method
  – Break physical memory into fixed-sized blocks
    called frames.
  – Break logical memory into fixed-sized blocks
    called pages.
                         Frame number

   Paging                        0      Page 1

                                 1

                                 2

                                 3      Page 0
Page 0      Page Table
                                 4      Page 3
Page 1        0    3
                                 5
Page 2        1    0
                                        Page 2
Page 3        2    6             6

              3    4             7

                                 8

                                 9
    Paging - Representation of
             Address

0           d       Memory Address

1
                    p     d
2
                Page      Displacement
                Number
3
     Paging - Representation of
              Address
• The page (frame) size is defined by the
  hardware. It is typically a power of 2
  – If the size of a logical address space is 2n
    bytes, and a page size is 2m bytes.
     • The high-level n-m bits is used for the page
       number
     • The remaining m bits are used for the page offset.
              Page Number    Displacement

                        p     d
                       n-m    m
                                                 Physical memory

Paging Hardware


      Logical address         Physical Address

CPU       p       d                e    d

                      Page Table


              p

                          e
                                                          Physical memory
             Example                                 0
                                                                            0

                                                                            32
                                                     1
Page size = Frame size = 32 bytes                                           64
                                                     2
                                                                            96




    CPU            8     15                24   15

                              Page Table

                                                                            736
Physical Address                                     23
                         8                                                  778
                                 24                  24
= (24*32) + 15
= 793
                                                     25
                                                          Physical memory
             Exercise                                0
                                                                            0

                                                                            32
                                                     1
Page size = Frame size = 32 bytes                                           64
                                                     2
                                                                            96




    CPU            6     17                23   17

                              Page Table

                                                                            736
                         6       23                  23
Physical Address         7
                         8        2                                         778
                                 24                  24
= (23*32) + 17
= 753
                                                     25
       Sharing in a Paging System
Process 1 page table




 Process 2 page table




    Process 3 page table
            Fragmentation
• Paging suffers from internal Fragmentation
                      Summary
•   Addressing an Address binding
•   Logical and Physical Addressing
•   Memory Management Unit
•   Contiguous Addressing
    – Fixed Partition
    – Variable Partition
• Non-Contiguous Addressing
    – Paging
    – Segmentation
Exercise

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:17
posted:5/13/2012
language:
pages:29