Operating Systems 635-321


 McGill Conted

 



Advanced Search

animated computer crash by Denton






Corbis Images FALL 2000
Lecture Notes 9: Click here to download the course notes in zipped format.


MEMORY MANAGEMENT 1


SINGLE USER CONTIGUOUS SYSTEM:

  • Each program to be processed is loaded in its entirety into memory.
  • If program doesn’t fit then it can’t be run
  • Doesn’t support multiprogramming.
  • Algorithm to load job in single user system:

    1. Store first memory location into base register
    2. Set program counter to address of first memory location.
    3. Get an instruction.
    4. Increment program counter by # bytes in instruction.
    5. Has the last instruction been reached?

    6.    If yes, stop loading program
    7. Is the program counter greater than memory size?

    8.    If yes, stop loading the program
    9. Load instruction into memory
    10. Go to step 3

    Memory management very easy – just needs to keep track of base address and value of accumulator. First computers (40’s and 50’s) had this type of memory management. Very uneconomical system $200,000 for equipment that could only be used by one person at a time.

    FIXED PARTITIONS.

    First attempt to allow for multiprogramming. One fixed (or static) partition for each job. Partition size designated when machine started. To change has to power down.

    Critical factor to protect the partition’s boundaries. Algorithm more complex:

    Determine job’s requested memory size.
    If job_size > largest partition
       Reject job – move to next job
    set counter to 1
    do while counter < = # partitions
       if job_size > partition[counter].size
          counter++
       else if partition[counter].status = "free"
          then load job into partition[counter]
          and change status of partition[counter] to busy.
             Else counter++;
    End while
    No partition available at this time – put job in queue
    Get next job

    This algorithm still requires that the entire program be stored contiguously in memory from the beginning to the end of execution. Memory manager must keep a table of partitions, their size, the job that is in them and their current status.

    Size
    100K
    25K
    25K
    50K
    Address
    200K
    300K
    325K
    375K
    Job
    Job 1
    Job 4

    Job 2
    Status
    Busy
    Busy
    Free
    Busy

    would be the status for an empty machine in the following jobs had come in :

    Job1 30K, Job2 50K, Job3 30K, Job4 25k. Job 3 must wait even though there is 70K space in partition 1. If partitions are set too small, larger jobs will be rejected or have a longer turnaround. If partitions set too big, space will be wasted. The creation of unused spaces within partitions because of too large partition size is called internal fragmentation.

     

    DYNAMIC PARTITIONS

    With dynamic partitioning, available memory is still kept in contiguous blocks, but jobs are only given as much memory as they need. This is an improvement over fixed partitions, but still has problems.

    When jobs first loaded in, memory fully utilized. When jobs are finished, a memory space the size of the previous job is freed up. These partitions can be allocated by best fit or first fit. Subsequent allocation creates fragments of free memory between blocks. This is called external fragmentation.

    FIRST-FIT

    Job list (J1 10K, J2 20K, J3 30K, J4 10K)

    Location

    Size

    Job

    Job size

    Status

    Fragmentation

    10K

    30K

    1

    10K

    Busy

    20K

    40K

    15K

    4

    10K

    Busy

    5K

    55K

    50K

    2

    20K

    Busy

    30K

    105K

    20K

       

    Free

     

    Available 115K Used 40K

    In this algorithm, a job is put into the first free space that it fits into, but you can see there can be problems. In example above there is 75 K of unused space, but Job 3 that needs 30K cannot be allocated 30K of contiguous space.

    First fit requires memory manager to keep lists of busy blocks and free blocks. Size of each incoming job compared to block until large enough block is found.

    counter = 0;
    do while counter <= # blocks
    if job_size > memory[counter].size
       then counter++
    else
       load job into memory[counter]
       adjust status – change free list
    if not loaded put job on queue
    get next job

    To show how free list would change...

    Suppose a job requests 200K from the following free list.
    BEFORE
    AFTER
    Address
    4075
    5225
    6785
    7560
    7605
    Size
    105
    5
    600
    20
    210
    Address
    4075
    5225
    6985
    7560
    7605
    Size
    105
    5
    400
    20
    210

    BEST-FIT

    Job list (J1 10K, J2 20K, J3 30K, J4 10K)

    Location

    Size

    Job

    Job size

    Status

    Fragmentation

    10K

    30K

    J3

    30K

    Busy

    None

    40k

    15k

    J1

    10K

    Busy

    5K

    55K

    50K

    J4

    10K

    Busy

    40K

    105K

    20K

    J2

    20K

    Busy

    None

    Available 115K Used 70K

    Fragmentation has been reduced and J3 got to run faster, but the allocation process takes more time.

    Algorithm for best fit.

    Initialize Min_waste to 99999.
    Set Good_spot to 0
    Set counter to 0
    Do while counter <= # blocks
       If job_size > memory[counter].size
          Then counter++
       Else
          Waste = memory[counter].size – job_size
          If Waste < Min_waste
             Min_waste = Waste
             Good_spot = counter
             counter++
    end while
    if Good_spot = 0
       then put job in queue
       else load job into memory[Good_spot]
          change free memory list
    get next job>

    Entire set of free memory blocks must be searched because they are stored in physical sequence. This takes time – they could be sorted but that would take time too.

     

    To show how free list would change...


    Suppose a job requests 200K from the following free list.
    BEFORE
    AFTER
    Address
    4076
    5226
    6786
    7561
    7605
    Size
    105
    5
    600
    20
    210
    Address
    4076
    5226
    6786
    7561
    7805
    Size
    105
    5
    600
    20
    10

    Best fit results in a better fit, but results in slivers of unused memory.

    DEALLOCATION

    Fixed partition can just use binary 0,1 for free and busy.
    With dynamic partitions, must take account of these situations:
    1. when the block to be deallocated is adjacent to another free block.
    2. When the block to be deallocated is between two free blocks
    3. When the block to be deallocated is isolated from other free blocks.

    RELOCATABLE DYNAMIC PARTITIONS

    In this approach to memory management, programs are relocated to put together all the empty space – a bit like defragging a disk. This is called "garbage collection".

    The great difficulty with this approach is the overhead associated with the relocation of the instructions. A computer instruction is a series of binary codes that contain addresses and instruction codes. However, if the program is to be moved each address must be flagged so that later the amount of memory locations to which the program has been displaced can be accounted for. Particularly important for loops and branching, since this is done by address.

    What has to be done by the memory manager to implement this approach?

    1. Actually move all the programs to consolidate space.
    2. Use special purpose registers that are available in some computers. There is a relocation register that contains the value that must be added to each address referenced in the program.
    3. Update the free and busy list.

    The overhead is so large in this approach that a crucial question is how often should it be done?

    1. one approach is to do it when memory is getting full, say around 75%. However, if no jobs were waiting this would be unnecessary overhead.
    2. Another is to compact when there are jobs waiting to get into the system
    3. Thirdly do it after a prescribed amount of time. If amount to small – excessive overhead. If too large, compaction not effective, too many jobs in queue.

    Common elements of all these early approaches to memory management is that they require the entire program

    1. be loaded into memory
    2. be stored contiguously
    3. remain in memory until job is completed.

    Later memory managers allow programs to not be stored contiguously – but can be divided into segments or pages. Each page or segment could be stored wherever there was an empty space. Furthermore, these pages or segments do not all have to reside in memory during the execution of the job.

    Worked examples:

    Given the following information:

    Job List
    Memory List

    Job #

    Memory Requested

    Block

    Size

    1

    740K

    1

    610K

    2

    500K

    2

    850K

    3

    700K

    3

    700K

    1. Where will the jobs go under the best-fit algorithm?
    2. Where will the jobs go with the first-fit algorithm?

     

    Repeat with the following:

    Job List
    Memory List

    Job #

    Memory Requested

    Block

    Size

    1

    700K

    1

    610K

    2

    500K

    2

    850K

    3

    740K

    3

    700K

     

    DOS MEMORY MANAGEMENT

    The memory manager of DOS has a relatively simple job – it’s managing a single job for a single user. To run a second process, the user must pause or close the first one.

    Originally used first fit method for early versions. Most efficient for single user environment.

    First one megabyte of memory would be:

    Reserved for BIOS    (1M)
    Unused
    -------------------------    (640K)
    Transient part of
    Command.com
    -------------------------
    User Memory    (transient program area, TPA)
    -------------------------
    TSR programs
    -------------------------
    Resident part of
    Command.com
    -------------------------
    Installable drivers
    ------------------------
    Buffer cache
    ------------------------
    MS-DOS kernel
    ------------------------
    BIOS interface
    ------------------------
    Interrupt vector
    ------------------------   (0M)

    If the user needed more space, can use transient portion of command.com. Is re-loaded after process is complete. The transient portion is where commands that can’t be executed while a program is running reside.

    First version of DOS gave all the memory to the resident application, but didn’t allow applications to dynamically allocate and deallocate memory blocks. Version 2 supported dynamic allocation, modification and release of main memory blocks by the application.

    Programs with COM extension given all TPA.

    Programs with EXE extension given only amount of memory then need. These files have a header indicating minimum and maximum amount of memory to run. Ideally, DOS gives maximum, but if not possible, tries to satisfy the minimum. If not enough for minimum, program won’t run.

    Except for COM files there can be any number of files in the TPA at one time, even though DOS can only run one at a time. By having several files in memory at once, the user can open one and work on it and close it before starting on the next. User can use two or more programs quickly and easily. E.g. a word processing package may allow user to display two files on the screen as split screen. One active, the other dormant. Switch to dormant screen indicates that other file should become active. A spellchecker is an example of a process that becomes active as your word processor becomes dormant.

    If a program when running should need more memory, memory manager checks to see whether there is enough memory left. If there is, will allocate it and update status of memory blocks. If not, will report error to the user.

    The initial DOS memory manager allocated memory by using a first fit algorithm and a linked list of memory blocks. With Version 3 and above, best-fit or last-fit can be selected. In last-fit, DOS allocates the highest addressable memroy block big enough.

    Size of the block can vary from 16 bytes (called a paragraph) to maximum available memory. When a block is formed, first five bytes contain following info:

    • Byte 0 : 90h if it’s the last block, 77h if it is not
    • Bytes 1-2: includes 0 to indicate busy block and pointer to the Program Segment Prefix that is created when the program is loaded.
    • Bytes 3-4: Gives the number of paragraphs contained in the block.

    If a block contains four paragraphs and is the first of two blocks its code would be :

         7700000004h

    When memory request arrives, DOS looks through the free/busy list, until it finds a free block that’s fits the request. . If the link list of blocks becomes disconnected, computer must be re-booted. Well designed application program releases memory blocks it no longer needs. It is merged back into list. Badly designed program will hoard its memory blocks until it stops running. Writing overlays in DOS allows other portions of an application to be put into the freed up spaces.

    Why 640K?

    Microprocessors work with memory chips via address lines, which are physical connections between the processor and the memory locations (each of which stores a single byte of memory). Each address line carries a single bit of a memory address, so the total amount of memory – the address space – any microprocessor can work with is limited to the number of address lines. A single address line can only address two bytes (0 and 1), whereas a two-line situation there are four addresses (00,01,10,11). Intel’s 8088 processor has 20 address lines, and this gives an address space of 1,048,576 bytes.) or I Mb. 286 has 24 address lines and can access up to 16MB, and the 386 an address space of 4 GB, because it has 32 lines.

    However, if the 8088 can address 1Mbwhy was DOS restricted to 640Kb? Well – IBM PC engineers, in their not so infinite wisdom, decided to reserve the last 384K for hardware uses (video buffers, ROM BIOS and so on) . Called upper memory area





All content © 2000-2001 Ann Logan - Web Site © 2000-2001 Carol Busato
Please address your comments and inquiries to lbusato@videotron.ca

Last updated April 7, 2001