OPERATING
SYSTEM: CSET209
Virtual memory
CONTENT
Demand paging
Page fault
Dynamic Loading
S
VIRTUAL MEMORY
❑ Paging was used to avoid memory fragmentation by breaking process memory requirements
down into smaller pages and storing them non-contiguously in memory. But the entire process
still had to be stored in memory somewhere.
❑ Virtual Memory is a storage scheme that provides user an illusion of having a very big main
memory. This is done by treating a part of secondary memory as the main memory.
❑ Practically, most processes do not need all their pages all at once because:
❑ Arrays are often over-sized for worst-case scenarios, so only a small fraction of arrays are
actually used in practice.
❑ Certain features of a program are rarely used like some functions, or routines.
S
VIRTUAL MEMORY
❑ The ability to load only the portions of processes that were actually needed and only when
they are needed has several benefits:
❑ Programs could be written for a much larger address space than physically exists on the
computer
❑ Because programs are only using a fraction of their total address space, there is more
memory left for other programs, improving CPU utilization and system throughput.
❑ Less swapping required, thus speeding things up
Virtual Memory
S
VIRTUAL MEMORY EXAMPLE
S
VIRTUAL ADDRESS SPACE
❑ Virtual address space is programmers logical view of
program memory storage
❑ The actual physical layout is controlled by process page
table
❑ In figure, the big hole in the middle of the address space is
never used, unless stack or heap grow to fill the hole.
S
VIRTUAL MEMORY
❑ Virtual memory allows the sharing of files and memory by multiple processes
S
DEMAND PAGING
❑ When a process is swapped in, its pages are not swapped in all at once. They are swapped in
only when the process needs them (i.e., ON DEMAND)
❑ Known as lazy swapper
❑ In paging, when a process is swapped in, the pager only loads into memory those pages that it
expects the process to need.
❑ Pages that are not loaded in memory are marked as invalid in the page table using an invalid
bit.
❑ If the process accesses pages that are loaded in memory, the process runs exactly as if all the
pages are in memory.
❑ So when a page is needed that was not originally loaded up, then a page fault trap is
generated.
S
DEMAND PAGING
❑ Page table when some pages are not in
memory
Demand Paging
Demand Paging
(Page Fault)
Demand Paging
(Swapping and Page Replacement)
Demand Paging
(Swapping and Page Replacement)
Demand Paging
(Swapping and Page Replacement)
Demand Paging
(Dirty Bit)
Demand Paging
(Protection Bits)
S
HANDLING PAGE FAULT
❑ Steps to handle page fault:
1. The memory address requested is checked first to make sure it was a valid memory request
2. If the reference was invalid, the process is terminated. Otherwise, the page must be paged in.
3. A free frame is located
4. A disk operation is scheduled to bring in the necessary page from the disk. (This will block
the process on an I/O wait, allowing some other process to use CPU in the meantime)
5. When the I/O operation is complete, the process page table is updated with the new frame
number, and the invalid bit is changed to the valid bit.
6. The instruction that caused page fault must now be restarted from the beginning.
In extreme cases, no pages are swapped in for a process until they are requested by page faults.
This is known as pure demand paging.
S
HANDLING PAGE FAULT
S
PERFORMANCE OF DEMAND PAGING
❑ Demand paging can significantly affect the performance of a computer system.
❑ For most computer systems, the memory-access time, denoted as ma ranges from 10 to
200ns.
❑ As long as we have no page faults, the effective access time is equal to the memory access
time.
❑ If a page fault occurs, we must first read the relevant page from the disk and then access
the desired word.
❑ Let p be the probability of a page fault ( 0 <= p <= 1). We expect p to be close to 0, that is,
we would expect to have only a few page faults
❑ The effective access time is then
effective access time = (1 - p)*ma + p*page fault time
S
PERFORMANCE OF DEMAND PAGING
Page-fault service time includes:
1. Service the page-fault interrupt.
2. Read in the page.
3. Restart the process.
Example: Suppose a normal memory access requires 200 ns and servicing a page fault requires 8 ms (8,000,000
ns), with page fault rate of p, the effective time is:
= (1 - p) * (200) + p * 8000000
= 200 + 7,999,800 * p
It clearly depends heavily on p. Even if only one access in 1000 causes a page fault, the effective access time
drops from 200 ns to 8.2 microsec (a slowdown of a factor of 40 times).
In order to keep the slowdown less than 10%, the page fault must be less than 0.0000025, or one on 399,990
accesses.
THRASHING
Thrashing is when the page fault and swapping happens very frequently at a higher rate, and then the operating
system has to spend more time swapping these pages. This state in the operating system is known as thrashing.
Because of thrashing, the CPU utilization is going to be reduced or negligible.
DYNAMIC LOADING
The operating system loads a library of functions during the execution of various programs. As the programs are
processed, files are brought into the required memories. There are two types of loading processes: static and
dynamic.
Static Loading: Static loading is the process of loading the complete program into the main memory before it is
executed.
In dynamic loading, a routine is not loaded until it is invoked. All of the routines are stored on disk in
a reloadable load format. The main advantages of dynamic loading are that new routines are never loaded. This
loading is useful when a huge amount of code is required to handle it efficiently.
Static Loading Dynamic Loading
Static loading refers to loading the whole program into the main Dynamic loading refers to the process of loading a program into
memory before executing the program. the main memory on demand.
It is only performed in structured programming languages such as It happens in OOPs languages such as C++, Java, and others.
C.
Static loading links and compiles the entire program without the All modules are loaded dynamically. The developer references all
need for additional software. of these, and the rest of the job is completed at execution time.
The linker joins the object program and other object modules to The linking process occurs dynamically in a relocatable form.
form a single static-loading program. Data is only loaded into memory when the program requires it.
Unlimited data and the program are loaded into memory to begin In run time, data and information are loaded bit by bit.
execution.
When static loading is used, static linking is used as well. When dynamic loading is used, dynamic linking is used as well.
Once the code is loaded into memory, it can be run or not. Only when it is necessary is an execution carried out.
It has a faster processing time because no files are changed The processing speed of dynamic loading is slower because the
during the process. files are uploaded at processing time.
Thank you