Difference Between Paging and Segmentation

Difference Between Paging and Segmentation

Introduction

Paging and Segmentation both are techniques of memory management in the operating system. Now, In order to understand the difference between paging and segmentation, first, it is necessary to know the basics of memory management. So let’s quickly discuss it.

Memory is the storage space in our computer systems. It is divided into a large number of small parts called cells or bytes. Each byte has its own address. In our computers, we have 2 types of memory: Secondary memory and Primary memory aka RAM. As we all know, all of our programs get stored in secondary memory, and in order to execute them, we need a CPU (Central Processing Unit). Now, the CPU is not directly connected to secondary memory because of the speed constraints. The CPU is connected to primary memory. That is why to run any program it must be present in Primary memory (or RAM) But the processing speed of the CPU is very fast. And, to use the CPU efficiently, it is necessary to store more and more programs in the RAM. As we all know, the size of RAM is quite less in our systems since it’s an expensive memory. Due to the smaller size, it’s very important to manage the memory efficiently.

What do we mean by managing memory? When programs come into the RAM, they must be allocated memory in such a way that more and more programs could get stored. In memory management, we make partitions in the memory such that we can fit more and more programs into it and we also make partitions in the program to store it in the memory. Here memory refers to the main memory i,e, RAM.

We have two types of memory management techniques:

  • Contiguous Memory Allocation
  • Non-contiguous Memory Allocation

In Contiguous memory allocation, all the partitions of a program are allocated contiguous memory in the main memory. In contiguous allocation, we have two types of partitioning: Fixed partitioning and Variable partitioning. As the name suggests, In fixed partitioning, we divide the memory into fixed-size slots. Each partition will be of the same size. On the other hand, In variable size partitioning, the partition size will depend on the size of the program and it will be allocated dynamically. Whenever the program comes into the RAM only then we will allocate the space to them. If the program is of 5 MB, then a partition of 5 MB in the memory will be allocated. Irrespective of the type of partitioning, the whole program will always be allocated contiguous memory only. Spanning of a program is not allowed here.

In Non-contiguous memory allocation, it is not necessary to store all the partitions of a program contiguously. We can store different partitions of a program at different memory allocations. Paging and Segmentation both are non-contiguous memory management techniques.

What is Paging?

In the Paging memory management technique, we divide the program into fixed equally sized pages and we divide the primary memory(RAM) into fixed equally sized frames. The size of each page will always be equal to the size of each frame. And due to this, one page of a process will fit exactly in the frame. When a program arrives in the system to be executed by the CPU, its size is examined which is expressed in the number of pages. Each page of the process needs one frame to get stored. Thus, if the program is divided into n pages, then at least n free frames must be available in the main memory. If n frames are available, they are allocated to the pages of this new arriving process. The first page of the program is loaded into one of the allocated frames, and the frame number is added to the page table for this process. The next page is loaded into another frame, its frame number is added to the page table, and so on.

In contiguous memory allocation, we had the problem of External fragmentation. Now external fragmentation is a condition when we have enough space in the memory to fulfill a request but that free space is scattered here and there, i.e, it is not contiguous. Free space is fragmented into a large number of small holes. Since Paging is a non-contiguous memory allocation technique, it solves the problem of external fragmentation. But along with external fragmentation, it also solves the overhead of compaction. Compaction: combining all the empty spaces together using reallocation.

Advantages of Paging 

  • Paging avoids external fragmentation and compaction. 
  • In paging, allocation of memory is easy and cheap. 
  • In this technique, swapping processes is easy between equal-sized pages and frames.
  • Paging also solves the problem of fitting memory chunks of varying sizes onto the backing store. 
  • The page which is just swapped out is least likely to be used again. 
  • It allows demand paging and prepaging. 

Disadvantages of Paging

Frames are allocated as units and because of this, they may have some internal fragmentation. If the memory requirements of a program do not happen to coincide with page boundaries, the last frame allocated may not be completely filled and will have some free space. 

For e.g, Suppose the page/frame size is 8MB and the process is 14 MB. In this case, 2 frames will be allocated but the last frame will have 2 MB of free space. 

In the worst case, a process would need n pages plus 1 byte extra. It would be allocated n + 1 frames, resulting in the internal fragmentation of almost an entire frame.
For e.g, Consider a process of 9MB where the page size is 8MB. In this case, it will be allocated 2 frames. The second frame will have only 1 byte occupied.

Page tables and frame tables are used to keep the details of free and allocated space. They consume additional memory to get stored. Every process will have its own page table in the memory.

What is Segmentation?

Segmentation is a memory management technique that is used to store a program in the main memory. In Segmentation, a process gets divided into variable-sized parts known as segments. Segments vary in length, and the length of each segment is intrinsically defined by its purpose in the program. Segmentation supports the programmer’s view of memory. Now, what is the programmer’s view of memory? So, when writing a program, a programmer thinks of it as a main program with a set of methods, procedures, or functions. It may also include various global variables, stacks, objects, and so on. In the same way, in segmentation, the compiler thinks of a program as different segments. When a program is compiled, the compiler automatically divides the whole program into segments. A compiler might create separate segments for global variables, stacks, code, heap, etc. The information about each segment is stored in a table known as a segment table. 

Advantages of Segmentation

  • Segmentation supports the programmer’s view of memory. 
  • No internal fragmentation. 
  • The size of the segment table is generally less than the size of the page table. Because in paging we need a separate page table for every process. And the size of each page table would depend on the number of pages a process has. 

Disadvantages of Segmentation

  • As programs are loaded and removed from the memory, the free memory space is broken into little pieces known as holes which cause External fragmentation.
  • The unequal size of segments is not good in the case of swapping.
  • It is difficult to allocate contiguous memory to variable-sized partitions.
  • Segmentation is a costly memory management technique.

Key Difference Between Paging and Segmentation

S.NoPagingSegmentation
1.In Paging, the program gets divided into equal-sized parts known as pages to be stored in the main memory of a computer when they are being accessed.In Segmentation, the program gets partitioned into variable size parts known as segments to store them in the main memory.
2. Paging is done by the operating system.Segmentation is done by the compiler.
3.It may store the code of the same function on different pages.It stores the same function in one segment.
4. It results in a less efficient system because to execute one function we may need to access multiple pages. It results in a more efficient system because the compiler keeps the same type of functions in one segment.  
5. Page tables are used to store details about pages and frames. Segment tables are used to store details of segments. 
6.To convert the logical address into a physical address an entry of a page table having <page number,frame number> is used.To convert the logical address into a physical address an entry of the segment table having <base address,limit> is used. 
7.Paging is faster compared to segmentation.Segmentation is slow.
8. Paging can cause internal fragmentation when the program size is less than the determined page size. Segmentation can cause external fragmentation after removing some programs resulting in small holes.
9. The logical address consists of two parts: page number and page offset.The logical address consists of two parts: segment number and segment offset.
10.In paging, the operating system maintains a list of free frames that can be allocated to pages. In segmentation, the operating system maintains a list of holes present in the main memory. 
11.A page is considered a physical unit of information. A segment is considered a logical unit of information. 
12.The concept of paging is totally hidden from the user.The concept of segmentation is visible to the user.
13.In paging, the size of the page is always equal to the size of frames. In segmentation, there’s no constraint on the size of segments. 

Conclusion

Paging and Segmentation both are non-contiguous memory management techniques. They both are used to allocate main memory to the programs. Paging divides all the programs into equal-sized pages regardless of the fact that a program can have some relative functions which need to be loaded on the same page. It may store the same function in different pages and those pages may or may not be loaded at the same time into the memory. It decreases the efficiency of the system. To overcome this limitation, segmentation is used. Segmentation divides the program into segments. Each segment contains the same type of functions such as the main function can be included in one segment, global variables in one segment, and the library functions can be included in the other segment.

Frequently Asked Questions

Q. Is Paging contiguous?
No, Paging is a non-contiguous memory allocation. We can store the pages of a program in non-contiguous frames. 

Q. What is paging used? 
In contiguous memory allocation, we can’t span a process in different partitions of memory. This results in external fragmentation. To overcome this we use non-contiguous memory allocation. Whenever a process wants to come into the RAM we check the total holes present in the memory. We make partitions of the process according to hole sizes. Now, this process is costly because we are partitioning a process at run time. So to make this whole process less costly we divide the process into fixed-sized pages and RAM in fixed-sized frames beforehand. 

Previous Post

Golang vs Python: Full Difference Explained

Next Post

Stateful vs Stateless: Full Difference

Exit mobile version