Operating Systems ECE 344 Processes and Virtual Memory

  • Slides: 22
Download presentation
Operating Systems ECE 344 Processes and Virtual Memory Ashvin Goel ECE University of Toronto

Operating Systems ECE 344 Processes and Virtual Memory Ashvin Goel ECE University of Toronto

Outline q Interaction of processes with virtual memory system q Page sharing and memory-mapped

Outline q Interaction of processes with virtual memory system q Page sharing and memory-mapped files 2

Processes and Virtual Memory q We have seen that the virtual memory system in

Processes and Virtual Memory q We have seen that the virtual memory system in the OS implements three main abstractions Address space, physical memory management, swap o OS implement the virtual memory hierarchy using these abstractions o q The rest of the OS invokes the virtual memory system when address space of the current process changes: o o o Process creation (fork) Process execution (execv) Process termination (exit) Memory allocation or deallocation (sbrk, stack) Memory-mapped files (mmap) Context switch 3

Fork q Fork creates a new address space o Copies parent’s address space structure

Fork q Fork creates a new address space o 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 Inefficient. will make it more efficient (described later) 4

Execv q Execv starts running a new process o Destroy old address space structure

Execv q Execv starts running a new process o Destroy old address space structure § Free all pages in the page table § Free all swap regions used by the process § Free the 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 5

Exit, Context Switch q Exit terminates a process o q The same as destroying

Exit, Context Switch q Exit terminates a process o q The same as destroying old address space in execv Context switch Need to change the currently active page table o Hardware managed TLB (x 86) o § Change the page table register, flush TLB o Software managed TLB (MIPS, Sparc, etc. ) § Flush TLB, TLB misses are handled in s/w 6

Memory Allocation or Deallocation When stack or heap grows, page is requested, OS allocates

Memory Allocation or Deallocation When stack or heap grows, page is requested, OS allocates a new page, thread continues Page 2^20 - 1 Stack Unallocated pages q Heap Data Page 1 Page 0 Text Virtual address space 7

Heap Region q User-level malloc implementation manages heap memory using bitmap, list, etc. Malloc

Heap Region q User-level malloc implementation manages heap memory using bitmap, list, etc. Malloc services allocation requests using a free pool o When the pool runs out of memory, malloc requests more heap memory from the OS by using the sbrk() system call o q sbrk(increment) This system call increase the heap size by increment bytes o It increases the heap region associated with address space o Initializes appropriate PTEs o Returns the old size of the heap o 8

Stack Region q q The stack contains local variables, parameters, and return values Stack

Stack Region q q The stack contains local variables, parameters, and return values Stack operations involve basic machine instructions such as push and pop o q Unlike heap, we cannot use a system call to grow the stack OS takes advantage of page faults to grow stack o 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? q Why is stack grown automatically, while the heap requires a system call? 9

Page Sharing q Processes do not share any memory Each process has its own

Page Sharing q Processes do not share any memory Each process has its own address space (page table) o Strong isolation, but slow communication via system calls o q Threads share all memory 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) o q Paging allows processes to share memory at page granularity o Multiple pages can share memory when they are mapped to the same frame 10

Page Sharing q Benefits Allows fast communication via shared memory pages o Reduces physical

Page Sharing q Benefits Allows fast communication via shared memory pages o Reduces physical memory usage o Provides good isolation, only share what is needed o § E. g. , the child processes of a web server program may wish to only share some data (e. g. , the web server data cache) q Applications Sharing text regions o Copy-on-write (COW) pages o Memory-mapped files o 11

Sharing Text Regions q Multiple instances of a program or dynamically loaded libraries can

Sharing Text Regions q Multiple instances of a program or dynamically loaded libraries can share all the pages in the text region o q Sharing is achieved by mapping pages to the same frames Must update all pages (i. e. , PTE) when frame is evicted 12

Sharing Text Regions Physical memory Stack (rw) Process 1 address space Process 1 page

Sharing Text Regions Physical memory Stack (rw) Process 1 address space Process 1 page table Data (rw) Text (rx) Shared text pages Process 2 address space Process 2 page table 13

Copy-on-Write (COW) Page Sharing q q Copying all pages is expensive However, processes can't

Copy-on-Write (COW) Page Sharing q q 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 RW RO After first write to page q Fork system call copies parent's address space to child After fork() q RW RW 14

Copy-on-Write Implementation q q q Initialize new page table for child on fork() However,

Copy-on-Write Implementation q q q 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 q These pages are called COW pages When a process modifies a COW page, this will cause a TLB "read-only“ protection fault o We can take advantage of this protection fault to make a copy of a page on a write 15

Copy-on-Write Implementation q On protection fault (in either child or parent): o o o

Copy-on-Write Implementation q On protection fault (in either child or parent): o o o q Allocate a new frame Copy the original frame to the new frame Remap page in address space from old frame to new frame Make page in address space writable and update TLB entry Resume execution Evicting shared pages: o Must update all shared pages to point to swap when frame is evicted 16

Memory-Mapped Files q Memory-mapped files allow accessing and sharing a file using a memory

Memory-Mapped Files q Memory-mapped files allow accessing and sharing a file using a memory interface o q 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 space o 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 17

Example of Memory-Mapped File Any part of the file can be mapped to an

Example of Memory-Mapped File Any part of the file can be mapped to an arbitrary region in the address space Page 2^20 - 1 Unallocated pages q Stack Mapped file Heap Data Page 1 Page 0 File Text Virtual address space 18

Memory-Mapped Files q Memory-mapped file operation File data is loaded in memory on page

Memory-Mapped Files q Memory-mapped file operation File data is loaded in memory on page fault (demand paging) o When dirty page is evicted, page frame is written to file o Essentially, file is used for backing store instead of swap area o 19

Shared Memory q q When processes map the same file region, they can share

Shared Memory q q When processes 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 protection o q E. g. , one thread has read, another write enabled Memory can be mapped at same or different virtual address in each process o Different virtual addresses allow more flexibility, but shared memory pointers become invalid 20

Summary q The virtual memory system is invoked when address space of the current

Summary q The virtual memory system is invoked when address space of the current process changes Process creation, process termination, context switch o Page faults (demand paging) o Memory allocation or deallocation o Loading dynamic libraries o q Paging enables sharing pages between processes Reduces memory footprint because pages can be shared o Enables copy-on-write, memory-mapped files, and shared memory applications o 21

Think Time q q q Since processes can share memory with mmap(), what is

Think Time q q q 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 cores 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? 22