Tuesday, August 19, 2014

VIRTUAL MEMORY ( COLLEGELA PADIKAVE ILLA. NO NO. SOLLI KODUKALA)


Virtual memory:


  • In computingvirtual memory is a memory management technique that is implemented using both hardware and software. 
  • It maps memory addresses used by a program(program generated address), called virtual addresses, into physical addresses in computer memory.
  • so the complete part of the program not reside in the main memory(RAM). Part of the program in the main memory and part of the program in the secondary memory
  •  Main storage as seen by a process or task appears as a contiguous address space or collection of contiguous segments(stack, heap,bss,DS,text)
  • But operating system uses the Address translation hardware in the CPU, often referred to as a memory management unit or MMU, automatically translates virtual addresses to physical addresses. 
  •  when computer is not having enough(large)RAM for a large program to be kept in memory, but as we know for fast and sucessful execution of prgram ,it is required to be present in memory and our problem is of less RAM. thus virtual memory  provide solution to this problem. virtual memory provides mapping technique for Mapping program generated RAM address to acutal physical address of secndary Memory, at run time .it provde illusion as if our RAM is large enough to hOLd comlete progam.

to make it simple. suppose my program needs the memory 3 gb but actual ram space is 1 gb. but using virtual memory concepts i can able show the program as i am having 3 gb to run the program.  

How is it possible:
there is some techniques there to achieve the same.
1. paging (demand paging is used in the linux)
2. segmentation ( used in old  Multics and IBM System/38 )
3.    swaping



Paging
  • Paging is memory managemnt technique where main memory divided into equal page frame  like 4k,4k,4k. 
  • Paging is a memory management technique used by operating system. complete memory is divided into logical blocks of equal size.whenever any page is required o/s search for that page in memory,if it dont find than a trap is generated known as Page fault and this new page get loaded in RAM from secondary memory.
  • Virtual memory is divided into chunks called pages; size of a page is equal to size of a page frame
  • OS keeps track of mapping of pages to page-frames
  • This virtual memory is managed by a hardware component, the MMU, in charge of mapping physical memory (RAM) to virtual memory(page frame to pages)
  •  For every process, there is a page-table (basically, an array),and page-number p is used as an index into this array for the translation. using page table virtual address(i.e divided pages) is translated into physical memroy(i.e physical frame)
  • Page tables are used to translate the virtual addresses seen by the application into physical addresses used by the hardware to process instructions; such hardware that handles this specific translation is often known as the memory management unit. Each entry in the page table holds a flag indicating whether the corresponding page is in real memory or not. If it is in real memory, the page table entry will contain the real memory address at which the page is stored. When a reference is made to a page by the hardware, if the page table entry for the page indicates that it is not currently in real memory, the hardware raises a page fault exception, invoking the paging supervisor component of the operating system.
  • Memory management is the heart of operating systems; it is crucial for both programming and system administration. In the next few posts I’ll cover memory with an eye towards practical aspects, but without shying away from internals. While the concepts are generic, examples are mostly from Linux and Windows on 32-bit x86. This first post describes how programs are laid out in memory.
    Each process in a multi-tasking OS runs in its own memory sandbox. This sandbox is the virtual address space, which in 32-bit mode is always a 4GB block of memory addresses. These virtual addresses are mapped to physical memory by page tables, which are maintained by the operating system kernel and consulted by the processor. Each process has its own set of page tables, but there is a catch. Once virtual addresses are enabled, they apply to all software running in the machine, including the kernel itself. Thus a portion of the virtual address space must be reserved to the kernel:
    Kernel/User Memory Split
    This does not mean the kernel uses that much physical memory, only that it has that portion of address space available to map whatever physical memory it wishes. Kernel space is flagged in the page tables as exclusive to privileged code (ring 2 or lower), hence a page fault is triggered if user-mode programs try to touch it. In Linux, kernel space is constantly present and maps the same physical memory in all processes. Kernel code and data are always addressable, ready to handle interrupts or system calls at any time. By contrast, the mapping for the user-mode portion of the address space changes whenever a process switch happens:
    Process Switch Effects on Virtual Memory
    Blue regions represent virtual addresses that are mapped to physical memory, whereas white regions are unmapped. In the example above, Firefox has used far more of its virtual address space due to its legendary memory hunger. The distinct bands in the address space correspond tomemory segments like the heap, stack, and so on. Keep in mind these segments are simply a range of memory addresses and have nothing to do with Intel-style segments. Anyway, here is the standard segment layout in a Linux process:
    Flexible Process Address Space Layout In Linux

swaping and demand paging:

One old solution is to displace a process memory completely toward a dedicated part of the disk. This is called swapping (out) and the disk area is called the swap space. Swapping is not used that much by modern OSes for being inefficient in most cases (but not all). It is unimplemented with Linux but is available with most other Unix and Unix like OSes.  

An alternative and common solution is to identify the less active parts of processes virtual memory and store them on that same disk area. This is called demand paging. The memory is stored in usually fixed size contiguous blocks called pages. Paging is often confused with swapping due to the storage area being called swap in both cases. As Linux doesn't support swapping anyway, the term is used interchangeably with paging in this context. 

As there is much less physical memory than virtual memory the operating system must be careful that it does not use the physical memory inefficiently. One way to save physical memory is to only load virtual pages that are currently being used by the executing program. For example, a database program may be run to query a database. In this case not all of the database needs to be loaded into memory, just those data records that are being examined. If the database query is a search query then it does not make sense to load the code from the database program that deals with adding new records. This technique of only loading virtual pages into memory as they are accessed is known as demand paging.

virtual memory is commonly implemented using deman paging. in demand paging process reside in the secondary memory. when we want to execute the process then we bring it to main memory. rather than bringing the whole process we brought the part of the process using lazy swapper.


reference:
thanks to all the tech people by writing above helping me for through my interview process:

No comments:

Post a Comment