Nilanjan Dey Chintan Bhatt Amira S. Ashour Suresh Chandra ...
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems...
-
Upload
leanna-jacka -
Category
Documents
-
view
221 -
download
3
Transcript of 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems...
![Page 1: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/1.jpg)
1
CMSC421: Principles of Operating Systems
Nilanjan Banerjee
Principles of Operating SystemsAcknowledgments: Some of the slides are adapted from Prof. Mark Corner
and Prof. Emery Berger’s OS course at Umass Amherst
Assistant Professor, University of MarylandBaltimore [email protected]
http://www.csee.umbc.edu/~nilanb/teaching/421/
![Page 2: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/2.jpg)
2
Announcements
• Project 2 progress report due on Nov. 9th• Homework 3 will be out soon (hopefully before the
end of this week)
![Page 3: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/3.jpg)
Quick Activity
• How much mem does a page table need?– 4kB pages, 32 bit address space– page table entry (PTE) uses 4 bytes
• 2^32/2^12*4=2^22 bytes=4MB– Is this a problem?– Isn’t this per process?– What about a 64 bit address space?
• Any ideas how to fix this?
![Page 4: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/4.jpg)
4
A
B
AB
Locality
• Most programs obey 90/10 “rule”– 90% of time spent
accessing 10% of memory
• Exploit this rule:– Only keep “live”
parts of process in memory
![Page 5: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/5.jpg)
Multi-Level Page Tables
• Use a multi-level page table
A A
A
Level 0 Table
Level 1 Table
Level 1 Table
![Page 6: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/6.jpg)
Quick Activity
• How much mem does a page table need?– 4kB pages, 32 bit address space– Two level page table– 20bits = 10 bits each level– page table entry (PTE) uses 4 bytes– Only first page of program is valid
• 2^10*4+2^10*4=2^13 bytes=8kB
• Isn’t this slow?
![Page 7: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/7.jpg)
7
Translation Lookaside Buffer (TLB)
• TLB: fast, fully associative memory– Caches page table entries– Stores page numbers (key) and frame (value) in
which they are stored• Assumption: locality of reference
– Locality in memory accesses =locality in address translation
• TLB sizes: 8 to 2048 entries– Powers of 2 simplifies translation
of virtual to physical addresses
![Page 8: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/8.jpg)
Linear Address in Linux
Uses a three-level paging strategy that works well for 32-bit and 64-bit systems
Linear address broken into four parts:
![Page 9: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/9.jpg)
Three-level Paging in Linux
![Page 10: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/10.jpg)
Inverted Page Tables
Maintain one global page table for all processes
![Page 11: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/11.jpg)
Swap Space
![Page 12: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/12.jpg)
12
Page table entry and page faults
![Page 13: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/13.jpg)
13
User-space memory allocation in the heap (malloc)
What happens int *arg = (int *)malloc(sizeof(int))
Programs ask memory manager– to allocate/free objects (or multiple pages)
Memory manager asks OS– to allocate/free pages (or multiple pages)
User Program
Allocator(libc)
OS
Pages (mmap,brk)
malloc, free
![Page 14: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/14.jpg)
14
User-space memory allocation in the heap (malloc)
kernel
Text region
stack
sbrk(0)
sbrk(100)brk
sbrk(100)brk
sbrk(100)brk
sbrk(0)
allocated
allocated
allocated
A demo?
![Page 15: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/15.jpg)
15
User-space memory allocation in the heap (malloc)
kernel
Text region
stack
sbrk(0)
brk
free
allocated
allocated
free
Memory allocator
keeps track of the free blocks
![Page 16: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/16.jpg)
16
Few Scenarios
kernel
Text region
stack
sbrk(0)
free
free
allocated malloc(200)Waste of resources
brkallocated
![Page 17: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/17.jpg)
17
Coalescing
kernel
Text region
stack
sbrk(0)
free
allocated malloc(200)Waste of resources
brk
![Page 18: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/18.jpg)
18
Jobs of a memory allocator like malloc
Manage heap space in virtual memory– Use sbrk to ask for more memory from OS
Coalescing Keep track of free blocks Merge them together when adjacent blocks
are free Malloc needs to be really fast
Decide which free block to allocate Lets take a look at the data structure that is
used for implementing malloc and free
![Page 19: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/19.jpg)
19
Memory layout of the heap
http://gee.cs.oswego.edu/dl/html/malloc.html
this linked list can be ordered in different ways
![Page 20: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/20.jpg)
20
Selecting the free block to allocate: Fragmentation
• Intuitively, fragmentation stems from “breaking” up heap into unusable spaces– More fragmentation = worse utilization
• External fragmentation– Wasted space outside allocated objects
• Internal fragmentation– Wasted space inside an object
![Page 21: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/21.jpg)
21
Classical Algorithms
• First-fit– find first chunk of desired size
![Page 22: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/22.jpg)
22
Classical Algorithms
• Best-fit– find chunk that fits best
• Minimizes wasted space
![Page 23: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/23.jpg)
23
Classical Algorithms
• Worst-fit– find chunk that fits worst– name is a misnomer!– keeps large holes around
• Reclaim space: coalesce free adjacent objects into one big object
![Page 24: 1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.](https://reader036.fdocuments.us/reader036/viewer/2022062421/56649c905503460f9494ab8d/html5/thumbnails/24.jpg)
24
In-class Discussion