0% found this document useful (0 votes)
208 views

Memory Management - Segmentation Paging and Virtual Memory

Virtual memory allows processes to access more memory than is physically available in RAM by storing unused process pages on disk. The OS divides processes and memory into pages. When a process needs a page not in RAM, it is loaded from disk. A page table tracks which pages are in RAM and their locations. This allows processes to run without being entirely loaded in RAM at once, improving memory utilization.

Uploaded by

Suhail Alam Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
208 views

Memory Management - Segmentation Paging and Virtual Memory

Virtual memory allows processes to access more memory than is physically available in RAM by storing unused process pages on disk. The OS divides processes and memory into pages. When a process needs a page not in RAM, it is loaded from disk. A page table tracks which pages are in RAM and their locations. This allows processes to run without being entirely loaded in RAM at once, improving memory utilization.

Uploaded by

Suhail Alam Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MEMORY MANAGEMENT

First of all you should know that in this case when we use the word memory we are referring to the RAM
only. A hard disk is not memory rather it is secondary storage.

What we must also understand is that RAM size is limited. For example you have a 8-GB RAM in your
computer, that means you cannot load more than 8-GB of data in the RAM, so you are limited.

EXAMPLE SCENARIO.

1. Suppose we have 4 programs A, B, C and D running on the computer system at the same time.
2. Program A is 4GB, Program B is 7GB , Program C is 3 GB and Program D is 1 GB in size.
3. The total size of all the programs is 4 + 7 + 3 + 1 = 15 GB
4. Whereas the RAM size in our computer is only 8GB.
5. Out of this 8GB ,some part of RAM size let’s say 1GB is used to hold the Operating System
currently in use which effectively leaves us with 7GB of space in the RAM.
6. So the programs require a total of 15GB whereas the RAM space we have available is only 7GB.
7. This simply means that the Operating System will not be able to load the entire 15GB into the
RAM at the same time. Rather OS will only load only parts of the programs into the RAM. In our
scenario only parts of program A, program B, program C and program D will be loaded into RAM.

The Operating System has several strategies to handle this problem. These memory management
techniques include

1. Segmentation
2. Paging
3. Virtual Memory
SEGMENTATION

1. A Process may be divided into many segments.


a. Each segment may be of different size
b. Segments normally match natural divide in jobs/process
c. Segment of a process are loaded into the RAM rather than loading the entire process
2. Memory is divided into variable length blocks called segments. RAM Memory is allocated to a
process segment based on the size of the segment
3. OS uses a segment table to store index of segments i.e. where the segment was loaded in the
memory and also the length of the segment

(Note: Segmentation is not a very popular technique and is not widely used by Operating Systems)

How does the OS system know what the size of the segment is going to be and what do we mean by
a natural divide? (Please note that the explanation given below is only to help you grasp the concept,
it is not a potential exam question. The actual size calculation of a memory segment is much more
complex)

By natural divide we mean for example the different modules in a program code that perform a
specific task. So one module would form one natural divide and the size of each module may be
different from the other . Thus the program is divided naturally into parts based on the different
modules that carry out different tasks.

As far as allocating RAM memory is concerned the OS looks at the size of a module in a program
and allocates the corresponding amount of memory in the RAM according to the size of the module.

The picture shows all 4 different processes A,B,C,D loaded into the RAM. Each process has been assigned
a segment of memory according to the need of the program. Program C for example needed the most
space. Program D needed the least amount of space.

The segment table holds all the necessary information related to the segments in the memory.
PAGING

1. RAM Memory is divided into equal sized parts called page frames.
2. A process that is to run on the processor is divided into equal sized pages. The size of a page is
the same as the size of a page frame (i.e. if the page frame is 4KB , then the page size is also
4KB)
3. All the Pages of a process are loaded into page frames

(Note: This is not a very good approach. Loading a very large process into RAM will not leave room for
other processes to be loaded into the RAM and is not widely used by the Operating System)
PAGING WITH VIRTUAL MEMORY

1. RAM Memory is divided into equal sized parts called page frames.
2. A process that is to run on the processor is divided into equal sized pages. The size of a page is
the same as the size of a page frame (i.e. if the page frame is 4KB , then the page size is also
4KB)
3. Only pages that are immediately needed by the process are loaded into the RAM
4. Pages may be loaded discontinuously in memory
5. OS maintains a page table for each process in the memory
a. The page table stores index of pages

The following table is an incomplete page table for a process X that is running on the computer

Page Presence Flag Page Frame Address Additional Data


1 1 132
2 1 245
3 1 232
4 0 0
5 1 542
6 0 0

135 0 0

UNDERSTANDING THE PAGE TABLE

1. Looking at this page table we can see that Process X has a total of 135 pages.
2. The presence flag indicates whether a page of the process is present in the RAM or not (1
indicating that the page is in the RAM and 0 indicating that the page is not present in the RAM.)
3. The page frame address (remember memory is divided into equal sized blocks called page
frames) indicates the page frame in the RAM where the process page is loaded e.g. Page 1 is
loaded in page frame number 132 and Page 2 of the process is loaded into page frame number
245.
4. This is what we mean by discontinuous i.e. if page 1 is in page frame 132 then it is not necessary
that page 2 is loaded continuously into the next page frame 133 rather it may be loaded
discontinuously in some other page frame such as 245.
5. Some pages are not present in the RAM
So if a process page is not present in the RAM where is it?

This is where the concept of virtual memory comes in. The remaining pages of the process are loaded in
a reserved area of the hard disk that acts as virtual memory .

Let’s try and understand through the following example

1. Suppose CPU is currently executing instruction which was fetched from location number 700 in
the RAM.
2. The current instruction being executed is a jump instruction i.e. JMP 2500
3. CPU simply thinks next instruction to be fetched is lying at address 2500 in RAM
4. There are two possibilities now
a. The instruction is part of a page that is not continuously located, so even though the
instruction is in a page in the RAM, it may not be at location 2500 rather it may be
discontinuously located at some location like 8205 for example.
b. The second possibility is that the instruction is in a page which is not inside the RAM
rather that page is lying in virtual memory
5. To solve this problem of where the instruction is located (whether it is lying inside a page
present in the RAM or lying in a page present in the virtual memory) , a special hardware called
the Memory Management Unit calculates/determines the actual address of the instruction from
the address provided by CPU
6. If this page is not lying in the RAM it is fetched from the Virtual memory and loaded into RAM
a. What if there is no free page frame available right now in which to load this page?
i. The OS then decides which page in the RAM can be replaced using a page
replacement algorithm

ADVANTAGES OF VIRTUAL MEMORY

1. One of the advantages of the virtual memory approach is that a very large program can be run
when an equally large amount of memory is unavailable
2. For the CPU, the address calculation is straightforward. It simply thinks all instructions of the
program are lying sequentially in the RAM. It does not have to perform complex address
calculations to determine the real address of the instruction.
3. One other advantage is that Virtual Memory allows only part of a program to be present in RAM
memory at one time. This allows RAM size to be shared amongst different processes that want
to run on the computer system.

DISADVANTAGES OF VIRTUAL MEMORY


1. The system overhead in running virtual memory can be a disadvantage (i.e. the amount of
processing required to compute address of an instruction)
2. Disadvantage: The worst problem associated with virtual memory is 'disk thrashing'. A page (e.g.
Page #135) is requested and fetched from virtual memory. It is loaded into RAM (at page frame
address 542) by replacing an existing page (page #5) in the RAM using page replacement
algorithm. This new page (page #135) however immediately requires the page that it just
replaced in the RAM (i.e. Page #5). This can lead to almost perpetual loading and unloading of
pages
Define virtual memory?
It is a paging mechanism that allows a program to use more memory addresses than are
available in main memory

Virtual Memory Summary


- A whole program does not need to be present in memory at the same time
- Only pages needed at the start of process are loaded into main memory
- Remaining pages of the process not inside the RAM are loaded in a reserved area of the hard disk
- During execution of a process if a new page is needed by a process it is loaded from virtual memory
into a page frame in the main memory/RAM
o If all page frames are currently in use (i.e. no page frame is free and available)
OS decides (using page replacement algorithm) which page can be replaced
from the RAM to allow for the new page to loaded in its place.
o If the page being replaced was modified during the execution of the process, it first needs
to be saved on the hard disk before being replaced
- Program believes that the whole program is stored in memory simultaneously
- Disk thrashing may occur. A page in RAM requires a paged from virtual memory may replace a page
that it needs almost immediately. The older page is restored once again into RAM by replacing this
newly loaded page. This may result in frequent loading and unloading of pages from virtual memory
to RAM which can tremendously slow down the computer system

You might also like