How is #PF error related to the swapping? How does the operating system use it?
When a page is swapped out to disk, the P
bit in the relevant page table entry
is set to zero, because the page is not present in physical memory anymore.
Accessing addresses on this page results in a page fault exception, which enables
the operating system to load the page contents into memory.