+ All Categories
Home > Documents > Memory management

Memory management

Date post: 12-Jan-2016
Category:
Upload: ayasha
View: 93 times
Download: 0 times
Share this document with a friend
Description:
Memory management. Ref: Stallings G.Anuradha. What is memory management?. The task of subdivision of user portion of memory to accommodate multiple processes is carried out dynamically by the operating system and is known as memory management Memory management terms. - PowerPoint PPT Presentation
Popular Tags:
52
Memory management Ref: Stallings G.Anuradha
Transcript
Page 1: Memory management

Memory management

Ref: StallingsG.Anuradha

Page 2: Memory management

What is memory management?

• The task of subdivision of user portion of memory to accommodate multiple processes is carried out dynamically by the operating system and is known as memory management

• Memory management terms

Page 3: Memory management

Memory management requirements

Page 4: Memory management

Memory management requirements – Contd…

• Relocation– Users generally don’t know where they will be placed in main

memory– May want to swap in at a different place– Must deal with user pointers– Generally handled by hardware

• Protection– Prevent processes from interfering with the O.S. or other

processes– Often integrated with relocation

Page 5: Memory management

Memory management requirements – Contd…

– Sharing• Allow processes to share data/programs

– Logical Organization• Main memory and secondary memory are organized into

linear/1D address space of segments and words. • Secondary memory is also similarly organized• Most programs are modularized• Advantages of modular approach

– Written and compiled independently– Can have different degrees of protection– Module level of sharing

– Physical Organization• Transferring data in and out of main memory to secondary

memory can’t be assigned to programmers• Manage memory disk transfers (System responsibility)

Segmentation

Page 6: Memory management

Memory Partitioning

• Fixed partitioning• Dynamic partitioning• Simple Paging• Simple segmentation• Virtual memory paging• Virtual memory segmentation

Page 7: Memory management

Fixed partitioning

Page 8: Memory management
Page 9: Memory management

Difficulties in equal size fixed partitions

• A program may be too big to fit into a partition. In such cases overlays can be used

• Main memory utilization is extremely inefficient.

• Leads to internal fragmentation– BLOCK OF DATA LOADED IS SMALLER THAN THE

PARTITION

Page 10: Memory management

Placement algorithm

• With equal size partition a process can be loaded into a partition as long as there is an available partition

• With unequal size partitions there are two possible ways to assign processes to partitions– One process queue per partition– Single partition

Page 11: Memory management

Memory assignment for fixed partitioning

Page 12: Memory management

Advantages and disadvantages of each of the approaches

• One process queue per partition– Advantages:

• Internal fragmentation is reduced– Disadvantages

• Not optimal from the system point of view• Single queue– Advantages

• Degree of flexibility, simple, requires minimal OS S/w and processing overhead

– Disadvantages• Limits the number of active processes in the system• Small jobs will not utilize partition space efficiently

IBM Mainframe OS.OS/MFT

Page 13: Memory management

Dynamic partitioning

Create partitions as programs loadedAvoids internal fragmentation, but must deal with external fragmentation

Page 14: Memory management

Effect of dynamic partitioning

Page 15: Memory management

Dynamic partitioning

• External fragmentation:-As time goes on more and more fragments are added and the effective utilization declines

• External fragmentation can be overcome using compaction

• Compaction– Time consuming

Page 16: Memory management

Placement algorithm

• Best-fit:- Chooses the block that is closest in size to the request

• First-fit:- scans memory from the beginning and chooses the first available block that is large enough

• Next-fit:-Scan memory from the location of the last placement and chooses the next available block that is large enough

Page 17: Memory management
Page 18: Memory management

Which amongst them is the best?

• First fit:-– Simple, best and fastest

• Next fit:-– Next to first fit. Requires compaction in this case

• Best fit:-– Worst performer. Memory compaction should be

done as frequently as possible

Page 19: Memory management

Buddy system

• Overcomes the drawbacks of both fixed and dynamic partitioning schemes

Page 20: Memory management

Algorithm of buddy system

• The entire space available for allocation is treated as a single block of size 2U

• If a request of size s ST is made then the entire block is allocated

• Otherwise the block is split into two of size 2U-

1

• This process continues until the smallest block greater than or equal to s is generated and allocated to the request

Page 21: Memory management

Example of a buddy system

Page 22: Memory management

Free representation of buddy systemModified version used in

UNIX kernel memory allocation

Page 23: Memory management

RelocationNot a major problem with fixed-sized partitions

Easy to load process back into the same partition

Otherwise need to deal with a process loaded into a new locationMemory addresses may change

When loaded into a new partitionIf compaction is used

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

Page 24: Memory management

Relocation• Different types of address– Logical address:- reference to a memory location

independent of the current assignment of data to memory

– Relative address:- example of logical address in which the address is expressed as a location relative to some known point

– Physical address:- actual location in main memoryA hardware mechanism is needed for translating the relative addresses to physical main memory addresses at the time of execution of the instruction that contain the reference

Page 25: Memory management

Hardware support for relocation

Page 26: Memory management

Base/Bounds Relocation• Base Register– Holds beginning physical address– Add to all program addresses

• Bounds Register– Used to detect accesses beyond the end of the

allocated memory– Provides protection to system

• Easy to move programs in memory– Change base/bounds registers

• Largely replaced by paging

Page 27: Memory management

Paging

• Problems with unequal fixed-size and variable-size partitions are external and internal fragmentations respectively.

• If the process is also divided into chunks of same size - pages

• Memory is divided into chunks called frames• Then a page can be framed into a page frame• Then there will be only internal fragmentation

especially in the last page of the process

Page 28: Memory management

Assignment of Process pages to free frames

Page 29: Memory management

Page tables

• When a new process D is brought in, it can still be loaded even though there is no contiguous memory location to store the process.

• For this the OS maintains a page table for each process

• The page table shows the frame location for each page of the process

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

Page 30: Memory management

Page table contd…

• In a simple partition, a logical address is the location of a word relative to the beginning of the program

• The processor translates it into physical address.

• With paging the logical-physical address translation is done by hardware

processor

Logical address{page number, offset}

Physical address{Frame number, offset}

Page 31: Memory management

Data structures when process D is stored in main memory

Paging similar to fixed size partitioningDifference:- 1. partitions are small

2. Program may occupy more than one partition 3. Partitions need not be contiguous

Page 32: Memory management

Computation of logical and physical addresses

• Page size typically a power of 2 to simplify the paging hardware– Example (16-bit address, 1K pages)• Relative address of 1502 is • Top 6 bits (000001)= page # {Page number is 1}• Bottom 10 bits (0111011110) = {offset within page, in

this case 478}

• Thus a program can consist of a maximum of 26 = 64 pages of 1K bytes each.

Page 33: Memory management
Page 34: Memory management

Why page size is in multiples of 2?

• The logical addressing scheme is transparent to programmer, assembler, linker.

• Easy to implement a function in hardware to perform dynamic address translation at runtime.

• Steps in address translation

Page 35: Memory management

Logical to physical address translation using paging

Page 36: Memory management

Hardware support

• OS has its own method for storing page tables. Pointer to page table is stored with the other register values in the PCB, which is reloaded whenever the process is loaded.

• Page table may be stored in special registers if the number of pages is small.

• Page table may be stored in physical memory, and a special register, page-table base register, points to the page table.(Problem is time taken for accessing)

Page 37: Memory management

Implementation of Page Table• Page table is kept in main memory• Page-table base register (PTBR) points to the page table• Page-table length register (PRLR) indicates size of the page table• In this scheme every data/instruction access requires two

memory accesses. One for the page table and one for the data/instruction.

• The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs)

• Some TLBs store address-space identifiers (ASIDs) in each TLB entry – uniquely identifies each process to provide address-space protection for that process

Page 38: Memory management

Hardware support contd…

• Use translation look-aside buffer (TLB). TLB stores recently used pairs (page #, frame #).

• It compares the input page # against the stored ones. If a match is found, the corresponding frame # is the output. Thus, no physical memory access is required.

• The comparison is carried out in parallel and is fast.

• TLB normally has 64 to 1,024 entries.

Page 39: Memory management

Paging Hardware With TLB

Page 40: Memory management

Memory Protection• Memory protection implemented by associating protection bit with

each frame

• Valid-invalid bit attached to each entry in the page table:– “valid” indicates that the associated page is in the process’

logical address space, and is thus a legal page– “invalid” indicates that the page is not in the process’ logical

address space

Page 41: Memory management

Valid (v) or Invalid (i) Bit In A Page Table

Page 42: Memory management

Shared pages

• Possibility of sharing common code• This happens in the case if the code is re-

entrant code(pure code).• Re-entrant code is non-self modifying code: it

never changes during execution. Two or more processes can simultaneously utilize the same code.

Page 43: Memory management

Shared Pages Example

Page 44: Memory management

Summary

1. Main memory is divided into equal sized frames

2. Each process is divided into frame-sized pages

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

4. This approach solves the problems in partitioning

Page 45: Memory management

Segmentation

• Segmentation is a memory management scheme that supports user view of memory

• Logical address space is a collection of segments

• Each segment has a name and length• Each address has a segment and a offset

within a segment

Page 46: Memory management

User’s View of a Program

Page 47: Memory management

Logical View of Segmentation

1

3

2

4

1

4

2

3

user space physical memory space

Page 48: Memory management

Segmentation Hardware

Page 49: Memory management

Example of Segmentation

Segment 2 , ref to byte 53=4300+53=4353Segment 3, ref to byte 852=3200+852=4052Segment 0, ref to byte 1222= 6300+1222

Page 50: Memory management

Implementation of Segment Tables

• Just like page tables the segment tables can be kept in registers and accessed

• When the program contains a large number of segments a segment table base register and segment table limit register is kept in the memory and checks are performed

Page 51: Memory management

Protection and sharing

• Segments are always protected becos its semantically defined

• Code and data can be shared in segmentation. Segments are shared when entries in the segment tables of two different processes point to the same physical location

Page 52: Memory management

Recommended