lecture 2: operating system structure - ku ittcheechul/courses/eecs678/f16/slides/17...lecture 2:...
TRANSCRIPT
![Page 1: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/1.jpg)
Memory Management
1
Disclaimer: some slides are adopted from book authors’ slides with permission
![Page 2: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/2.jpg)
Recap: Page Replacement
• On a page fault
– Step 1: allocate a free page frame
• If there’s a free frame, use it
• If there’s no free frame, choose a victim frame and evict it to disk (if necessary) swap-out
– Step 2: bring the stored page on disk (if necessary)
– Step 3: update the PTE (mapping and valid bit)
– Step 4: restart the instruction
2
![Page 3: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/3.jpg)
Recap: Page Replacement Policies
• FIFO– Evict the oldest page first. Pros: fair; Cons: can throw out frequently
used pages
• Optimal– Evict the page that will not be used for the longest period
– Pros: optimal; Cons: you need to know the future
• Random– Randomly choose a page. Pros: simple. TLB commonly uses this
method; Cons: unpredictable
• LRU– Look at the past history, choose the one that has not been used for the
longest period. Pros: good performance; Cons: complex, requires h/w support
3
![Page 4: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/4.jpg)
Recap: Page Table Entry (PTE)
• PTE format (architecture specific)
– Valid bit (V): whether the page is in memory
– Modify bit (M): whether the page is modified
– Reference bit (R): whether the page is accessed
– Protection bits(P): readable, writable, executable
4
Page Frame NoV M R P
20 bits21 1 1
![Page 5: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/5.jpg)
Recap: Clock Algorithm
• Key idea
– Replace an oldpage, not the oldest page
• Algorithm– Step 1: advance the pointer
by one
– Step 2: check the reference bit of the page:1 Used recently. Clear the bit and go to Step 10 Not used recently. Selected victim. End.
5
![Page 6: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/6.jpg)
Quiz.
Reference E D H B D E D A E B E
Page #1 E E E
Page #2 D D
Page #3 H
Mark X for a fault X X X
6
• Complete the following with the FIFO replacement policy
![Page 7: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/7.jpg)
Quiz.
7
• Complete the following with the FIFO replacement policy
Reference E D H B D E D A E B E
Page #1 E E E B B B B A A A A
Page #2 D D D * E E E * B B
Page #3 H H H H D D D D E
Mark X for a fault X X X X X X X X X
![Page 8: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/8.jpg)
Concepts to Learn
• Memory-mapped I/O
• Copy-on-Write (COW)
• Memory allocator
8
![Page 9: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/9.jpg)
Recap: Program Binary Sharing
• Multiple instances of the same program
– E.g., 10 bash shells9
Bash text
Physical memoryBash #1 Bash #2
![Page 10: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/10.jpg)
Memory Mapped I/O
• Idea: map a file on disk onto the memory space
10
![Page 11: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/11.jpg)
Memory Mapped I/O
• Benefits: you don’t need to use read()/write() system calls, just directly access data in the file via memory instructions
• How it works?
– Just like demand paging of an executable file
– What about writes?• Mark the modified (M) bit in the PTE
• Write back the modified pages back to the original file
11
![Page 12: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/12.jpg)
Copy-on-Write (COW)
• Fork() creates a copy of a parent process
– Copy the entire pages on new page frames?
• If the parent uses 1GB memory, then a fork() call would take a while
• Then, suppose you immediately call exec(). Was it of any use to copy the 1GB of parent process’s memory?
12
![Page 13: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/13.jpg)
Copy-on-Write
• Better way: copy the page table of the parent
– Page table is much smaller (so copy is faster)
– Both parent and child point to the exactly same physical page frames
13
parent child
![Page 14: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/14.jpg)
Copy-on-Write
• What happens when the parent/child reads?
• What happens when the parent/child writes?
– Trouble!!!
14
parent child
![Page 15: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/15.jpg)
Page Table Entry (PTE)
• PTE format (architecture specific)
– Valid bit (V): whether the page is in memory
– Modify bit (M): whether the page is modified
– Reference bit (R): whether the page is accessed
– Protection bits(P): readable, writable, executable
15
Page Frame NoV M R P
20 bits21 1 1
![Page 16: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/16.jpg)
Copy-on-Write
• All pages are marked as read-only
16
parent child
RORORO
Page tbl
RORORO
Page tbl
![Page 17: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/17.jpg)
Copy-on-Write• Up on a write, a page fault occurs and the OS copies
the page on a new frame and maps to it with R/W protection setting
17
parent child
RORORW
Page tbl
RORORO
Page tbl
RW
![Page 18: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/18.jpg)
Kernel/User Virtual Memory
• Kernel memory
– Kernel code, data
– Identical to all address spaces
– Fixed 1-1 mapping of physical memory
• User memory
– Process code, data, heap, stack,...
– Unique to each address space
– On-demand mapping (page fault)
18
Kernel
User
0xFFFFFFFF
0xC0000000
0x00000000
![Page 19: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/19.jpg)
User-level Memory Allocation
• When a process actuallyallocate a memory from thekernel?
– On a page fault
– Allocate a page (e.g., 4KB)
• What does malloc() do?
– Doesn’t physically allocate pages
– Manage a process’s heap
– Variable size objects in heap
19
![Page 20: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/20.jpg)
Kernel-level Memory Allocation
• Page-level allocator (low-level)
– Page granularity (4K)
– Buddy allocator
• Other kernel-memory allocators
– Support fine-grained allocations
– Slab, kmalloc, vmalloc allocators
20
![Page 21: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/21.jpg)
Kernel-Level Memory Allocators
21
Kernel code
kmallocArbitrary size objects
SLAB allocatorMultiple fixed-sized object caches
Page allocator (buddy)Allocate power of two pages: 4K, 8K, 16K, …
vmalloc(large) non-physically contiguous memory
![Page 22: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/22.jpg)
Buddy Allocator
• Linux’s page-level allocator
• Allocate power of two number of pages: 1, 2, 4, 8, … pages.
• Request rounded up to next highest power of 2
• When smaller allocation needed than is available, current chunk split into two buddies of next-lower power of 2
• Quickly expand/shrink across the lists
22
4KB
8KB
16KB
32KB
![Page 23: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/23.jpg)
Buddy Allocator
• Example
– Assume 256KB chunk available, kernel requests 21KB
23
32 A
32 Free
64 Free
128Free
32 Free
64 Free
128Free
32 Free
64 Free
128Free
64 Free
128Free
128Free
256Free
![Page 24: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/24.jpg)
Buddy Allocator
• Example
– Free A
24
32 A
32 Free
64 Free
128Free
32 Free
64 Free
128Free
32 Free
64 Free
128Free
64 Free
128Free
128Free
256Free
![Page 25: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/25.jpg)
Virtual Memory Summary
• MMU and address translation
• Paging
• Demand paging
• Copy-on-write
• Page replacement
25
![Page 26: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/26.jpg)
Quiz: Address Translation
26
2nd level offset
8 bits 8 bits
1st level
8 bits
Virtual address format (24bits)
Frame # V
4 bits 3
Unused
1
Page table entry (8bit)
Addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +A +B +C +D +E +F
0x000 31
0x010
0x020 41
..
0x100 00 01 01 00 01
..
0x200
Page-table base address = 0x100
Vaddr: 0x0703FEPaddr: 0x3FE
Vaddr: 0x072370Paddr: ???
Vaddr: 0x082370Paddr: ???
![Page 27: Lecture 2: Operating System Structure - KU ITTCheechul/courses/eecs678/F16/slides/17...Lecture 2: Operating System Structure Author heechul Created Date 10/27/2016 12:26:03 PM](https://reader033.vdocuments.net/reader033/viewer/2022060413/5f11abaeab67d21fed64a413/html5/thumbnails/27.jpg)
Quiz: Address Translation
27
2nd level offset
8 bits 8 bits
1st level
8 bits
Virtual address format (24bits)
Frame # V
4 bits 3
Unused
1
Page table entry (8bit)
Addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +A +B +C +D +E +F
0x000 31
0x010
0x020 41
..
0x100 00 01 01 00 01
..
0x200
Page-table base address = 0x100
Vaddr: 0x0703FEPaddr: 0x3FE
Vaddr: 0x072370Paddr: 0x470
Vaddr: 0x082370Paddr: invalid