0
$\begingroup$

The below-given lines are written in the book of the operating system (Page -307), details of the book are as follows:

OPERATING SYSTEM PRINCIPLES, 7TH ED., Abraham Silberschatz, Peter Baer Galvin, Greg Gagne. Wiley India Pvt. Limited, Nov 27, 2006

"A lazy swapper never swaps a page into memory unless that page will be needed. In the context of a demand-paging system, the use of the term swapper is technically incorrect. A swapper manipulates entire processes, whereas a pager is concerned with the individual pages of a process. We thus use pager, rather than swapper, in connection with demand paging."

Can you please kindly explain how a swapper manipulates entire processes?

$\endgroup$
2
$\begingroup$

One processor can only execute one process at a time. Today processors have several cores/threads in the same chip. Even if you have one core in your processor, the OS needs to swap the entire execution context (the entire process) in order to execute something else and it needs to do it efficiently to prevent latency in rendering.

What is called a swapper here is the part of the OS which does this execution context swap.

For example, on Linux the task_struct is a structure containing the process information. When Linux calls schedule(), the function will determine what process should run next and on what core based on priority. This priority is probably stored in the task_struct.

Paging swapping and process swapping is quite different. Every process has its own page tables.

For example, on x86-32 systems (32 bits systems) you are going to have 2 levels of page table: PDT (page directory table) and PT (page table). The CR3 register contains the address of the bottom of the PDT and the PDT contains addresses of the PTs. Since on most systems pages are 4KB, one PDT, which has itself a size of 4KB, can map up to 4MB. If your PDT is full, it will thus map the whole address space available (4GB) (https://wiki.osdev.org/Paging). A virtual address is separated in chunks representing the offset in respective tables. For virtual address 0xABCDEF12, on x86-32 you have 0xF12 as the offset in the physical page, ten bits as the offset in the PT and the most significant 10 bits as the offset in PDT.

When a process swap occurs, all page tables are kept in memory (RAM) and the actual physical pages are kept where they are. The only thing which changes is the CR3 register which will point to a new address. At this address is another PDT for the new process taking place. The TLB (page cache) needs to be flushed as well.

Page swapping is completely different. It involves taking pages that are on a swap partition on the hard-disk and loading it to RAM. Linux for example works with interrupts to manage page swapping. The CPU was made to throw an exception (or interrupt) and launch a certain interrupt handler when a page fault occurs (when the CPU tried to execute or read data that isn't in RAM). For x86-32, the interrupt number of a page fault is 0xE (https://wiki.osdev.org/Exceptions). When a page fault occurs, the CPU will look in its IDT for the address of the handler (code present in RAM that will run to manage the page fault). The address of the IDT is stored in a special register called the LIDT.

A page fault can occur when the PT contains a page which has the present bit not set. This means that the page is mapped but not present in RAM at the moment. The page fault handler registered by the OS will thus take the page on the hard-disk and load the page to memory and return.

With virtual addressing, each process is considered to have a full 4GB of virtual address space. One core has only one process running and this process has access to the whole 4GB of virtual address space. The same virtual address can point to 2 different addresses in physical RAM. The OS uses this to map the process space to different positions in RAM so that processes don't interlace each other. If physical RAM is full (which is rare nowadays), the OS will place some pages not currently in use onto the hard-disk. I often installed Linux on computers without swap space. I simply considered that RAM would never be full and thus I would not need it.

As an example, Linux uses ELF files as executable files. Very often, the ELF file's code will be mapped to virtual address 0x400000. Every process' code is mapped to the same virtual address. Thus the OS needs to setup its page tables accordingly and needs to determine what process is going to get what part of the available RAM. It is done so that all processes don't interfere with each other or the kernel.

Processes are swapped according to a timer which raises an interrupt after some time. The timer is programmed to a certain time, counts down to 0, then raises a certain interrupt number. The OS will register a handler to that interrupt number which will call a scheduler which will determine what process goes next and reschedule the timer etc. When processes are swapped all registers are stored onto the process' stack and a pointer to the stack is kept in a structure somewhere (https://en.wikipedia.org/wiki/Context_switch).

$\endgroup$
1
$\begingroup$

Swapping a process means to move all of its memory onto the hard disk. Paging means to move a single page of memory onto the hard disk. So, the difference is the granularity at which data is moved between main memory and persistent storage (the hard disk). Over time, the term "swap" has been used more broadly and not everyone uses the word "swap" in a way that is consistent with that bright-line distinction.

See https://en.wikipedia.org/wiki/Paging#History and https://en.wikipedia.org/wiki/Paging#Unix_and_Unix-like_systems.

$\endgroup$
  • $\begingroup$ In today's words process swapping doesn't mean to swap the whole process to hard-disk. Maybe in 1960 but not today. Today never a process is swapped completely to the hard-disk. $\endgroup$ – user123 Nov 19 '20 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.