+ All Categories
Home > Documents > Memory Management

Memory Management

Date post: 12-Jan-2016
Category:
Upload: dakota
View: 43 times
Download: 0 times
Share this document with a friend
Description:
Memory Management. Chapter 7. Memory Management. In uniprogramming system, memory is divided into two parts, one part for the OS and one part for the program that currently being executed (user part). - PowerPoint PPT Presentation
Popular Tags:
79
CSNB224 – AAG 2007 Memory Management Chapter 7
Transcript
Page 1: Memory Management

CSNB224 – AAG 2007

Memory Management

Chapter 7

Page 2: Memory Management

CSNB224 – AAG 2007

Memory Management

In uniprogramming system, memory is divided into two parts, one part for the OS and one part for the program that currently being executed (user part).In multiprogramming system, the user part of the memory must be further subdivided to accommodate multiple processes.The task of subdivision is carried out dynamically by the OS memory management.

Page 3: Memory Management

CSNB224 – AAG 2007

Memory Management

Memory needs to be allocated efficiently to pack as many processes into memory as possible.This is because effective memory management is important in multiprogramming system.

Page 4: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

Five requirements of memory management:

1. Relocation

2. Protection

3. Sharing

4. Logical Organization

5. Physical Organization

Page 5: Memory Management

CSNB224 – AAG 2007

Memory Management RequirementsRelocation

Programmer does not know where the program will be placed in memory when it is executed

While the program is executing, it may be swapped to disk and returned to memory at a different location (relocate process at different area)

The location for the process is unknown and the process must be allowed to moved in and out due to swapping.

Page 6: Memory Management
Page 7: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

These facts raise some technical concerns related to addressing, as illustrated in Figure 7.1.Figure 7.1 shows a process image.The OS will need to know the location of:

process control informationthe execution stackthe entry point to begin the execution of the program for the process.

Because OS is managing memory and is responsible for bringing this process into memory, these addresses are easy to recognize.

Page 8: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

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

Branch inst. contain an address to reference the instruction to be executed next.Data reference inst. contain the address of the byte or word of data referenced.

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

reflecting the current location of the program in memory.

Page 9: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

ProtectionProcesses should not be able to reference memory locations in another process without permissionImpossible to check absolute addresses in programs since the program could be relocated.All memory references must be checked at run time to ensure that it refer to the only memory space allocated to the process

Page 10: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

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

OS cannot anticipate all of the memory references a program will make Time consuming to screen each program in advance for possible memory referenced violation.It is only possible to assess the permissibility of a memory reference at the time of execution of the instruction making reference.

Page 11: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

SharingAllow several processes to access the same portion of memory.

So, any protection mechanism must have the flexibility, example:

If a number of processes are executing the same program, it is advantageous to allow each process access to the same copy of the program rather than have their own separate copy e.g. ECHO procedure

Page 12: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

Memory management system must therefore allow controlled access to shared areas of memory without compromising essential protection.

Page 13: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

Logical OrganizationPrograms are written in modules

Modules can be written and compiled independently

Different degrees of protection given to modules (read-only, execute-only)

Share modules

##Read on your own

Page 14: Memory Management

CSNB224 – AAG 2007

Memory Management Requirements

Physical OrganizationMemory available for a program plus its data may be insufficient

Overlaying allows various modules to be assigned the same region of memory

Programmer does not know how much space will be available

##Read on your own

Page 15: Memory Management

CSNB224 – AAG 2007

Memory Partitioning

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 (VM) that use both segmentation and paging techniques.First look at simpler technique that do not use VM PartitioningTwo types of partitioning:

1. Fixed Partitioning2. Dynamic Partitioning

Page 16: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning

Most schemes of memory management, assume that the OS occupies some fixed partition of memory and the rest of memory is available for use by multiple processes.The simplest scheme for managing this available memory is to partition it into regions with fixed boundaries.Figure 7.2 shows example of two alternatives for fixed partitioning:

1. Equal-size partition2. Unequal-size partition

Page 17: Memory Management
Page 18: Memory Management

CSNB224 – AAG 2007

Fixed PartitioningEqual-size partition (Figure 7.2a)

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

if all partitions are full, the operating system can swap a process out of a partition

Page 19: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning

Two difficulties: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

Memory use is inefficient. Any program, no matter how small, occupies an entire partition. This is called internal fragmentation.

– Eg: Program length 2MB, it occupies an 8MB partition. So 6MB is internal fragmentation.

Page 20: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning

Unequal-size partition (Figure 7.2b)A program as large as 16MB can be accommodate without need to overlays it.

Partition smaller than 8MBallow smaller program to be accommodate with less internal fragmentation.

Page 21: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning - Placement Algorithm

Where to put / load process in memory.Equal-size partitions

If there is an available partition, a process can be loaded into that partition

because all partitions are of equal size, it does not matter which partition is used

If all partitions are occupied by blocked processes, choose one process to swap out to make room for the new process

Page 22: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning - Placement Algorithm

Unequal-size partitions: Two ways:

1. Use of multiple queuesAssign each process to the smallest partition within which it will fit

A queue for each partition size

Tries to minimize internal fragmentation

Problem: some queues will be empty if no processes within a size range is present

Page 23: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning - Placement Algorithm

Page 24: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning - Placement Algorithm

2. Use of a single queueWhen its time to load a process into memory the smallest available partition that will hold the process is selected

If all partitions is occupied:Preference to swapping out the smallest partition that will hold the incoming process.

Also have to consider other factors such as priority and blocked vs. ready process.

Increases the level of multiprogramming at the expense of internal fragmentation

Page 25: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning - Placement Algorithm

Page 26: Memory Management

CSNB224 – AAG 2007

Fixed PartitioningThe used of unequal-size partition provides a degree of flexibility to fixed partitioning.Fixed partitioning schemes are relatively simple and require minimal OS software and processing overhead.But it still have disadvantages:

1. The number of partitions specified at system generation time limits the number of active (not suspended) processes in the system.

2. Because partition sizes are preset at system generation time, small jobs will not utilize partition space efficiently.

Page 27: Memory Management

CSNB224 – AAG 2007

Fixed Partitioning

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.

Page 28: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning

Partitions are of variable length and numberEach 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 1 MB (1024KB) of memory is shown in Figure 7.4.

Page 29: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning

Page 30: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning

Initially memory is empty, except for the OS(refer figure a)The first three process are loaded in, starting where the OS ends and occupying just enough space for each process (refer figure b,c,d).This leaves a “hole” at the end of memory that is too small for a fourth process.

Page 31: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning

Page 32: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning

At some point none of the processes in memory is ready. OS swaps process 2 (figure e), which leaves sufficient room to load a new process, process 4 (figure f). Because process 4 is smaller than process 2, another small hole is created.Later, a point is reached at which none of the processes in memory is ready, but process 2, in the Ready-Suspend state is available.So OS swaps process 1 out (figure g) and swaps process 2 back in (figure h).

Page 33: Memory Management

CSNB224 – AAG 2007

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.

Referring to fact that the memory is external to all partitions becomes increasingly fragmented.

Technique to overcome external fragmentation is called compaction.

Time to time OS shifts the processes so that they are contiguous and so that all of free memory is together in one block.

Page 34: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning

Example in figure 7.4h, compaction will result in a block of free memory of length 256K, and this maybe sufficient to load an additional process.Difficulties of compaction is time consuming procedure and wasteful of processor time.

Page 35: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning - Placement Algorithm

Where to put / load process in memory.OS must decide which free block to allocate to a process.3 placement algorithms that might be considered:

1. Best Fit2. First Fit3. Next Fit

Page 36: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning - Placement Algorithm1. Best Fit

Chooses the block that is closest in size to the request.

Chooses smallest hole 2. First-fit:

Begins to scan memory from the beginning and chooses the first available block that is large enough.

Chooses first hole from beginning

Page 37: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning - Placement Algorithm3. Next-fit:

Begins to scan memory from the last placement and chooses the next available block that is large enough.

Chooses first hole from last placement

Page 38: Memory Management
Page 39: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning - Placement Algorithm

Which of these algorithms is best will depend on the exact sequence of process swapping that occurs and the size of those processes.Remarks for each of the algorithms:

1. First FitThe simplest and usually the best and fastest.

2. Next-FitTends to produce slightly worse result than first fit.More frequently lead to an allocation from a free block at the end of memoryCompaction maybe required more frequently with next fit

Page 40: Memory Management

CSNB224 – AAG 2007

Dynamic Partitioning - Placement Algorithm

3. Best FitWorse performer, because this algorithm will looks for the smallest block that will satisfy the requirement, it guarantees that the fragment left behind is small as possible.The memory is quickly littered by blocks too small to satisfy memory allocation requests.Compaction must be done more frequently than the other two algorithms.

Page 41: Memory Management

CSNB224 – AAG 2007

Replacement AlgorithmWhen all processes in memory are blocked, the OS must choose which process to replace

A process must be swapped out (to a Blocked-Suspend state) and be replaced by a new process or a process from the Ready-Suspend queue

We will discuss later such algorithms for memory management schemes using virtual memory

Page 42: Memory Management

CSNB224 – AAG 2007

Buddy System

Both fixed and dynamic partitioning schemes have drawbacks.A fixed partitioning scheme limits the number of active processes and may used space inefficiently if there is a poor match between available partition sizes and process sizes.A dynamic partitioning scheme is more complex to maintain and includes the overhead of compaction.An interesting compromise is the buddy system.

Page 43: Memory Management

CSNB224 – AAG 2007

Buddy System

Memory blocks are available of size 2K , L<=K<=U, where: 2L = smallest size block that is allocated 2U = largest size block that is allocated, generally 2U is

the size of the entire memory available for allocation.

Begin with entire space available is treated as a single block of 2U

If a request of size s such that 2U-1 < s <= 2U, entire block is allocated

Otherwise block is split into two equal buddiesProcess continues until smallest block greater than or equal to s is generated

Page 44: Memory Management

CSNB224 – AAG 2007

Buddy System

At any time, the buddy system maintains a list of holes (unallocated blocks) of each size 2 i .A hole may be removed from the (i+1) list by splitting it in half to create two buddies of size 2i in the i list.Whenever a pair of buddies on the i list both become unallocated, they are removed from that list and combine back into a single block on the (i+1) list.

Page 45: Memory Management

CSNB224 – AAG 2007

Buddy SystemExample shows in Figure 7.6 using 1MB initial block.

The first request, A, is for 100kb is needed. The initial block is divided into two 512k buddies.

The first of these is divided into two 256K buddies, and the first these is divided into two 128K buddies, one of which is allocated to A.

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.

Page 46: Memory Management
Page 47: Memory Management

CSNB224 – AAG 2007

Buddy System

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.

Page 48: Memory Management
Page 49: Memory Management

CSNB224 – AAG 2007

Buddy System

The buddy system is a reasonable compromise to overcome the disadvantages of both partitioning schemes.In contemporary OS, VM based on paging and segmentation is superior.This buddy system has found application in parallel systems as an efficient means of allocation and release for parallel programs.A modified form of buddy system is used for UNIX kernel memory allocation.

Page 50: Memory Management

CSNB224 – AAG 2007

Relocation

When program loaded into memory the actual (absolute) memory locations are determinedA process may occupy different partitions which means different absolute memory locations during execution (from swapping)Compaction will also cause a program to occupy a different partition which means different absolute memory locationsThus the locations (instruction and data) referenced by a process are not fixed.

Page 51: Memory Management

CSNB224 – AAG 2007

Relocation

To solve this problem, a distinction is made among several types of addresses:

1. Logical Addressreference to a memory location independent of the current assignment of data to memory

translation must be made to the physical address2. Relative Address

address expressed as a location relative to some known point

3. Physical Address / Absolute Addressthe absolute address or actual location in memory

Page 52: Memory Management

CSNB224 – AAG 2007

Relocation

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.

Page 53: Memory Management
Page 54: Memory Management

CSNB224 – AAG 2007

Relocation

When a process is assigned to the Running state:

Base register - loaded with the starting address in memory of the process.“bounds” register loaded with the address of the ending location of the process

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.

Page 55: Memory Management

CSNB224 – AAG 2007

Relocation

These include the contents of the instruction register, instruction addresses that occur in branch and call instruction, and addresses that occur in load and store instruction.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

Page 56: Memory Management

CSNB224 – AAG 2007

Relocation2. The resulting address is compared to the

value in the bound register– 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.

Check either address valid or not

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

Page 57: Memory Management

CSNB224 – AAG 2007

RelocationIt also provide a measure of protection:

Each process image is isolated by the contents of the base and bounds registers and safe from unwanted accesses by other processes.

Page 58: Memory Management

CSNB224 – AAG 2007

Partition memory into small equal-size chunks and divide each process into the same size chunksThe chunks of a process are called pages and chunks of memory are called frames or page framesFigure 7.9 illustrates the use of pages and frames.Some of the frames in memory are in use and some are free and list of free frames is maintained by the OS.

Paging

Page 59: Memory Management
Page 60: Memory Management
Page 61: Memory Management

CSNB224 – AAG 2007

PA stored on disk, consists of four pages.When it comes to load PA, the OS finds four free frames and loads the four pages of PA into the four frames (Fig 7.9b).PB consisting of three pages and PC consisting of four pages are subsequently loaded. (Fig 7.9c,d).The PB is suspended and is swapped out of memory.

Paging

Page 62: Memory Management

CSNB224 – AAG 2007

Later all of the processes in memory are blocked, and the OS needs to bring in anew process, PD that contains five pages.From the figure (Fig7. 9e) we can see that there are no sufficient unused frames to hold the PD.Now the concept of logical address is used but with the absent of base registerOS maintains a page table for each process.

The page table shows the frames location of each page of the process.

Paging

Page 63: Memory Management

CSNB224 – AAG 2007

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).From figure 7.9d, five pages of PD are loaded in frames 4, 5 ,6 ,11 and 12.

Paging

Page 64: Memory Management

CSNB224 – AAG 2007

Figure 7.10 shows the various page tables.Page table entry contains the number of the frame in memory, if any, that holds the corresponding page.

OS also maintains a single free frame list of all frames in memory that are currently occupied and available pages.Simple paging similar to fixed partitioning but the different are:

In paging the partitions are rather smallProgram may occupy more than one partitions and no need to be contiguous.

Paging

Page 65: Memory Management

Page Tables for Example

Page 66: Memory Management

CSNB224 – AAG 2007

An example is shown in Figure 7.11In this example consider 16bit addresses are used, and the page size is 1K = 1024 bytes.The relative address 1502 in binary form is 0000010111011110.Offset need 10 bits (m, rightmost) and page number 6 bits (n, leftmost). So the program can consist a maximum 26 = 64 pages of 1Kb each.As figure 7.11b shows address 1502 (0000010111011110) correspond to an offset of 478(0111011110) and page number 1 (000001).

Paging

Page 67: Memory Management

CSNB224 – AAG 2007

Consider an address of n+m bits, where the leftmost n bits are the page number and the rightmost m bits are the offset. From the example n=6 and m=10.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.

Paging

Page 68: Memory Management

CSNB224 – AAG 2007

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.

From our example the logical address is 0000010111011110, which page number 1 and offset 478.

Paging

Page 69: Memory Management

CSNB224 – AAG 2007

Suppose that this page is residing in memory frame 6 = binary 000110. Then the physical address is frame number 6, offset 478 = 0001100111011110 (Fig 7.12a).

Paging

Page 70: Memory Management

CSNB224 – AAG 2007

Summarize for simple paging: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.

Paging

Page 71: Memory Management

CSNB224 – AAG 2007

Segmentation

Alternative way to subdivided user program into segments.All segments of all programs do not have to be of the same length, but there is a maximum length.Addressing consist of two parts - a segment number and an offsetSince segments are not equal, segmentation is similar to dynamic partitioning the different is that with segmentation a program may occupy more than one partition and these partitions need not be contiguous.

Page 72: Memory Management

CSNB224 – AAG 2007

Segmentation

Segmentation eliminates internal fragmentation but suffer from external fragmentation.However because a process is broken up into a number of smaller pieces, the external fragmentation should be less.Paging is invisible for the programmer but segmentation is visible and is provided as a convenience for organizing programs and data.

Page 73: Memory Management

CSNB224 – AAG 2007

Segmentation

Programmer or compiler will assign programs and data to different segmentsFor modular programming, the program or data may be further broken down into multiple segments

Page 74: Memory Management

CSNB224 – AAG 2007

Segmentation

The principal inconvenience is that the programmer must be aware of the maximum segment size limitation.For unequal size segments there is no simple relationship between logical addresses and physical addresses.Analogous form paging, a simple segmentation scheme would make use of segment table for each process and a list of free blocks of memory.

Page 75: Memory Management

CSNB224 – AAG 2007

Segmentation

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.

Page 76: Memory Management

CSNB224 – AAG 2007

Segmentation

Consider and address of n + m bits, where the leftmost n bits are segment number and the rightmost m bits are the offset. In our example (Fig. 7.11c), n=4 and m=12. Thus the maximum segment size is 212 = 4096.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.

Page 77: Memory Management

CSNB224 – AAG 2007

Segmentation

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.

Example the logical address 0001001011110000, which is segment number 1 (0001) and offset 752 (001011110000).

Page 78: Memory Management

CSNB224 – AAG 2007

Segmentation

Suppose that this segment is residing in memory starting at physical address 0010000000100000.Then the physical address is (Fig 7.12b) 010000000100000 + 001011110000 = 0010001100010000.

Page 79: Memory Management

CSNB224 – AAG 2007

Segmentation

Summarization for simple segmentation: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.


Recommended