Beruflich Dokumente
Kultur Dokumente
Memory Management
Chapter 4 BCT III/II
By: Shayak Raj Giri@IOE,Pulchowk Campus
Introduction
Programs are getting bigger faster than memories. The part of OS that manages the memory hierarchy is called memory manager.
Keeps track of used/unused part of memory. Allocate/de-allocate memory to processes. Manages swapping between main memory and disk.
Memory Hierarchy
.
Cache Memory
Main Memory
Secondary Memory
Memory Hierarchy
Ideally programmers want memory that is
large fast non volatile
Memory hierarchy
small amount of fast, expensive memory cache some medium-speed, medium price main memory gigabytes of slow, cheap disk storage
Monoprogramming Model
Only one program at a time in main memory; can use whole of the available memory.
Modeling Multiprogramming
(Probabilistic viewpoint of CPU usage) Let p=the fraction of time waiting for I/O to complete.
n= no. of processes in the memory at once. The probability that all n processes are waiting for I/O(CPU idle time) =pn So, CPU utilization = 1- pn
Modeling Multiprogramming
Degree of multiprogramming
Modeling Multiprogramming
Example Let, total memory = 1M =100K Memory space occupied by OS = 200K Memory space taken by an user program = 200K NOW
Number of processes n= (1000-200)/200 =4 [n= total user space/size of an user program] CPU utilization = 1- (0.8)4 =60%
Modeling Multiprogramming
Add another 1M memory, then
n= (2000-200)/200 =9 CPU utilization = 1- (0.8)9 =87% Improvement = (87-60)/60 = 45%
Job-C
Job-B A Job-D OS
OS
When a process finishes and leaves hole, the hole may not be large
enough to place new job. Thus, variable partition multiprogramming, waste does occur.
OS
Compaction
.
OS 20K hole Process-A
. . .
Drawbacks of Compaction
Reallocation info must be maintained. System must stop everything during compaction. Memory compaction requires lots of CPU time. For example: On a 256MB machine that can copy 4 bytes in 40nses, it takes 2.7sec to compact all of memory.
Swapping
If there is not enough main memory to hold all the currently active processes, the excess processes must be kept on the disk and brought in to run dynamically. Swapping consists of moving processes from main memory and disk. Relocation may be required during swapping.
Swapping, a picture
Can compact holes by copying programs into holes This takes too much time
2. Next fit It works the same way as first fit, except that it keeps track of where it is whenever it finds a suitable hole.
Advantages: more storage utilization than first fit. Disadvantages: slower than first fit because it requires searching whole list at time.
Advantages: some time it has more storage utilization than first fit and best fit. Disadvantages: not good for both performance and utilization.
5. Quick fit: Keeps list of common sizes requested.
Virtual Memory
Programs address space is broken up into fixed size pages Pages are mapped to physical memory If instruction refers to a page in memory, fine Otherwise OS gets the page, reads it in, and re-starts the instruction While page is being read in, another process gets the CPU
MMU maps virtual addresses to physical addresses and puts them on memory bus
16 bit addresses, 4 KB pages 32 KB physical memory, 16 virtual pages and 8 page frames
Example:
MOV REG,0 -> Virtual address o is sent to MMU.The MMU sees that this virtual address falls in page 0(0 to 4095), which is mapped to page frame 2(8192 to 12287). ->Thus it transforms the address to 8192 & outputs 8192 onto the bus. Similarly, MOV REG 8192 is effectively transformed into MOV REG, 24576.
Page Table
Virtual address={virtual page number, offset} Virtual page number used to index into page table to find page frame number If present/absent bit is set to 1, attach page frame number to the front of the offset, creating the physical address which is sent on the memory bus
Mapping/Paging Mechanism
Let us see how the incoming virtual address 8196(0010000000000100 in binary) is mapped to physical address 24580.
Incoming 16-bit virtual address is split into 4-bit page number and 12-bit offset. With 4-bits, we can have 16 pages and with 12-bits for the offset, we can address 212 =4096 bytes within a page. The page number is used as an index into the page table, gives number of virtual pages. Page table contains Present/Absent bit also.
Mapping Mechanism
Frame number: The actual page frame number. Present (1) / Absent (0) bit: Defines whether the virtual page is currently mapped or not. Protection bit: Kinds of access permission; read/write/execution. Modified bit: Defies the changed status of the page since last access. Referenced bit: Used for replacement strategy. Caching disabled: Used for the system where the mapping into device registers rather than memory.
(a) A 32-bit address with two page table fields. (b) Two-level page tables.
Pick the one which will not used before the longest time. Not possible unless know when pages will be referenced. Used as ideal reference algorithm. To obtain optimum performance, the page to replace is one that will not be used again for a long time. If one page will not be used for r8 million instructions and another page will not be used for 6 million instructions; choosing the former for replacement avoids the page fault for longer time. Difficult to realize.
Second Chance
A simple modification to FIFO. Avoids the problem of throwing out a heavily used page by inspecting the R bit. If it is 0, the page is both old and unused, so it is replaced immediately. If the R bit is 1, the bit is then cleared, the page is put onto the end of the pages, and load time is updated as though it is arrived in the memory.
Second Chance
In the following diagram, pages A-H are sorted in the list by their arrived in memory.
The Clock Page Replacement Algorithm Page frames are kept on circular list, to remove unnecessary movements that occurred in second chance. When page fault occurs, the page being pointed to by the hand is inspected. If its R bit is 0, then the page is removed, new page is inserted into the clock in its place and the hand is advanced one position. If R bit is 1, it is cleared and the hand is advanced to next page.
. In a one-dimensional address space with growing tables, one table may bump into another.
Segmentation
A segmented memory allows each table to grow or shrink independently of the other tables.
Segmentation
Same as variable partitioning and mechanisms with freedom of contiguous memory requirement restriction. Many completely independent address spaces, called segments. Different segments have different lengths any size from 0 to the maximum allowed. Segments are logical entity, which the programmer is aware of and uses as a single logical entity.
Advantages of Segmentation
Simplifies handling of data structures which are growing and shrinking Address space of segment n is of form (n,local address) where (n,0) is starting address Can compile segments separately from other segments Can put library in a segment and share it Can have different protections (r,w,x) for different segments
Paging vs Segmentation
Next class
Deadlock