Download - Operating Systems ECE344 Ashvin Goel ECE University of Toronto Processes and Virtual Memory
Operating SystemsECE344
Ashvin GoelECE
University of Toronto
Processes and Virtual Memory
2
Outline
Interaction of processes with virtual memory system
Page sharing and memory-mapped files
3
Processes and Virtual Memory
We have seen that the virtual memory system implements three main abstractionso Address space, physical memory management, swapo OS implement the virtual memory hierarchy using these
abstractions
The rest of the OS invokes the virtual memory system when address space of the current process changes:o Process creation (fork)o Process execution (execv)o Process termination (exit)o Context switcho Memory allocation or deallocation (sbrk, stack)
4
Fork
Fork creates a new address spaceo Copies parent’s address space structure
All regions will have the same location, sizes and permissions as the parent’s structure
o Creates a new page table All valid pages must be copied from the parent to the child
– Allocate memory frames for text, data, heap, stack regions– Copy contents from parent's regions– Create valid PTE for each allocated frame– If parent’s page is in swap, create a shared swap page
5
Execv
Execv starts running a new processo Destroy old address space structure
Free all mapped page frames in the page table Free all swap regions used by the process Free space used by address space structure, page table Must check reference counts in coremap
– If frames are shared, free frame only when reference count is zero
o Create new address space structure Set size text and code regions according to executable Set size of heap and stack according to defaults Initialize a new page table with all invalid entries
6
Exit, Context Switch
Exit terminates a processo The same as destroying old address space in execv
Context switch o Need to change the currently active page tableo Hardware managed TLB (x86)
Change the page table register, flush TLBo Software managed TLB (os161)
Flush TLB, TLB misses are handled in s/w
7
Memory Allocation or Deallocation
When stack or heap grows, page is requested, OS allocates a new page, thread continues
Virtual address space
Page 1Page 0
Unallocated pages
Page 2^20 - 1 Stack
Text
Data
Heap
8
Heap Region
User-level malloc implementation manages heap memory using bitmap, list, etc.o Malloc services allocation requests using a free poolo When the pool runs out of memory, malloc requests more
heap memory from the OS by using the sbrk() system call
sbrk(increment)o This system call increase the heap size by increment bytes o It increases the heap region associated with address spaceo Initializes appropriate PTEso Returns the old size of the heap
9
Stack Region
The stack contains local variables, parameters, and return values
Stack operations involve basic machine instructions such as push and popo Unlike heap, we cannot use a system call to grow the stack
OS takes advantage of page faults to grow stacko When faulting address is *close* to stack, extend the stack
region, run the standard page fault handler code How do you know whether address is "close" to stack?
Why is stack grown automatically, while the heap requires a system call?
10
Page Sharing
Processes do not share any memoryo Each process has its own address space (page table)o Strong isolation, but slow communication via system calls
Threads share all memoryo They have the same address space (same page table)o Fast communication via memory accesses, but poor isolation
(bugs in one thread affect can affect all threads)
Paging allows processes to share memory at page granularityo Multiple pages can share memory when they are mapped to
the same frame
11
Page Sharing
Benefitso Allows fast communication via shared memory pageso Provides good isolation, only share what is needed
E.g., the child processes of a web server program may wish to only share some data (e.g., the web server data cache)
Applicationso Sharing text regionso Copy-on-write pageso Memory-mapped files
12
Sharing Text Regions
Multiple instances of a program or dynamically loaded libraries can share all the pages in the text regiono Sharing is achieved by mapping pages to the same frameso Note that
Must update all pages (i.e., PTE) when frame is evicted
13
Sharing Text Regions
Data (rw)Text (rx)
Stack (rw)Thread 1addressspace
Thread 2addressspace
Thread 1page table
Physical memory
Thread 2page table
Shared text
pages
14
Copy-on-Write (COW) Page Sharing
Fork system call copies parent's address space to child
Copying all pages is expensive
However, processes can't notice difference between copying and sharing unless pages are modified
With copy-on-write (COW), child shares pages with parent until the pages are modified
15
Copy-on-Write Implementation
Initialize new page table for child on fork()
However, page table shares parent's page frames, i.e., page table is a copy of the parent's page table, but the pages are not copied
Mark all writeable page table entries in both page tables temporarily as “read-only”o These pages are called COW pages
When a process modifies a COW page, this will cause a TLB "read-only“ protection faulto We can take advantage of this protection fault to make a copy
of a page on a write
16
Copy-on-Write Implementation
On protection fault:o Allocate a new frameo Copy the original frame to the new frameo Remap page in address space from old frame to new frameo Make page in address space writable and update TLB entryo Resume execution
Evicting shared pages:o Must update all shared pages to point to swap when frame is
evicted
17
Memory-Mapped Files
Memory-mapped files allow accessing and sharing a file using a memory interfaceo Threads read and write files using memory load/store
instructions rather than read/write system calls
The mmap system call maps a file at a given offset contiguously within an address spaceo mmap(addr, length, prot, flags, fd, offset)
addr: virtual address of mapped region length: length of mapped region prot: protection flags (writeable, readable, executable) fd: descriptor of file offset: offset in file
o After mmap, accessing addr + N refers to offset + N in file fd
18
Example of Memory-Mapped File
Any part of the file can be mapped to an arbitrary region in the address space
File
Virtual address space
Page 1Page 0
Unallocated pages
Page 2^20 - 1
Text
Data
Heap
Mappedfile
Stack
19
Memory-Mapped Files
Memory-mapped file operationo File data is loaded in memory on page fault (demand paging)o When dirty page is evicted, page frame is written to fileo Essentially, file is used for backing store instead of swap area
20
Shared Memory
When threads map the same file region, they can share file data by reading and writing to the mapped memory region
Page table entries can have different protectiono E.g., one thread has read, another write enabled
Memory can be mapped at same or different virtual address in each processo Different virtual addresses allow more flexibility, but shared
memory pointers become invalid
21
Summary
The virtual memory system is invoked when address space of the current process changeso TLB and page fault handlingo Process creation, process termination, context switcho Memory allocation or deallocationo Loading dynamic libraries
Paging enables sharing pages between processeso Reduces memory footprint because pages can be sharedo Enables copy-on-write, memory-mapped files, and shared
memory applications
22
Think Time
Since processes can share memory with mmap(), what is the difference between processes and threads?
Does the OS use page tables for its memory?
Do the different processors in an SMP use a single page table or different page tables?
Why is it hard to program with shared data structures that are mapped at different virtual addresses in two processes?
Why is it more efficient to access a memory-mapped file than using a read or write system call?