View on GitHub

Notes

reference notes

Memory Management Requirement

Memory management is intended to satisfy the following requirements:

Relocation

Programmers typically do not know in advance which other programs will be resident in main memory at the time of execution of their program

Active processes need to be able to be swapped in and out of main memory in order to maximize processor utilization

Specifying that a process must be placed in the same memory region when it is swapped back in would be limiting

These facts raise some technical concerns related to addressing, as illustrated in Figure 7.1. The figure depicts a process image.

Figure 7.1

The OS will need to know the location of:

Because the operating system is managing memory and is responsible for bringing this process into main memory, these addresses are easy to come by.

However, the processor need to deal with memory references within the program.

So the processor and OS must be able to translate the memory references found in the code of the program into actual physical memory address

Protection

Processes need to acquire permission to reference memory locations for reading or writing purposes

Location of a program in main memory is unpredictable

Memory references generated by a process must be checked at run time

Mechanisms that support relocation also support protection

Memory protection must be satisfied by the processor rather than OS. why?

Sharing

Advantageous to allow each process access to the same copy of the program rather than have their own separate copy

Memory management must allow controlled access to shared areas of memory without compromising protection

Mechanisms used to support relocation support sharing capabilities

Logical Organization

Almost invariably, main memory in a computer system is organized as a linear or one-dimensional address space, consisting of a sequence of bytes or words. Secondary memory, at its physical level, is similarly organized. While this organization closely mirrors the actual machine hardware, it does not correspond to the way in which program are typically constructed.

Most programs are organized into modules, some of which are unmodifiable (read only, execute only) and some of which contain data that may be modified. If the operating system and computer hardware can effectively deal with user programs and data in the form of modules of some sort, then a number of advantages can be realized:

Segmentation is the tool that most readily satisfies these requirements.

Physical Organization

Computer memory is organized into at least two levels: Main memory | Secondary memory —|— Fast access at high cost | Slower and cheaper Volatile | Not volatile Small capacity holds programs and data currently in use | Large capacity for long term storage of programs and data

The organization of the flow of information between main and secondary memory is a major system concern.

Cannot leave the programmer with the responsibility to manage memory, because

Memory Management

Principal operation of memory management is to bring programs into memory for execution by the processor.

In most modern multiprogramming system it invokes virtual memory that use both segmentation and paging techniques.

Simpler technique that do not use virtual memory:

Partitioning

The simplest scheme for managing the available memory is to partition it into regions with fixed boundaries.

Two types of partitioning:

Fixed Partitioning

Equal Size Partition

Divide the memory into same size partition

Any process whose size is less than or equal to the partition size can be loaded into an available partition

The operating system can swap out a process if all partitions are full and no process in the Ready or Running state

Equal Size Partition

  1. A program may not fit in a partition.
    • The programmer must design the program with “overlays”
      • Program and data are organized in such away that various modules can be assigned the same region of memory with a main program responsible for switching in and out as needed
  2. Memory use is inefficient.
    • Any program, no matter how small, occupies an entire partition. This is called internal fragmentation. (If the memory allocated to the process is slightly larger than the memory demanded, then the difference between allocated and demanded memory is known as internal fragmentation.)
      • E.g. Program length 2MB, it occupies an 8MB partition. So 6MB is internal fragmentation.

Unequal Size Partition

Divide the memory into different partition size.

Using unequal size partitions helps lessen the problems

Unequal Size Partition

Placement Algorithm

Where to put / load process in memory.

Equal-size partitions:

Unequal-size partitions:

Use of multiple queues

Multiple Queues

Use of single queue

Single Queue

Advantages and Disadvantages

Advantages Disadvantages
Simple and require minimal OS software and processing overhead The number of partitions specified at system generation time limits the number of active (not suspended) processes in the system.
Unequal-size partition provides a degree of flexibility to fixed partitioning Because partition sizes are preset at system generation time, small jobs will not utilize partition space efficiently.

The used of fixed partitioning is almost unknown today.

Example of OS that use this technique was an early IBM mainframe OS, OS/MFT (Multiprogramming with a Fixed Number of Tasks).

Dynamic Partitioning

Partitions are of variable length and number

Each process is allocated exactly as much memory as it requires.

Used in IBM’s OS/MVT (Multiprogramming with a Variable number of Tasks)

An example using 64 MB of memory is shown in Figure 7.4.

Dynamic Partitioning

Eventually holes are formed in the memory.

Memory will become more and more fragmented, and memory utilization declines.

This hole is called external fragmentation.

Technique to overcome external fragmentation is called compaction.

From figure 7.4h, compaction will result in a block of free memory of length 16M, and this maybe sufficient to load an additional process.

Placement Algorithm

Where to put / load process in memory.

Best-fit First-fit Next-fit
Choose the block that is closest in size to the request Begins to scan memory from the beginning and chooses the first available block that is large enough Begins to scan memory from the location of the last placement and chooses the next available block that is large enough
Choose smallest hole Choose first hole from beginning Choose first hole from last placement

Placement Algorithm

Buddy System

Both fixed and dynamic partitioning schemes have drawbacks.

An interesting compromise is the buddy system.

Space available for allocation is treated as a single block

Memory blocks are available of size 2K words, L ≤ K ≤ U, where: 2^L = smallest size block that is allocated 2^U = larger size block that is allocated; generally 2^u is the size of the entire memory available for allocation.

Buddy System

Example shows in Figure 7.6 using 1MB initial block.

The first request, A, is for 100kb is needed.

The next request B, requires 256K block. Such block is already available and is allocated.

The process continues with splitting and combine back into 256K, which immediately combine back with its buddy.

Figure 7.7 shows a binary tree representation of the buddy allocation immediately after the release B request.

The leaf nodes represent the current partitioning of the memory.

If two buddies are leaf nodes, then at least one must be allocated, otherwise they would be combine into a larger block.

Relocation

When program loaded into memory the actual (physical / absolute address ) memory locations are determined

A process may occupy different partitions which means different physical memory locations during execution

Compaction will also cause a program to occupy a different partition which means different physical memory locations

Thus the locations of instruction and data referenced by a process are not fixed.

Types of addresses

Programs that employ relative addresses in memory used dynamic run-time loading.

It means all of the memory references in the loaded process are relative to the origin of the program.

Hardware mechanism is needed for translating relative addressed to physical memory addresses at the time of execution of the instruction that contain the reference.

Figure 7.8 shows the way in which this address translation is dynamically accomplished.

Relocation

When a process is assigned to the Running state:

These values must be set when the program is loaded into memory or when the process image is swapped in.

During execution of the process, relative address are encountered.

Each relative address goes through two steps of manipulation by the processor:

  1. The value in the base register is added to the relative address to produce an absolute address.
    • Address translation
  2. The resulting address is compared to the value in the bound register
    • Check either address valid or not
    • If the address within bounds, then the instruction execution may proceed, otherwise an interrupt is generated to the OS which must respond to the error.

The scheme in Figure 7.8 allows programs to be swapped in and out of memory during execution.

It also provide a measure of protection:

Paging

Partition memory into equal fixed-size

Process is also divided into equal fixedsize chunks of the same size as memory

Paging

OS maintains a page table for each process.

Paging

Simple paging similar to fixed partitioning but the different are:

Within the program, each logical address consists of a page number and an offset within the page.

Logical to physical address translation is done by processor and the processor must know how to access the page table of current process.

Presented with a logical address (page #, offset), the processor uses the page table to produce a physical address (frame #, offset).

Address Translation

The following steps are needed for address translation:

  1. Extract the page number as the leftmost n bits of the logical address.
  2. Use the page number as an index into the process page table to find the frame number k.
  3. The starting physical address of the frame is k X 2m, and the physical address of the referenced byte is that number plus the offset. This physical address need not be calculated; it is easily constructed by appending the frame number to the offset.

Consider 16bit addresses are used, and the page size is 1Kb

Paging

This page 1 is residing in memory frame 6 = binary 000110.

Summary

Memory is divided into small equal-size frames.

Each process is divided into frame-size pages

Smaller processes require fewer pages, larger processes require more.

When a process is brought in, all of its pages are loaded into available frames, and a page table is set up.

This approach solves many problems inherent in partitioning.

Segmentation

A program can be subdivided into segments

Segments vary in length

Have the maximum segment

Addresses consists of:

Similar to dynamic partitioning the different is that with segmentation a program may occupy more than one partition and these partitions does not need to be contiguous.

Eliminates internal fragmentation but external fragmentation still exist

Paging is invisible for the programmer but segmentation is visible and is provided as a convenience for organizing programs and data.

Address Translation

No simple relationship between logical addresses and physical addresses (like paging).

Every process will have a segment table

Analogous form paging, a simple segmentation scheme would make use of segment table for each process and a list of free blocks of memory.

Each segment table entry would have to give the starting address in memory of the corresponding segment.

The entry should also provide the length of the segment, to assure that invalid addresses are not used.

When process enters the Running state, the address of its segment table is loaded into a special register used by the memory management hardware.

Every process will have a segment table that consists of:

Length will be used to assure that invalid addresses are not used.

Consider 16bit addresses, maximum number of segments is 16.

Logical Address = 0001001011110000

Segment Number used 4 bits and Offset used 12 bits

The following steps are needed for address translation:

  1. Extract the segment number as the leftmost n bits of the logical address.
  2. Use the segment number as an index into the process segment table to find the starting physical address of the segment.
  3. Compare the offset, expressed in the rightmost m bits, to the length of the segment. If the offset greater than the length, the address is invalid.
  4. The desired physical address is the sum of the starting physical address of the segment plus the offset.

Segmentation

Summary

A process is divided into a number of segments which need not be equal size.

When a process brought in, all of its segments are loaded into available regions of memory, and a segment table is setup.