A common question that comes to our mind is – What is the difference between Contiguous Memory allocation, Paging and Segmentation? We will understand this concept with an individual example, explaining each of the three concepts.
Contiguous Memory Allocation
In contiguous memory allocation the entire address space of the process is together. For example, if a process requires 20MB space then this space should be a single block. Let us understand this with an example.
Suppose the total memory space is 100MB
Now, if process P1 requires 10Mb space then allocate it together. The updated memory will look like this
Next, suppose P2 process arrives and it requests 30MB space. So this space is also allocated together. The memory will now look as below.
Next, suppose P3 process arrives and it requests 20MB space. So this space is also allocated together. The memory will now look as below.
Next, suppose P1 process finishes. Then the system frees the space of process P1. The memory will now look as below.
Next, suppose P4 process arrives and it requests 25MB space. So this space is also allocated together. The memory will now look as below.
Next, suppose P3 process finishes. Then the system frees the space of process P3. The memory will now look as below.
Next, suppose P5 process arrives and it requests 25MB space. Can the system allocate this space? The answer is No. Because although 45MB of space is free but 25MB is not together. There are three free blocks 10MB, 20MB and 15MB but none is big enough to accomodate process P5. This problem is called External Fragmentation.
What is Paging?
In paging the system divides the memory space into slots called frames. The size of frames is fixed. Similarly, the logical address space of the process is divides into pages. The size of a page is equal to the size of a frame. Finally, the system does a one-to-one mapping of pages and frames. That is every page is allocated a frame in the memory. For example, consider the memory (in frames) as shown below. A process P1 arrives whose size is 2048 bytes. The frame size is 512bytes. Hence, the page size will also be 512 bytes. So, the number of pages required for P1 are 4 (2048/512). So, every page gets a frame as shown in the Page Table. Every process has its own page table.
Next, a process P2 arrives whose size is 1024 bytes. This means it requires 2 pages. This process gets frames as shown in the figure below. P2 has its own page table.
Next, a process P2 arrives whose size is 1536 bytes. This means it requires 3 pages. This process gets frames as shown in the figure below. As only 2 frames are free, so only 2 pages of P3 gets the frame. So P3 can at least start its processing. This was not possible if contiguous memory allocation was used. P3 has its own page table.
Paging suffers from Internal Fragmentation. Internal Fragmentation means some space is wasted within a frame because the page has less number of addresses as compared to frame size. For example, if the process size is 112bytes. This means it requires only one page and hence one frame. But frame size is 512 bytes. So, 400bytes within the frame are free. Or in other words wasted.
What is Segmentation?
In segmentation the process address space is divided into segments. Each segment gets a block of main memory. The segments may be apart int the main memory but all address within a segment a together.
Process P has three segments. Segment 0 starts at address 0 and the limit is 500 i.e., it has total 500 address in it. Similarly segment 2 starts at 700 and extends till 1300 because there are total 600 addresses in it. Finally, Segment 3 starts at address 4000 and extends till 5000 because the limit is 1000.
The PPT below contains an animation to explain the difference between contiguous memory allocation paging and segmentation.