CIE AS & A Level Computer Science, section 16.1 (Purposes of an Operating System (OS))
Why memory must be managed
The OS must allocate memory to each process, stop processes accessing each other's memory, and make best use of the limited main memory.
Paging
Memory is divided into fixed-size blocks. A program is divided into pages and RAM into frames of the same size. Pages need not be in adjacent frames. A page table maps each page to its frame.
- Benefit: no external fragmentation, so memory is used efficiently.
- Drawback: the last page may be partly empty (wasted space), and pages are not logical units.
Segmentation
A program is divided into variable-size segments that match its logical parts (such as the code, data or a subroutine). A segment table holds each segment's start and size.
- Benefit: segments match the logical structure, so are easier to share and protect.
- Drawback: different sizes cause fragmentation of memory.
Virtual memory
Virtual memory uses part of secondary storage as an extension of RAM, so programs larger than RAM can run. Only the pages needed now are in RAM; others are held on disk, and swapped in when needed.
When a page is needed that is not in RAM, a page fault occurs. The page is loaded from disk, and if RAM is full another page is swapped out.
Disk thrashing
If RAM is too small for the programs running, the OS spends most of its time swapping pages between RAM and disk instead of running programs. The computer becomes very slow. It can be reduced by adding more RAM or running fewer programs.
Comparison
| Paging | Segmentation | |
|---|---|---|
| Block size | fixed | variable |
| Division based on | physical memory | logical parts |
Common mistake: saying virtual memory is the hard disk. It is the use of disk space as if it were RAM. Also, paging blocks have a fixed size and segments have variable sizes.
Exam tip: explain virtual memory as swapping pages between RAM and secondary storage, and link thrashing to too little RAM, with a result that the computer slows down.
Check yourself
What is the difference between a page and a frame?
A page is a fixed-size block of a program; a frame is a block of RAM of the same size that holds a page.
What is disk thrashing?
When the system spends most of its time swapping pages between RAM and disk, so little useful work is done.
Give one difference between paging and segmentation.
Pages are all the same size, whereas segments vary in size to match the logical parts of a program.