Module 5
Module 5
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 9: Virtual Memory
Background
Demand Paging
Copy-on-Write
Page Replacement
Allocation of Frames
Thrashing
Memory-Mapped Files
Allocating Kernel Memory
Other Considerations
Operating-System Examples
Operating System Concepts – 9th Edition 9.2 Silberschatz, Galvin and Gagne ©2013
Objectives
Operating System Concepts – 9th Edition 9.3 Silberschatz, Galvin and Gagne ©2013
Background
Code needs to be in memory to execute, but entire program rarely
used
Error code, unusual routines, large data structures
Entire program code not needed at same time
Consider ability to execute partially-loaded program
Program no longer constrained by limits of physical memory
Each program takes less memory while running -> more
programs run at the same time
Increased CPU utilization and throughput with no increase
in response time or turnaround time
Less I/O needed to load or swap programs into memory ->
each user program runs faster
Operating System Concepts – 9th Edition 9.4 Silberschatz, Galvin and Gagne ©2013
Background (Cont.)
Virtual memory – separation of user logical memory from
physical memory
Only part of the program needs to be in memory for execution
Logical address space can therefore be much larger than physical
address space
Allows address spaces to be shared by several processes
Allows for more efficient process creation
More programs running concurrently
Less I/O needed to load or swap processes
Operating System Concepts – 9th Edition 9.5 Silberschatz, Galvin and Gagne ©2013
Background (Cont.)
Virtual address space – logical view of how process is stored
in memory
Usually start at address 0, contiguous addresses until end of
space
Meanwhile, physical memory organized in page frames
MMU must map logical to physical
Virtual memory can be implemented via:
Demand paging
Demand segmentation
Operating System Concepts – 9th Edition 9.6 Silberschatz, Galvin and Gagne ©2013
Virtual Memory That is Larger Than Physical Memory
Operating System Concepts – 9th Edition 9.7 Silberschatz, Galvin and Gagne ©2013
Virtual-address Space
Usually design logical address space for
stack to start at Max logical address and
grow “down” while heap grows “up”
Maximizes address space use
Unused address space between
the two is hole
No physical memory needed
until heap or stack grows to a
given new page
Enables sparse address spaces with
holes left for growth, dynamically linked
libraries, etc
System libraries shared via mapping into
virtual address space
Shared memory by mapping pages read-
write into virtual address space
Pages can be shared during fork(),
speeding process creation
Operating System Concepts – 9th Edition 9.8 Silberschatz, Galvin and Gagne ©2013
Shared Library Using Virtual Memory
Operating System Concepts – 9th Edition 9.9 Silberschatz, Galvin and Gagne ©2013
Demand Paging
Could bring entire process into memory
at load time
Or bring a page into memory only when
it is needed
Less I/O needed, no unnecessary
I/O
Less memory needed
Faster response
More users
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
Operating System Concepts – 9th Edition 9.10 Silberschatz, Galvin and Gagne ©2013
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
Without changing program behavior
Without programmer needing to change code
Operating System Concepts – 9th Edition 9.11 Silberschatz, Galvin and Gagne ©2013
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:
Operating System Concepts – 9th Edition 9.12 Silberschatz, Galvin and Gagne ©2013
Page Table When Some Pages Are Not in Main Memory
Operating System Concepts – 9th Edition 9.13 Silberschatz, Galvin and Gagne ©2013
Page Fault
Operating System Concepts – 9th Edition 9.14 Silberschatz, Galvin and Gagne ©2013
Steps in Handling a Page Fault
Operating System Concepts – 9th Edition 9.15 Silberschatz, Galvin and Gagne ©2013
Performance of Demand Paging
Stages in Demand Paging (worse case)
1. Trap to the operating system
2. Save the user registers and process state
3. Determine that the interrupt was a page fault
4. Check that the page reference was legal and determine the location of the page on the disk
5. Issue a read from the disk to a free frame:
1. Wait in a queue for this device until the read request is serviced
2. Wait for the device seek and/or latency time
3. Begin the transfer of the page to a free frame
6. While waiting, allocate the CPU to some other user
7. Receive an interrupt from the disk I/O subsystem (I/O completed)
8. Save the registers and process state for the other user
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory
11. Wait for the CPU to be allocated to this process again
12. Restore the user registers, process state, and new page table, and then resume the
interrupted instruction
Operating System Concepts – 9th Edition 9.16 Silberschatz, Galvin and Gagne ©2013
Performance of Demand Paging (Cont.)
Three major activities
Service the interrupt – careful coding means just several hundred instructions
needed
Read the page – lots of time
Restart the process – again just a small amount of time
Page Fault Rate 0 p 1
if p = 0 no page faults
if p = 1, every reference is a fault
Effective Access Time (EAT)
EAT = (1 – p) x memory access
+ p (page fault overhead
+ swap page out
+ swap page in )
Operating System Concepts – 9th Edition 9.17 Silberschatz, Galvin and Gagne ©2013
Demand Paging Example
Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds
EAT = (1 – p) x 200 + p (8 milliseconds)
= (1 – p x 200 + p x 8,000,000
= 200 + p x 7,999,800
If one access out of 1,000 causes a page fault, then
EAT = 8.2 microseconds.
This is a slowdown by a factor of 40!!
If want performance degradation < 10 percent
220 > 200 + 7,999,800 x p
20 > 7,999,800 x p
p < .0000025
< one page fault in every 400,000 memory accesses
Operating System Concepts – 9th Edition 9.18 Silberschatz, Galvin and Gagne ©2013
Demand Paging Optimizations
Swap space I/O faster than file system I/O even if on the same device
Swap allocated in larger chunks, less management needed than file
system
Copy entire process image to swap space at process load time
Then page in and out of swap space
Used in older BSD Unix
Demand page in from program binary on disk, but discard rather than paging
out when freeing frame
Used in Solaris and current BSD
Still need to write to swap space
Pages not associated with a file (like stack and heap) – anonymous
memory
Pages modified in memory but not yet written back to the file system
Mobile systems
Typically don’t support swapping
Instead, demand page from file system and reclaim read-only pages
(such as code)
Operating System Concepts – 9th Edition 9.19 Silberschatz, Galvin and Gagne ©2013
Copy-on-Write
Copy-on-Write (COW) allows both parent and child processes to initially
share the same pages in memory
If either process modifies a shared page, only then is the page copied
COW allows more efficient process creation as only modified pages are
copied
In general, free pages are allocated from a pool of zero-fill-on-demand
pages
Pool should always have free frames for fast demand page execution
Don’t want to have to free a frame as well as other processing on page
fault
Why zero-out a page before allocating it?
vfork() variation on fork() system call has parent suspend and child
using copy-on-write address space of parent
Designed to have child call exec()
Very efficient
Operating System Concepts – 9th Edition 9.20 Silberschatz, Galvin and Gagne ©2013
Before Process 1 Modifies Page C
Operating System Concepts – 9th Edition 9.21 Silberschatz, Galvin and Gagne ©2013
After Process 1 Modifies Page C
Operating System Concepts – 9th Edition 9.22 Silberschatz, Galvin and Gagne ©2013
What Happens if There is no Free Frame?
Operating System Concepts – 9th Edition 9.23 Silberschatz, Galvin and Gagne ©2013
Page Replacement
Operating System Concepts – 9th Edition 9.24 Silberschatz, Galvin and Gagne ©2013
Need For Page Replacement
Operating System Concepts – 9th Edition 9.25 Silberschatz, Galvin and Gagne ©2013
Basic Page Replacement
1. Find the location of the desired page on disk
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
Operating System Concepts – 9th Edition 9.26 Silberschatz, Galvin and Gagne ©2013
Page Replacement
Operating System Concepts – 9th Edition 9.27 Silberschatz, Galvin and Gagne ©2013
Page and Frame Replacement Algorithms
Operating System Concepts – 9th Edition 9.28 Silberschatz, Galvin and Gagne ©2013
Graph of Page Faults Versus The Number of Frames
Operating System Concepts – 9th Edition 9.29 Silberschatz, Galvin and Gagne ©2013
First-In-First-Out (FIFO) Algorithm
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
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!
Belady’s Anomaly
How to track ages of pages?
Just use a FIFO queue
Operating System Concepts – 9th Edition 9.30 Silberschatz, Galvin and Gagne ©2013
FIFO Illustrating Belady’s Anomaly
Operating System Concepts – 9th Edition 9.31 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 9.32 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 9.33 Silberschatz, Galvin and Gagne ©2013
LRU Algorithm (Cont.)
Counter implementation
Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter
When a page needs to be changed, look at the counters to find
smallest value
Search through table needed
Stack implementation
Keep a stack of page numbers in a double link form:
Page referenced:
move it to the top
requires 6 pointers to be changed
But each update more expensive
No search for replacement
LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
Operating System Concepts – 9th Edition 9.34 Silberschatz, Galvin and Gagne ©2013
Use Of A Stack to Record Most Recent Page References
Operating System Concepts – 9th Edition 9.35 Silberschatz, Galvin and Gagne ©2013
Thrashing
If a process does not have “enough” pages, the page-fault rate is
very high
Page fault to get page
Replace existing frame
But quickly need replaced frame back
This leads to:
Low CPU utilization
Operating system thinking that it needs to increase the
degree of multiprogramming
Another process added to the system
Operating System Concepts – 9th Edition 9.36 Silberschatz, Galvin and Gagne ©2013
Thrashing (Cont.)
Operating System Concepts – 9th Edition 9.37 Silberschatz, Galvin and Gagne ©2013
Demand Paging and Thrashing
Why does demand paging work?
Locality model
Process migrates from one locality to another
Localities may overlap
Operating System Concepts – 9th Edition 9.38 Silberschatz, Galvin and Gagne ©2013
Locality In A Memory-Reference Pattern
Operating System Concepts – 9th Edition 9.39 Silberschatz, Galvin and Gagne ©2013
End of Chapter 9
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 10: Mass-Storage
Systems
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 10: Mass-Storage Systems
Overview of Mass Storage Structure
Disk Structure
Disk Attachment
Disk Scheduling
Disk Management
Swap-Space Management
RAID Structure
Stable-Storage Implementation
Operating System Concepts – 9th Edition 9.42 Silberschatz, Galvin and Gagne ©2013
Objectives
To describe the physical structure of secondary storage devices
and its effects on the uses of the devices
To explain the performance characteristics of mass-storage
devices
To evaluate disk scheduling algorithms
To discuss operating-system services provided for mass storage,
including RAID
Operating System Concepts – 9th Edition 9.43 Silberschatz, Galvin and Gagne ©2013
Overview of Mass Storage Structure
Magnetic disks provide bulk of secondary storage of modern computers
Drives rotate at 60 to 250 times per second
Transfer rate is rate at which data flow between drive and computer
Positioning time (random-access time) is time to move disk arm to
desired cylinder (seek time) and time for desired sector to rotate
under the disk head (rotational latency)
Head crash results from disk head making contact with the disk
surface -- That’s bad
Disks can be removable
Drive attached to computer via I/O bus
Busses vary, including EIDE, ATA, SATA, USB, Fibre Channel,
SCSI, SAS, Firewire
Host controller in computer uses bus to talk to disk controller built
into drive or storage array
Operating System Concepts – 9th Edition 9.44 Silberschatz, Galvin and Gagne ©2013
Moving-head Disk Mechanism
Operating System Concepts – 9th Edition 9.45 Silberschatz, Galvin and Gagne ©2013
Hard Disks
Platters range from .85” to 14” (historically)
Commonly 3.5”, 2.5”, and 1.8”
Range from 30GB to 3TB per drive
Performance
Transfer Rate – theoretical – 6 Gb/sec
Effective Transfer Rate – real –
1Gb/sec
Seek time from 3ms to 12ms – 9ms
common for desktop drives
Average seek time measured or
calculated based on 1/3 of tracks
Latency based on spindle speed
1 / (RPM / 60) = 60 / RPM (From Wikipedia)
Average latency = ½ latency
Operating System Concepts – 9th Edition 9.46 Silberschatz, Galvin and Gagne ©2013
Hard Disk Performance
Access Latency = Average access time = average seek time +
average latency
For fastest disk 3ms + 2ms = 5ms
For slow disk 9ms + 5.56ms = 14.56ms
Average I/O time = average access time + (amount to transfer /
transfer rate) + controller overhead
For example to transfer a 4KB block on a 7200 RPM disk with a
5ms average seek time, 1Gb/sec transfer rate with a .1ms
controller overhead =
5ms + 4.17ms + 0.1ms + transfer time =
Transfer time = 4KB / 1Gb/s * 8Gb / GB * 1GB / 10242KB =
32 / (10242) = 0.031 ms
Average I/O time for 4KB block = 9.27ms + .031ms =
9.301ms
Operating System Concepts – 9th Edition 9.47 Silberschatz, Galvin and Gagne ©2013
The First Commercial Disk Drive
1956
IBM RAMDAC computer
included the IBM Model
350 disk storage system
5M (7 bit) characters
50 x 24” platters
Access time = < 1 second
Operating System Concepts – 9th Edition 9.48 Silberschatz, Galvin and Gagne ©2013
Disk Scheduling
The operating system is responsible for using hardware
efficiently — for the disk drives, this means having a fast
access time and disk bandwidth
Minimize seek time
Seek time seek distance
Disk bandwidth is the total number of bytes transferred,
divided by the total time between the first request for service
and the completion of the last transfer
Operating System Concepts – 9th Edition 9.49 Silberschatz, Galvin and Gagne ©2013
Disk Scheduling (Cont.)
There are many sources of disk I/O request
OS
System processes
Users processes
I/O request includes input or output mode, disk address, memory
address, number of sectors to transfer
OS maintains queue of requests, per disk or device
Idle disk can immediately work on I/O request, busy disk means
work must queue
Optimization algorithms only make sense when a queue exists
Operating System Concepts – 9th Edition 9.50 Silberschatz, Galvin and Gagne ©2013
Disk Scheduling (Cont.)
Note that drive controllers have small buffers and can manage a
queue of I/O requests (of varying “depth”)
Several algorithms exist to schedule the servicing of disk I/O
requests
The analysis is true for one or many platters
We illustrate scheduling algorithms with a request queue (0-199)
Operating System Concepts – 9th Edition 9.51 Silberschatz, Galvin and Gagne ©2013
FCFS
Illustration shows total head movement of 640 cylinders
Operating System Concepts – 9th Edition 9.52 Silberschatz, Galvin and Gagne ©2013
SSTF
Shortest Seek Time First selects the request with the minimum
seek time from the current head position
SSTF scheduling is a form of SJF scheduling; may cause
starvation of some requests
Illustration shows total head movement of 236 cylinders
Operating System Concepts – 9th Edition 9.53 Silberschatz, Galvin and Gagne ©2013
SCAN
The disk arm starts at one end of the disk, and moves toward the
other end, servicing requests until it gets to the other end of the
disk, where the head movement is reversed and servicing
continues.
SCAN algorithm Sometimes called the elevator algorithm
Illustration shows total head movement of 208 cylinders
But note that if requests are uniformly dense, largest density at
other end of disk and those wait the longest
Operating System Concepts – 9th Edition 9.54 Silberschatz, Galvin and Gagne ©2013
SCAN (Cont.)
Operating System Concepts – 9th Edition 9.55 Silberschatz, Galvin and Gagne ©2013
C-SCAN
Provides a more uniform wait time than SCAN
The head moves from one end of the disk to the other, servicing
requests as it goes
When it reaches the other end, however, it immediately
returns to the beginning of the disk, without servicing any
requests on the return trip
Treats the cylinders as a circular list that wraps around from the
last cylinder to the first one
Total number of cylinders?
Operating System Concepts – 9th Edition 9.56 Silberschatz, Galvin and Gagne ©2013
C-SCAN (Cont.)
Operating System Concepts – 9th Edition 9.57 Silberschatz, Galvin and Gagne ©2013
C-LOOK
LOOK a version of SCAN, C-LOOK a version of C-SCAN
Arm only goes as far as the last request in each direction,
then reverses direction immediately, without first going all
the way to the end of the disk
Total number of cylinders?
Operating System Concepts – 9th Edition 9.58 Silberschatz, Galvin and Gagne ©2013
C-LOOK (Cont.)
Operating System Concepts – 9th Edition 9.59 Silberschatz, Galvin and Gagne ©2013
Selecting a Disk-Scheduling Algorithm
SSTF is common and has a natural appeal
SCAN and C-SCAN perform better for systems that place a heavy load
on the disk
Less starvation
Performance depends on the number and types of requests
Requests for disk service can be influenced by the file-allocation method
And metadata layout
The disk-scheduling algorithm should be written as a separate module of
the operating system, allowing it to be replaced with a different algorithm
if necessary
Either SSTF or LOOK is a reasonable choice for the default algorithm
What about rotational latency?
Difficult for OS to calculate
How does disk-based queueing effect OS queue ordering efforts?
Operating System Concepts – 9th Edition 9.60 Silberschatz, Galvin and Gagne ©2013
Disk Management
Low-level formatting, or physical formatting — Dividing a disk into
sectors that the disk controller can read and write
Each sector can hold header information, plus data, plus error
correction code (ECC)
Usually 512 bytes of data but can be selectable
To use a disk to hold files, the operating system still needs to record its
own data structures on the disk
Partition the disk into one or more groups of cylinders, each treated
as a logical disk
Logical formatting or “making a file system”
To increase efficiency most file systems group blocks into clusters
Disk I/O done in blocks
File I/O done in clusters
Operating System Concepts – 9th Edition 9.61 Silberschatz, Galvin and Gagne ©2013
Disk Management (Cont.)
Raw disk access for apps that want to do their own block
management, keep OS out of the way (databases for example)
Boot block initializes system
The bootstrap is stored in ROM
Bootstrap loader program stored in boot blocks of boot
partition
Methods such as sector sparing used to handle bad blocks
Operating System Concepts – 9th Edition 9.62 Silberschatz, Galvin and Gagne ©2013
Booting from a Disk in Windows
Operating System Concepts – 9th Edition 9.63 Silberschatz, Galvin and Gagne ©2013
RAID Structure
RAID – redundant array of inexpensive disks
multiple disk drives provides reliability via redundancy
Increases the mean time to failure
Mean time to repair – exposure time when another failure could
cause data loss
Mean time to data loss based on above factors
If mirrored disks fail independently, consider disk with 1300,000
mean time to failure and 10 hour mean time to repair
Mean time to data loss is 100, 0002 / (2 ∗ 10) = 500 ∗ 106
hours, or 57,000 years!
Frequently combined with NVRAM to improve write performance
Several improvements in disk-use techniques involve the use of
multiple disks working cooperatively
Operating System Concepts – 9th Edition 9.64 Silberschatz, Galvin and Gagne ©2013
RAID (Cont.)
Disk striping uses a group of disks as one storage unit
RAID is arranged into six different levels
RAID schemes improve performance and improve the reliability
of the storage system by storing redundant data
Mirroring or shadowing (RAID 1) keeps duplicate of each
disk
Striped mirrors (RAID 1+0) or mirrored stripes (RAID 0+1)
provides high performance and high reliability
Block interleaved parity (RAID 4, 5, 6) uses much less
redundancy
RAID within a storage array can still fail if the array fails, so
automatic replication of the data between arrays is common
Frequently, a small number of hot-spare disks are left
unallocated, automatically replacing a failed disk and having data
rebuilt onto them
Operating System Concepts – 9th Edition 9.65 Silberschatz, Galvin and Gagne ©2013
RAID Levels
Operating System Concepts – 9th Edition 9.66 Silberschatz, Galvin and Gagne ©2013
RAID (0 + 1) and (1 + 0)
Operating System Concepts – 9th Edition 9.67 Silberschatz, Galvin and Gagne ©2013
Other Features
Regardless of where RAID implemented, other useful features
can be added
Snapshot is a view of file system before a set of changes take
place (i.e. at a point in time)
More in Ch 12
Replication is automatic duplication of writes between separate
sites
For redundancy and disaster recovery
Can be synchronous or asynchronous
Hot spare disk is unused, automatically used by RAID production
if a disk fails to replace the failed disk and rebuild the RAID set if
possible
Decreases mean time to repair
Operating System Concepts – 9th Edition 9.68 Silberschatz, Galvin and Gagne ©2013