Difference between paging and segmentation in operating systems

Comparison charts and difference between paging and segmentation is discussed in this article. Starting with memory management systems and basic introduction of paging and segmentation.

What is memory management system?

In operating systems, while we work with different processes, we always come across memory management systems techniques in operating system which are used to manage the memory blocks as efficiently as possible. These techniques are mostly used when we need to work with a large number of processes with a limited memory provided. During the execution of a process the main data (code, stack and heap) of a process is stored in the main memory but due to the restricted size of the main memory we cannot keep all the currently running processes in the main memory so we switch the processes which are not currently running to the hard disk so that we can fetch them back to main memory when it is called by the CPU.

Memory management system is used to keep track of the memory (main memory and hard disk memory). It will keep a thorough track of each memory portion, i.e. whether it is allocated to some processes or not and it will allocated each chunk of memory to the newly created process accordingly and update their information in the memory management system accordingly of whether it is occupied or freed.

In memory management system, there is a specific portion to understand i.e. whether we are storing the data in memory as large chunks of variable sizes (segmentation) or we are allocating memory to different pages in the form of small fixed sized block (pages).  Let’s have a thorough look on the functionality and uses of each one of them.

Introduction to Paging in OS

As we have discussed earlier, whenever we want to execute a process in CPU, its data i.e. code, stack and heap must be available in the main memory however only a few of pages are present in the main memory and all other are stored in the hard disk memory (as it has a larger space than main memory), hence for the execution of a page we have to load all the stored data of that page from the hard disk to main memory in order to complete its execution.

While loading (or swapping) data from the disk memory to main memory we have to swap a specific amount of memory in some cases. If we load a fixed block of memory from disk to main memory these fixed blocks are then referred as pages and the technique is called paging.

The simple paging phenomenon can be explained well in the image given below,

Figure 1: Paging

Lets’ assume that the block at the left is a single process divided into 4 parts and the block at the right is the physical memory. It is not mandatory to place all the pages in the main memory one after the other we can place then according to the need so that other processes can share the same memory without any issue. The page table (stored in memory management unit) MMU as we have studied previously is used to access the stored pages in future. For instance, in the above figure the page table tells us that the page with page number 3 (logical page number 3) is stored at the physical address of 7 and so on, which is not difficult to read for a person who have taken a course of operating systems.

While working with pages we don’t have to store memory contiguously rather we store it in the form of small chunks (pages). We divide the total memory space required by a process into small fixed sized chunks and place them at different locations within the memory provided that their logical or virtual address are contiguous no matter how distant they are placed in physical memory. This paging technique will reduce the effect of external fragmentation however it will increase the effect of internal fragmentation. Lets’ now have a brief look on fragmentation before jumping to segmentation.

Fragmentation basics 

In normal language the word fragmentation is referred as breaking a state or a large chunk into small fragments. Similarly, in computer sciences, when a memory unit is used inefficiently small chunks of unused memory are created in the overall memory space. Because of which we cannot use these small chunks and a large amount of memory is wasted. This wastage of memory decreases the effectiveness of memory usage. As we can see from figure 1, that the small non-contiguous portion of memory (the gray colored memory blocks) is used and a large portion of memory is wasted (blue colored blocks). This wastage of memory is referred as fragmentation. Fragmentation can be divided into two major types:

  • Internal fragmentation
  • External fragmentation

Internal fragmentation

This is the type of fragmentation which occurs whenever we have divided memory into fixed sizes of blocks. For instance, the block size in which we have divided the memory is 4Kb and we want to assign memory to a data block of size 3Kb, thus the memory assigned to the data will be a bit larger than the memory requested. This will cause internal fragmentation of 1kb in a single page. This may seem small but on a larger scale even this amount of memory will reduce the memory performance to a significant rate.

External fragmentation

Now comes, the external fragmentation portion, whenever we load a process in the memory it will create small holes or gaps in memory allocation. External fragmentation occurs when the physical memory has enough space available in it to entertain a whole new process, but it could not do so because the available memory is not present in a contiguous manner. This can occur either by first fit or best fit method. External fragmentation occurs when the memory management strategy used is segmentation rather then paging. We will now divert our attention towards segmentation. But let’s first conclude the fragmentation topic with the help of an image illustration as shown below,difference between paging and segmentation

Figure 2: Fragmentation

As shown in the figure above, internal fragmentation occurs within the same memory chunk and external fragmentation occurs between two different portions of memory.

Introduction to Segmentation in OS

Segmentation is also one of the techniques to use memory efficiently. When we allocate memory to processes in form of variable sized large portions, this allocation is called as segmentation. The issue with segmentation is that when we allocate a process to memory it will create a hole in that specific portion of memory. Even after we remove the processes from that portion, we cannot allocate the created hole to a process with larger data size.segmentation

Let’s assume that data is stored in the above memory block through segmentation. We assigned a data block of 1Kb then 3 kb then 8kb and at the end 4kb to the memory and after that we remove the process with size 3kb and 4kb. Now, we want to add data block of size 6kb to memory, although we have available space of 7kb but we cannot assign the given data bloc (7kb) to the memory because the memory availble is non-contiguous. Memory allocation of this type is called segmentation and this flaw in segmentation is called external fragmentation. This issue in segmentation can be resolved with the help of coalescing, which itself is a very long topic of discussion in computer systems. For the time being, accept that coalescing is a scheme of combining the externally fragmented portion of memory that are coincinding with each other in phycial memory.

Comparison chart of paging and segmentation 

Paging Segmentation
Size of block Fixed Variable
Type of fragmentation Internal fragmentation External fragmentation
Basic Fixed size of data is retrieved from disk to main memory Variable size of data is retrieved depending on the size of process data
Table A page table is involved which contains the page numbers mapped with physical address A segment table is used which contains the segment number mapped with physical address
Address Address is divided in page number and offset Address is divided in segment number and offset

You may also like to read:

Leave a Comment

shares