Sunday, February 2, 2014

Paging and Segmentation

I hope that you know about fragmentation . If not please go through my other post in the same blog about it.

Paging and segmentation are both used by the linux kernel to deal with the problem of external fragmentation.



PAGING:

  •  Physical memory is broken into fixed sized blocks called frames.
  • Logical memory is also broken into same sized blocks called pages.
  • Every logical address generated by CPU is divided into two parts : Page number(p) and pageoffset(d).
  • The system maintains a page table which has a mapping of logical and physical addresses eg. page 3 in logical memory corresponds to frame number 8 in physical memory.
  • When a pointer tries to access a page that is currently not mapped to the physical  memory page fault occurs.( it's a normal occurrence and not observed by user).

SEGMENTATION:

  •  Segmentation differs from paging in the sense that in segmentation the blocks of memory(both logical and physical) are of different size.
  • The segment table ( analogous to the page table in case of paging) keeps two values for each segment 1. Segment base 2. Segment Limit.
  • The segment table uses these two values to generate physical addresses.
  • If a program tries to access the data beyond the limit specified by segment base and segment limit then the kernel receives a trap called the segmentation fault and it can abort the program.
  • In segmentation, the address space is typically divided into a preset number of segments like data segment (read/write), code segment(read-only), stack(read/write) etc. And the programs are divided into these segments accordingly. Logical addresses are represented as tuple <segment, offset>.





Each of these techniques offer several benefits e.g., segmentation offers better security since the segments are pre-defined and we can avoid spurious reads/writes at the time of address translations etc. 

On the other hand, paging helps reduce fragmentation(as there won't be memory holes left out of smaller holes being left) and ensures better memory management.

So, most modern operating systems employ a combination of both these techniques for their memory management.

No comments:

Post a Comment