Top Banner
Operating System-Memory Operating System-Memory Management Management by by Vishal Singh Vishal Singh ([email protected]) ([email protected])
35

Memory management

Nov 20, 2014

Download

Education

Vishal Singh

This presentation is related to the Memory management part of the operating systems.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Memory management

Operating System-Memory Operating System-Memory ManagementManagement

by by Vishal SinghVishal Singh

([email protected])([email protected])

Page 2: Memory management

Why Memory Why Memory management ?management ?

1.1. To ensure protection of different To ensure protection of different processes from each other (so processes from each other (so that they do not interfere with that they do not interfere with each other’s operations).each other’s operations).

2.2. To place the programs in memory To place the programs in memory (such that memory is utilized to (such that memory is utilized to its fullest extent.its fullest extent.

Page 3: Memory management

Logical vs. physical address Logical vs. physical address spacespace

►A logical address is the address of an A logical address is the address of an instruction or data as used by a instruction or data as used by a program. program.

►A physical address is the effective A physical address is the effective memory address of an instruction or memory address of an instruction or data i.e. it is the address obtained after data i.e. it is the address obtained after binding of logical addresses has been binding of logical addresses has been done. done.

Page 4: Memory management

Program relocationProgram relocation

Page 5: Memory management

Relocation registerRelocation register

►Relocation register is a special register Relocation register is a special register in the CPU used for program relocation in the CPU used for program relocation means mapping of logical addresses means mapping of logical addresses used by the program to physical used by the program to physical addresses of the system’s main addresses of the system’s main memory memory

Page 6: Memory management

Storage Allocation and Storage Allocation and Management TechniquesManagement Techniques

The Storage allocation can be of two The Storage allocation can be of two types: types:

(i) Contiguous storage allocation.(i) Contiguous storage allocation.

(ii) Non-contiguous storage allocation. (ii) Non-contiguous storage allocation.

Page 7: Memory management

Contiguous Storage Contiguous Storage AllocationAllocation

►Contiguous storage allocation implies Contiguous storage allocation implies that a program’s data and instructions that a program’s data and instructions are assured to occupy a single are assured to occupy a single contiguous memory area. contiguous memory area.

► It is further subdivided into Fixed-It is further subdivided into Fixed-partition storage allocation strategy partition storage allocation strategy and variable-partition storage and variable-partition storage allocation strategy.allocation strategy.

Page 8: Memory management

1. Fixed-partition contiguous 1. Fixed-partition contiguous storage allocationstorage allocation

►processes with small address space processes with small address space use small partitions and processes use small partitions and processes with large address space use large with large address space use large partitions. This is known as fixed partitions. This is known as fixed partition contiguous storage allocation. partition contiguous storage allocation.

Page 9: Memory management
Page 10: Memory management

2. Variable - partition 2. Variable - partition contiguous storage contiguous storage

allocationallocation

This notion is derived from parking of This notion is derived from parking of vehicles on the sides of streets where the vehicles on the sides of streets where the one who manages to enter will get the space. one who manages to enter will get the space. Two vehicles can leave a space between Two vehicles can leave a space between them that cannot be used by any vehicle. them that cannot be used by any vehicle. This means that whenever a process needs This means that whenever a process needs memory, a search for the space needed by it, memory, a search for the space needed by it, is done. If contiguous space is available to is done. If contiguous space is available to accommodate that process, then the process accommodate that process, then the process is loaded into memory. is loaded into memory.

Page 11: Memory management

External FragmentationExternal Fragmentation

►This phenomenon of entering and This phenomenon of entering and leaving the memory can cause the leaving the memory can cause the formation of unusable memory holes formation of unusable memory holes (like the unused space between two (like the unused space between two vehicles). This is known as External vehicles). This is known as External Fragmentation. Fragmentation.

Page 12: Memory management

Three strategies that can be used Three strategies that can be used to allocate memory to Variable - to allocate memory to Variable -

partitionpartition They are the following:They are the following:

1.1. Best - FitBest - Fit

2.2. Worst - fitWorst - fit

3.3. First - fitFirst - fit

Page 13: Memory management

Best - Fit - Best - Fit - chooses a partition that is smallest and chooses a partition that is smallest and whose size is greater than equal to k. It whose size is greater than equal to k. It leaves small-sized unusable partitions or leaves small-sized unusable partitions or holes.holes.

Page 14: Memory management

First - fit - First - fit - chooses the first partition chooses the first partition whose size is greater than equal to whose size is greater than equal to k.k.

Page 15: Memory management

Worst - fit - Worst - fit - chooses the largest partition and chooses the largest partition and allocates it to process p. It can leave allocates it to process p. It can leave bigger unusable partitions.bigger unusable partitions.

Page 16: Memory management

CompactionCompaction means to move the processes means to move the processes in the memory in such a way that scattered in the memory in such a way that scattered pieces of unused (free) memory can be pieces of unused (free) memory can be placed together so that any other process placed together so that any other process demanding contiguous memory for it can demanding contiguous memory for it can

use it.use it.

Page 17: Memory management

Non-contiguous Storage Non-contiguous Storage AllocationAllocation

►To resolve the To resolve the problem of external problem of external fragmentationfragmentation and to and to enhance the enhance the degree of multiprogrammingdegree of multiprogramming to a to a greater extent, it was decided to greater extent, it was decided to sacrifice the simplicity of allocating sacrifice the simplicity of allocating contiguous memory to every process. It contiguous memory to every process. It was decided to have a non-contiguous was decided to have a non-contiguous physical address space of a process so physical address space of a process so that a process could be allocated that a process could be allocated memory wherever it was available. memory wherever it was available.

Page 18: Memory management

Non-contiguous Storage AllocationNon-contiguous Storage AllocationThere are 2 techniques for non-contiguous There are 2 techniques for non-contiguous allocation:allocation:1. Paging1. Paging2. Segmentation2. Segmentation

Page 19: Memory management

1. 1. PagingPaging ► In this approach, In this approach, physical memory physical memory

is divided into fixed-size blocks is divided into fixed-size blocks called framescalled frames and the and the logical logical memory is divided into the fixed-memory is divided into the fixed-sized blocks called pages. sized blocks called pages. The The size of a page is same as that of size of a page is same as that of frame. The key idea of this method frame. The key idea of this method is to place the pages of a process is to place the pages of a process into the available frames of memory, into the available frames of memory, whenever, this process is to be whenever, this process is to be executed. The address mapping is executed. The address mapping is done by Page table.done by Page table.

Page 20: Memory management

Paging example Paging example

Page 21: Memory management

It works as:It works as:

Translate logical address 13 = 01101 in Translate logical address 13 = 01101 in binarybinary

Offset = 01 in binary or 1 in decimal Offset = 01 in binary or 1 in decimal

Page no. = 011 in binary or 3 in decimalPage no. = 011 in binary or 3 in decimal

Physical address = pageframe*pagesize Physical address = pageframe*pagesize + offset i.e. 2*4+1 = 9 (which is the + offset i.e. 2*4+1 = 9 (which is the real address of page containing real address of page containing character “n” in the main memory)character “n” in the main memory)

Page 22: Memory management

Problem with pagingProblem with paging ► It uses a page table per process for It uses a page table per process for

translating logical to physical address translating logical to physical address space. A basic issue associated with space. A basic issue associated with the use of page table is, that the look the use of page table is, that the look up in the page table should be very up in the page table should be very fast (which is generally slow), as it is fast (which is generally slow), as it is done on every memory reference. done on every memory reference.

►So to make the look up time less, So to make the look up time less, caching is used. In this case, the caching is used. In this case, the specific cache device is called a specific cache device is called a translation look aside buffer (TLB). translation look aside buffer (TLB).

Page 23: Memory management

Translation look aside buffer Translation look aside buffer (TLB). (TLB).

►The TLB contains a set of entries, The TLB contains a set of entries, each of which contains a page each of which contains a page number, the corresponding frame number, the corresponding frame number, and the protection bits. number, and the protection bits.

►So if we use TLB, then mapping is So if we use TLB, then mapping is done by TLB and we generally find done by TLB and we generally find the entry in TLB but in case the entry the entry in TLB but in case the entry is not found in TLB than we have a is not found in TLB than we have a TLB miss and finally we go to page TLB miss and finally we go to page table.table.

Page 24: Memory management

SegmentationSegmentation ► segmentation is another technique for the segmentation is another technique for the

noncontiguous storage allocation. It is noncontiguous storage allocation. It is different from paging as it supports users’ different from paging as it supports users’ view of his program.view of his program.

► For a programmer it might be more For a programmer it might be more relevant to divide the logical address relevant to divide the logical address space of his program into variable sized space of his program into variable sized segments (with respect to his view of main segments (with respect to his view of main program, subroutines, data, etc.) than to program, subroutines, data, etc.) than to divide it into fixed size pages. Such divide it into fixed size pages. Such variable sized segments, which are a variable sized segments, which are a collection of logically related information, collection of logically related information, are the basis of segmentation technique. are the basis of segmentation technique.

Page 25: Memory management

Segmentation exampleSegmentation example

Page 26: Memory management

Virtual MemoryVirtual Memory

► Virtual memory is a technique of executing Virtual memory is a technique of executing program instructions that may not fit program instructions that may not fit entirely in system memory. This is done by entirely in system memory. This is done by calling instructions as and when the calling instructions as and when the application requires it. Virtual memory is application requires it. Virtual memory is implemented by using secondary storage to implemented by using secondary storage to augment the main memory. Data is augment the main memory. Data is transferred from secondary to main storage transferred from secondary to main storage as and when necessary and the data as and when necessary and the data modified is written back to the secondary modified is written back to the secondary storage according to a predetermined storage according to a predetermined algorithm. algorithm.

Page 27: Memory management

SwappingSwapping ►Swapping is the technique of Swapping is the technique of

temporarily removing inactive programs temporarily removing inactive programs from the main memory of a computer from the main memory of a computer system. An inactive program is one that system. An inactive program is one that is neither executing nor performing an is neither executing nor performing an I/O operation. I/O operation.

►This may also happen when it is desired This may also happen when it is desired to place a higher-priority process in the to place a higher-priority process in the memory. A lower priority process may memory. A lower priority process may be swapped out so that higher-priority be swapped out so that higher-priority process may be loaded and executed. process may be loaded and executed.

Page 28: Memory management
Page 29: Memory management

Demand PagingDemand Paging ► In virtual memory system, demand paging In virtual memory system, demand paging

is a type of swapping in which pages of is a type of swapping in which pages of programs are not copied from disk to main programs are not copied from disk to main memory until they are needed for memory until they are needed for execution.execution.

► In demand paging, virtual address (logical In demand paging, virtual address (logical address) is accessed by CPU, the address) is accessed by CPU, the corresponding page number is looked up corresponding page number is looked up in the page table and if it shows that in the page table and if it shows that currently this page is not in main memory, currently this page is not in main memory, then this page must be brought into the then this page must be brought into the main-memory. main-memory.

Page 30: Memory management

►A A page faultpage fault occurs when an invalid occurs when an invalid page is addressed. Page fault must be page is addressed. Page fault must be followed by swapping-in the page followed by swapping-in the page (demanded just now by the CPU) from (demanded just now by the CPU) from disk to main-memory or a trap should disk to main-memory or a trap should be generated to the operating system if be generated to the operating system if the page being demanded is not within the page being demanded is not within the logical address space of the process. the logical address space of the process. To determine whether the reference to To determine whether the reference to the requested page is within the logical the requested page is within the logical address space or not, an internal table address space or not, an internal table may be consulted may be consulted

Page 31: Memory management
Page 32: Memory management
Page 33: Memory management

Pure Demand PagingPure Demand Paging ►Pure demand paging is the form of Pure demand paging is the form of

demand paging in which demand paging in which not even a not even a single page is loaded into single page is loaded into memory, initially.memory, initially. Thus, the very Thus, the very first instruction causes a page fault first instruction causes a page fault in this case. This kind of demand in this case. This kind of demand paging may significantly decrease paging may significantly decrease the performance of a computer the performance of a computer system by generally increasing the system by generally increasing the effective access time of memory. effective access time of memory.

Page 34: Memory management

Page ReplacementPage Replacement ►Once the main memory fills up, a Once the main memory fills up, a

page must be swapped out to make page must be swapped out to make room for any pages to be swapped room for any pages to be swapped in. This is known as page in. This is known as page replacement. replacement.

►We have page replacement We have page replacement algorithms for page replacement algorithms for page replacement purpose, which are as follows:purpose, which are as follows:

Page 35: Memory management

1.1. The optimal page replacement The optimal page replacement algorithmalgorithm

2.2. The first-in, first-out (FIFO) page The first-in, first-out (FIFO) page replacement algorithmreplacement algorithm

3.3. The second chance page The second chance page replacement algorithmreplacement algorithm

4.4. The least recently used (LRU) page The least recently used (LRU) page replacement algorithmreplacement algorithm

5.5. Simulating LRU in SoftwareSimulating LRU in Software(Not (Not frequently used algorithm)frequently used algorithm)

6.6. The Working Set Page Replacement The Working Set Page Replacement AlgorithmAlgorithm