Fall 2017 :: CSE 306
Applications
- f
Virtual Memory
in
OS Design
Nima Honarmand
Applications of Virtual Memory in OS Design Nima Honarmand Fall - - PowerPoint PPT Presentation
Fall 2017 :: CSE 306 Applications of Virtual Memory in OS Design Nima Honarmand Fall 2017 :: CSE 306 Introduction Virtual memory is a powerful level of indirection Indirection: IMO, the most powerful concept in Computer Science
Fall 2017 :: CSE 306
in
Nima Honarmand
Fall 2017 :: CSE 306
Computer Science
“All problems in computer science can be solved by another level of indirection”
many levels of indirection
as virtual memory?
Fall 2017 :: CSE 306
Fall 2017 :: CSE 306
information about Process Address Space Layout
information
→ We need a side data structure to store this info
Fall 2017 :: CSE 306
Fall 2017 :: CSE 306
heap and stack
Virtual Address Space (4GB)
0xffffffff
code
(from /bin/ls)
code
(from libc.so)
heap stk Kernel
0xc000000
Fall 2017 :: CSE 306
vm_area_struct, or VMA
Fall 2017 :: CSE 306
vma (/bin/ls) start end next vma anonymous vma (libc.so)
mm_struct (one per process)
0xffffffff
code
(from /bin/ls)
code
(from libc.so)
heap stk
0xc000000
Kernel
vma anonymous
Fall 2017 :: CSE 306
OS, or deletes existing mappings, or changes protection flags, etc.
itself
Fall 2017 :: CSE 306
prot, int flags, int fd, off_t offset)
anywhere is okay)
Fall 2017 :: CSE 306
hence “anonymous”
appropriate location
a big-enough hole in the address space
in VMA for the OS to know how to treat this VMA
Fall 2017 :: CSE 306
address space
and offset fields
read()/write()
regular load/store instructions to access that part
Fall 2017 :: CSE 306
copies that part of the file to the region, and returns region’s beginning address
.data from binary files and shared libraries
Fall 2017 :: CSE 306
allocate at addr
files)
mem-mapping it?
Fall 2017 :: CSE 306
char *p; int fd; struct stat sb; fd = open("/my_file.txt", O_RDONLY); fstat(fd, &sb); p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); close (fd); for (len = 0; len < sb.st_size; len++) putchar(p[len]); munmap(p, sb.st_size);
Open the file (in the right mode) Get its size (and other stats) mmap the part that you need (doesn’t have to be all of it) Can close the file descriptor now. Unmap the file from address space
In reality, there should be quite a bit of error checking after each system call.
Access the mmaped memory.
Fall 2017 :: CSE 306
truncating/splitting existing VMAs
size_t new_size, int flags, ... /* void *new_address */)
new address (depends on flags)
memory addresses
Fall 2017 :: CSE 306
address space, create a VMA and keep mmap() arguments in it
address in that range
be allowed
Fall 2017 :: CSE 306
and adds it to page table at the accessed address
copies corresponding file data to it → OS only allocates physical memory on-demand, when a valid virtual address is accessed for the first time
Fall 2017 :: CSE 306
necessary (OS; not you!)
immediately to disk
when system load is low
Fall 2017 :: CSE 306
really necessary
them (or parts of them) in a given run
written again soon
Fall 2017 :: CSE 306
Fall 2017 :: CSE 306
Fall 2017 :: CSE 306
file in the address space with little cost
access (on-demand)
address spaces
Fall 2017 :: CSE 306
process; identical except for the return value
int pid = fork(); if (pid == 0) { // child code } else if (pid > 0) { // parent code } else { // error }
Fall 2017 :: CSE 306
space and copy each page
binary without using any of these pages
parent’s pages may never be touched → Being lazy is better!
Fall 2017 :: CSE 306
COW bit
data
Fall 2017 :: CSE 306
they decrease in virtual address order
Stack “bottom”: 0x13000 0x12600 0x12300 0x11900 Exceeds stack page main() foo() bar() End of stack: 0x12000
Fall 2017 :: CSE 306
Guard Page
growth?
address space below the stack and marks it as not-accessible
page frame
OS knows it needs to grow the stack
the guard page up (i.e., lower address)
Stack Page New Guard Page New Stack Page main() foo() bar() Page Fault! Allocate a new stack page
Fall 2017 :: CSE 306
process address spaces
processes
issues when accessing such memory
Fall 2017 :: CSE 306
the address space of each process
further
kernel, etc.
and (maybe) the code to access that data into the process address space
Fall 2017 :: CSE 306
multiple physical computers processes
power than can be had on a single computer
a network
using a Partitioned Global Address Space (PGAS) model
Fall 2017 :: CSE 306
mapped to a physical page that resides on another computer
to get a copy of that page from the machine where it resides
Fall 2017 :: CSE 306
more virtual memory than available physical memory
more than available physical memory
memory page out to disk and use its space
this victim?
systems
Fall 2017 :: CSE 306
with many application is OS design
concepts such as “virtual”, “abstract”, etc.), ask yourself “what else can I do with it?”