OS Mod4
OS Mod4
● Main memory, cache and CPU registers in the processors are the only
storage spaces that CPU can access directly.
● The program and data must be brought into the memory from the
disk, for the process to run. Each process has a separate memory
space and must access only this range of legal addresses. Protection
of memory is required to ensure correct operation. This prevention
is provided by hardware implementation.
● Two registers are used - a base register and a limit register.
The base register holds the smallest legal physical memory
address; the limit register specifies the size of the range.
● For example, if the base register holds 300040 and limit
register is 120900, then the program can legally access all
addresses from 300040 through 420940 (inclusive).
Contd..
● The base and limit registers can be loaded only by the operating system, which uses a
special privileged instruction. Since privileged instructions can be executed only in kernel
mode
Multistep Processing of a User Program
Address Binding
3. Execution Time - If a program can be moved around in memory during the course of its
execution, then binding must be delayed until execution time.
Logical Versus Physical Address Space
● The address generated by the CPU is a logical address, whereas the
memory address where programs are actually stored is a physical address.
● The set of all logical addresses used by a program composes the logical
address space, and the set of all corresponding physical addresses
composes the physical address space.
● The run time mapping of logical to physical addresses is handled by the
memory- management unit (MMU).
Logical vs Physical Address Space
Advantages:
1. An unused routine is never loaded.
2. Useful when large amounts of code are needed to handle infrequently
occurring cases.
3. Although the total program-size may be large, the portion that is used
(and hence loaded) may be much smaller.
Dynamic Linking and Shared Libraries
● With static linking library modules get fully included in executable modules, wasting both
disk space and main memory usage, because every program that included a certain routine from
the library would have to have their own copy of that routine linked into their executable code.
● With dynamic linking, however, only a stub is linked into the executable module, containing
references to the actual library module linked in at run time.
● The stub is a small piece of code used to locate the appropriate memory-resident library-
routine.
● This method saves disk space, because the library routines do not need to be fully included
in the executable modules, only the stubs.
● An added benefit of dynamically linked libraries (DLLs, also known as shared libraries on
UNIX systems) involves easy upgrades and updates.
Shared libraries
● A library may be replaced by a new version, and all programs that reference
the library will automatically use the new one.
● Version info. is included in both program & library so that programs won't
accidentally execute incompatible versions.
Swapping
Assume that the user process is 10 MB in size and the backing store is a standard hard
disk with a transfer rate of 40 MB per second.
The actual transfer of the 10-MB process to or from main memory takes 10000
KB/40000 KB per second = 1/4 second
= 250 milliseconds.
Assuming that no head seeks are necessary, and assuming an average latency of 8
milliseconds, the swap time is 258 milliseconds. Since we must both swap out and swap
in, the total swap time is about 516 milliseconds.
Contiguous Memory Allocation
OS OS OS OS
process 5 process 5 process 5 process 5
process 9 process 9
process
process 8
10
process 2 process 2 process 2 process 2
– Operating system maintains information about:
a) allocated partitions
b) free partitions (hole)
Dynamic Storage-Allocation Problem
First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
Examples
In the first fit, the partition is allocated which is first sufficient from the top of Main Memory.
Example :
1 212 2
2 417 5
3 112 2
The size of a page is power of 2, varying between 512 bytes and 16MB per page.
If the size of logical address space is 2 m, and a page size is 2n , then high-order
m-n bits of a logical address designate the page number, and the n low-order bits
designate the page offset.
To calculate physical
address =
((frame*pagesize)+offset)
1. TLB Hit
2. TLB Miss
Effective Access Time
• Associative Lookup = ε time unit
• Assume memory cycle time is 1 microsecond
• Hit ration – percentage of times that a page number is found in the
associative registers; ration related to number of associative registers.
• Hit ratio = α
• Effective Access Time (EAT)
EAT = (1 + ε) α + (2 + ε)(1 – α)
=2+ε–α
Problem
What will be the EAT if hit ratio is 70%, time for TLB is 30ns and access to main memory
is 90ns?
P = 70% = 70/100 = 0.7
Hit memory time = 30ns + 90ns = 120ns
Miss memory time = 30ns + 90ns + 90ns = 210ns
Therefore,
EAT = P x Hit + (1-P) x Miss =
0.7 x 120 + 0.3 x 210 =840 + 63.0 =147 ns
Memory Protection
Reentrant code is non self modifying code it never changes during execution. Two or more processes can
execute the same code at the same time
Two-Level Page-Table Scheme
Two-Level Paging Example
Loading the entire program into memory results in loading the executable code for
all options, regardless of whether an option is ultimately selected by the user or not.
An alternative strategy is to load pages only as they are needed. This technique is
known as Demand paging and is commonly used in virtual memory systems.
● Similar to paging system with swapping (diagram on right)
● Page is needed ⇒ reference to it
● invalid reference ⇒ abort
● not-in-memory ⇒ bring to memory
● Lazy swapper – never swaps a page into memory unless page will
be needed
● Swapper that deals with pages is a pager
Basic Concepts
● With swapping, pager guesses which pages will be used
before swapping out again
● Instead, pager brings in only those pages into memory
● How to determine that set of pages?
● Need new MMU functionality to implement demand
paging
● If pages needed are already memory resident
● No difference from non demand-paging
● If page needed and not memory resident
● Need to detect and load the page into memory from
storage
4 Without changing program behavior
4
Valid-Invalid Bit
● With each page table entry a valid–invalid bit is associated
(v ⇒ in-memory – memory resident, i ⇒ not-in-memory)
● Initially valid–invalid bit is set to i on all entries
● Example of a page table snapshot:
Page Table When Some Pages Are Not in Main Memory
Page Fault
● If there is a reference to a page, first reference to that
page will trap to operating system:
page fault
1. Operating system looks at another table to decide:
● Invalid reference ⇒ abort
● Just not in memory
2. Find free frame
3. Swap page into frame via scheduled disk operation
4. Reset tables to indicate page now in memory
Set validation bit = v
5. Restart the instruction that caused the page fault
Steps in Handling a Page Fault
Aspects of Demand Paging
● Extreme case – start process with no pages in memory
● OS sets instruction pointer to first instruction of process,
non-memory-resident -> page fault
● And for every other process pages on first access
● Pure demand paging
● Actually, a given instruction could access multiple pages ->
multiple page faults
● Consider fetch and decode of instruction which adds 2
numbers from memory and stores result back to memory
● Pain decreased because of locality of reference
● Hardware support needed for demand paging
● Page table with valid / invalid bit
● Secondary memory (swap device with swap space)
● Instruction restart
Instruction Restart
● Consider an instruction that could access several different locations
● block move
2. Using temporary registers to hold the values of the overwritten locations. If there
is page fault then old values are written back into memory before trap. This way
data is not lost and operation can be restarted.
Performance of Demand Paging
Demand paging can significantly affect the performance of a computer
system.
Let p be the probability of a page fault (0 <= p <= 1). We would expect p to be
close to zero-that is, we would expect to have only a few page faults. The
effective access time is = (1 - p) x ma + p x page fault time.
3. Bring the desired page into the (newly) free frame; update the page and
frame tables
4. Continue the process by restarting the instruction that caused the trap.
Note now potentially 2 page transfers for page fault – increasing EAT
Page Replacement
Page and Frame Replacement Algorithms
15 page
faults
● Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
● Adding more frames can cause more page faults!
4 Belady’s Anomaly
● How to track ages of pages?
● Just use a FIFO queue
FIFO Illustrating Belady’s Anomaly
Optimal Algorithm
● Replace page that will not be used for longest period of time
● 9 is optimal for the example
● How do you know this?
● Can’t read the future
● Used for measuring how well your algorithm performs
Least Recently Used (LRU) Algorithm
● Use past knowledge rather than future
● Replace page that has not been used in the most amount of
time
● Associate time of last use with each page
● Program 2
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
data[i,j] = 0;
Other Issues – I/O interlock
● I/O Interlock – Pages must
sometimes be locked into
memory
● Consider I/O - Pages that are
used for copying a file from a
device must be locked from
being selected for eviction by a
page replacement algorithm
● Pinning of pages to lock into
memory
Operating System Examples
● Windows
● Solaris
Windows
● Uses demand paging with clustering. Clustering brings in
pages surrounding the faulting page
● Processes are assigned working set minimum and
working set maximum
● Working set minimum is the minimum number of pages
the process is guaranteed to have in memory
● A process may be assigned as many pages up to its
working set maximum
● When the amount of free memory in the system falls
below a threshold, automatic working set trimming is
performed to restore the amount of free memory
● Working set trimming removes pages from processes that
have pages in excess of their working set minimum
Solaris
● Maintains a list of free pages to assign faulting processes
● Lotsfree – threshold parameter (amount of free memory)
to begin paging
● Desfree – threshold parameter to increasing paging
● Minfree – threshold parameter to being swapping
● Paging is performed by pageout process
● Pageout scans pages using modified clock algorithm
● Scanrate is the rate at which pages are scanned. This
ranges from slowscan to fastscan
● Pageout is called more frequently depending upon the
amount of free memory available
● Priority paging gives priority to process code pages
Solaris 2 Page Scanner
End of Chapter 9