Paging and Segmentation are both methods of memory allocation.
Let us discuss them one by one.
Difference Between Paging and Segmentation Explained
Paging is one way of allocating memory to processes.
It helps in avoiding external fragmentation during the computer operation when processes are added and removed from memory.
Let us discuss how it works
1. Logical Memory
Let’s assume you want to load a process into memory in order to run it. Unfortunately, you do not have sufficient amount of physical RAM.
In situations like these, the operating systems resort to what we call logical memory (virtual memory), Basically, the OS turns a portion of the allocated hard disk space into a temporary RAM.
One thing to note here is the hard disk is not turned into the physical RAM itself.
instead, the information is swapped between virtual hard disk memory and the physical RAM.
The less immediate information is sent to the hard disk and recalled when needed to the RAM.
You now have logical memory where you are imagining storing processes.
Basically what the OS is doing is dividing the physical memory and the logical memory in to equal sized blocks keep allocating them to the processes.
The blocks on the logical memory are called pages.
One page will hold a small portion of the process.
Many pages, all in a sequence, one after the other, will together house the complete process.
3. Logical AddressTo address the process elements in logical memory, you need to identify the page and then the position of the data within the page.
This will be the logical address of the process in memory.
The pages are all given a number. Thus, the logical address becomes a combination pair of page number and offset.
4. Physical Memory
The processes in actual have to be physically stored in the RAM, no matter what the allocation method the OS uses.
Since you are imagining the process in logical memory where it is contiguously stored, you will need to have a mechanism that actually causes the process to be transferred to and fro the physical memory.
Similarly, when you are done with the process and you want to make space for another process you will actually be deallocating it from the physical memory and back to the logical memory.
When the OS wants to store logical memory pages to the physical memory, it divides the physical memory into chunks the same size as the page.
The chunks of physical memory are called frames.
So why have logical memory pages and then physical memory frames?
Basically, this is where the paging technique has a clear advantage.
The frames do not have to be next to each other for storing the pages of the logical memory which are sequential.
In other words, pages are contiguous while the frames holding the same data are non-contiguous.
The imaginary layer makes the it look to the system, that the process is stored contiguously, whereas in reality, it is not.
Internal fragmentation is one of the drawbacks of the paging technique as it leaves a free space inside a memory block.
Recall that in paging, pages and frames are made up of equal sized blocks.
Therefore, if a page on the virtual memory is 4 kb in size, but the a process makes a request for 3 kb only, then this leaves a 1 kb free and unused.
This is called internal fragmentation.
6. Physical Address
The physical address of the process data is defined in terms of Frame number and offset pair.
So, every page number has a corresponding frame number where the process data is stored in physical memory.
The page size and frame size are the same, so the offset inside the page in logical memory corresponds to the same offset value inside the frame in physical memory.
7. Page TableThe information mapping the page number to the frame number is stored in a page table in the pairs page number, frame number.
Every process has its own page table created when it is loaded into memory.
8. Paging Mechanism
Loading the process into memory
- When a process is to be loaded into memory, an internal mechanism splits the process among contiguous pages in logical memory.
- For each page, a frame is allocated in the physical memory and the Page#: Frame# pair is stored in the Page Table.
- The process data is stored in the physical memory in the corresponding frames.
Memory access by computer program
- The processed may be accessed for Read or Write operation.
- As long as the process is running, the process data is accessed by the computer program via page number: Offset pair.
- Through internal mechanism, the frame number corresponding to the page number is read from the Page Table.
- The frame number: Offset pair is used to access the actual data in the physical memory and for the Read or Write operation to be performed on it.
Segmentation is also a way of allocating memory to processes.
It can also be considered, in principle, as Paging with variable page sizes.
1. Logical Memory
Let’s assume the computer want to load a process into memory in order to run it.
One thing the computers needs is a contiguous memory space to store the process. So, similar to paging above, you imagine a contiguous memory space and call it logical memory.
What you will keep doing is imagining that you are loading processes into this logical memory (virtual memory in the hard disk) and deallocating them from this logical memory.
Therefore, so far, there is no difference between paging and segmentation. They principally try to achieve the same objective.
2. Physical Memory
The processes in actual have to be physically stored in the RAM, no matter what the allocation method the computer uses.
Since the computer is imagining the process is stored logical memory (hard disk) where it is contiguously stored, it will need to have a mechanism that allows it to be transferred to the physical RAM when needed.
Similarly, when the computer is done with the process it will need to deallocat it from the physical memory and send it back to logical memory to make space for new processes in the RAM.
So far so good. Paging and Segmentation are similar thus far.
3. A Better Approach from Programmer’s Perspective?
Programmers think in terms of “Sub Routines”, “Stack”, “Symbol Table”, “Program Libraries”, “Main Program” etc.
Instead of viewing the logical memory simply as a linear array of bytes, programmers view it as a collection of program modules of variable sizes as those mentioned above.
This view is directly translated to physical memory as well and this is one of the main advantages of segmentation.
In case of use of segmentation method for memory allocation, you are basically storing the process into memory module-wise, where each module has a varying length.
Each process module is stored in a chunk in memory and is called a segment.
Recall that in paging, the unit for transferring and storing data was a page and a frame of EQUAL length.
So as the difference between paging and segmentation working principal goes, this is the main difference.
5. External Fragmentation
When you are creating segments of varying sizes in memory and then deallocating them from memory, you are going to end up with external fragmentation.
This is a drawback of segmentation. The following image explain it very well. Again recall from earlier discussion that the memory has to be contagious in order to store the process.As far as the difference between paging and segmentation in terms of drawbacks are concerned, this is the main difference.
- Segmentation = External Fragmentation
- Paging = Internal Fragmentation
6. Logical Address
All segments are given a unique number when they are imagined in the logical memory.
Thus, they are addressed using a logical address which corresponds to this imagined logical memory.
The logical address of an element within a segment consists of a pair of values, namely the segment number and the offset.
7. Physical Address
The physical address of an element within a segment also consists of a pair of values, namely the segment number and the offset.
8. Loading the Process Into Memory
- When a process is to be loaded into memory, it is segregated into segments based on their functions within the process.
- Each segment is assigned a number and its suitable location in memory is identified based on the length of the segment, so that it is stored contiguously.
- The segment is stored in the physical memory at the identified location having a certain base address.
- The segment number, segment length and segment address are stored in a segment table.
- The same is repeated for all segments of the process
9. Segment Table
Each process has a segment table created. The segment table contains three values for each segment of the process,
- Segment Number
- Segment Length
- Segment Address
10. Memory Access in Segmentation
- As long as the process is running, the process segments are accessed by the computer program via Segment Number: Offset pair.
- Through internal mechanism, the segment address of the required segment is read from the Segment Table of the process using the segment number.
- The system looks for the row with the desired segment number.
- The row additionally contains the segment length and the segment address.
- If the offset in the Segment Number: Offset pair is within the segment length, the address is passed on to the system. This is a protection mechanism to ensure that area outside the segment is not accessed.
- The Segment address: Offset pair is used to access the segment in the physical memory.
In this we looked at the difference between paging and segmentation by learning how they work and what purpose they serve.
Paging and segmentation are two methods of memory allocation. As you have seen, they both have their own advantages and drawbacks.
Paging mechanism allows for non-contiguous storage of processes in physical memory, where we know that the contiguous storage is a must.
In a way, the system fools the program into thinking that it is stored contiguously. There is an internal translation that takes place to take care of the non-contiguous storage.
Segmentation splits the process into meaningful chunks that need to be stored contiguously. The chunks being called the segments.
In case of paging method, there is bound to be internal fragmentation because the pages have a standard length across the board.
It implies that when processes are placed into multiple pages, the last page will mostly have some free space left.
In case of segmentation, there in no internal fragmentation but since the physical memory is divided into variable length segments which become empty when the process is no more required, there is great chance of external segmentation occurring.
Practically speaking the engineers use a combination of both paging and segmentation to make sure fragmentation is completely eliminated.
In the end, if you are suffering from low memory, it is better to upgrade your PC by investing in more Physical RAM.