Paging and Segmentation in Operating Systems are memory management techniques that are used to divide Processes, store them in physical memory and retrieve them efficiently whenever is necessary.
Scope
- We will understand what Paging is with an example and discuss how Segmentation resolves the drawback of Paging.
- We will visualize the flow of Paging and Segmentation and learn how they share some similarities and differences.
Definitions
In layman’s terms, Paging is a memory management technique that allows the physical and contiguous address of Process to be non-contiguous and disintegrated into pieces of even size of memory which can be called Page.
If we take one example there is a function in Process(program) named as function1 which has 150 lines of code into it then In Paging, there are chances that that function can be shattered into multiple pages because once memory allocation is completed with Paging then it can't vary in size. To learn more about why it is Segmentation is required visit this.
These kinds of restrictions can either result in a shortage or wastage of memory. To solve the above problem There is an alternative memory management technique provided by some computer systems known as Segmentation in OS.
In Segmentation, memory is fragmented into logical and variable-sized blocks which can hold necessary data for that segment into multiple locations like stack and heap memory. On the other hand, Paging stores necessary data as an array of bytes of fixed size.
How are Paging and Segmentation similar and different in their flow of execution?
In this part, we will understand similarities and differences in Paging and Segmentation in Operating Systems along with their flow of execution.
In Paging, physical memory is divided into fixed-size frames and logical memory(Process) is divided into even-sized pages. and those pages can be laid down into frames of the same size.
Before jumping into the flow of execution we need to know about Page Table which is associated with the Process in execution and residing in the main memory. Page Table is a data structure that acts as a co-ordinator between CPU which needs a specific Page or its data and Physical memory where this page is residing in one of the frames. Page Table entries consist of Page number as key and frame no of a page in physical memory and offset as value when the CPU generates a logical address to get some information from Pages residing in physical memory. This logical address consists of two parts: Page number and offset. to use this logical address Operating system locates Page Table and using this page Table Operating System will be able to locate required pages in physical memory which will be returned to the CPU. This is a high-level overview of how Paging works.
In Segmentation, Processes are divided into variable sized chunks which can be called Segments and all those segments can be laid down into main memory with different sized frames.
Before jumping into the flow we will understand little about the Segment Table. Again it is a similar kind of data structure like Page Table which acts as a mapper between the logical address generated by CPU for one of the segments to that particular physical segment address. Although it has the same structure yet it works differently from Page Table.
Here the logical address consists of two parts Segment number and distance. When the CPU generates the logical address of Segment then the Operating System locates Segment Table where it will search for segment Number so it gets tuples of base Address and limit. Now the Operating System checks if the distance provided in the logical address is greater than the limit means we can assume the CPU is asking for data that does not exist in physical memory so an exception is raised. On the other hand, where distance is less than the limit it will fetch information onto that segment and return that to the CPU for further processing. That's how Segmentation works.
Summary
- Both Paging and Segmentation are memory management techniques.
- In Paging, Process is divided into even-sized Pages
- In Segmentation, the Process is divided into variable-sized Segments.
- In Paging and Segmentation, Page Table and Segment Table acts as middleware by CPU and physical memory respectively.
Author Bio
Akash Pawar is a software engineer and has the passion to write technical blogs with expertise in Data Structures and Algorithms, Operating systems and has exposure to large-scale System Designs.