written 2.8 years ago by |
Virtual Memory
There are instances when the combined size of the user program, the data it accesses exceeds the RAM size. (means, you have x GB space in RAM, but the requirement is X+)
In that case, one can use overlays. (Note that we can't use swapping here since even after swapping an existing program, space in RAM will not be sufficient).
In case of overlaying, it is the responsibility of the programmer to split the programs into modules to fit into the available memory.
This concept lets the OS run a program, whose size (program size + data size + stack size) is greater than the RAM size available.
Virtual memory allows only a portion of program to reside in the memory while keeping the rest in the hard disk.
Sounds similar to the overlaying right? Of course yes. But it comes with a difference.
Overlaying involves manual effort, virtual memory doesn't.
This virtual memory can be realized by implementing paging and segmentation.
Paging
• Paging technique lets the whole program reside non contiguously in RAM, while avoiding the external fragmentation problem (and compaction to reduce it) which existed with the partitioning technique.
• Do you know? We have a virtual memory in every PC (Check settings please)
• The OS treats some portion of the hard disk as virtual memory that is seen as RAM by the OS.
• A virtual RAM space is what we call virtual memory.
• This virtual memory is split into pages and the RAM is also split into frames of same size as pages.
• Size of the pages ranges from 4 KB.
RAM frames and the page size in virtual memory should be the same.
• The pages are loaded in the free frames in RAM.
. So, we have now two structures, Virtual Memory and Physical Memory, (i.e.) RAM.
• There must definitely be a way of mapping between the two.
• OS maintains a Page Table for this purpose.
• We shall understand the scheme clearly, now
What is seen by the processor is a 32 bit virtual address of a page to be accessed. Now, the OS will have to use this virtual address to find out the actual page number in the page table
• The OS splits the first 12 bits and treats it as offset in RAM. It doesn't bother much about it. The next 20 bits are used to identify the page number in the page table.
. Since there are 20 bits available for page number, we can have as many 2*20 pages!! That is why we have marked the numbers from 0 to 20-1.
• The page table has two fields;
One holds the frame number where the corresponding page is present, along with a valid/invalid bit. If it is 1. then the page is in RAM. Else the page is not present in the RAM and it will lead to a page fault. The address present against the invalid bit will then be of no use.
Segmentation
• What's wrong with Paging?
. With paging, the memory reference in user's point of view is different from the actual memory reference.
• What is known to the user is virtual address while the address in RAM will be something different.
• Another fact about paging is it deals with a linear address space starting from 0 to a maximum depending on number of bits of address the architecture supports.
• Consider you are writing a program to a fetch employee details from a structure or class. In the code, following elements may be present:
a. Variables or identifiers to hold the data - symbol table maintains the list
b. Procedures or sub-routines to perform some operations
c. Calls to the procedures and returns from them - stack is accessed here
d. Data that represent employee details - Source text area holds the data
• At one point in time, if there is no space in the stack space and still push operation is going on without pop, the entries will start entering the neighboring space other than the designated stack space!!
• This leads to collision.
• Similarly, if there are lots of variables, symbol table may fill up and take space in other regions.
In order to prevent this, segmentation concept was brought in!
• Instead of one linear address space, the segmentation concept leveraged user point of view of memory.
• The virtual address is split into independent segments of varied sizes, with the ability to grow and shrink dynamically as and when needed.
• The obvious benefit of this method would be that there is no collision among the entities of the program.
• They can now grow and shrink without disturbing other addresses.
• Each program can have different number of segments as well; suppose there is program just to print your name, in which case there is no stack involvement, there will be no symbol table involvement too.
From the virtual address, identify the segment number from the segment table. Basically, it is an offset in the segment table addresses starting from 0.
Move to the offset and identify the limit of that segment.
Compare the offset in the virtual address with the limit of the identified segment. If the former is lesser than the later, things are fine. Else generate an error. 4. If the offset is lesser than the limit, take the base address of the segment from the same row of the table.
Use the base address and the offset from the virtual address and generate the physical address!!