slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CMSC421: Principles of Operating Systems PowerPoint Presentation
Download Presentation
CMSC421: Principles of Operating Systems

Loading in 2 Seconds...

play fullscreen
1 / 50

CMSC421: Principles of Operating Systems - PowerPoint PPT Presentation


  • 109 Views
  • Uploaded on

CMSC421: Principles of Operating Systems. Nilanjan Banerjee. Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/. Principles of Operating Systems

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CMSC421: Principles of Operating Systems' - yachi


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

CMSC421: Principles of Operating Systems

Nilanjan Banerjee

Assistant Professor, University of Maryland

Baltimore County

nilanb@umbc.edu

http://www.csee.umbc.edu/~nilanb/teaching/421/

Principles of Operating Systems

Acknowledgments: Some of the slides are adapted from Prof. Mark Corner and Prof. Emery Berger’s OS course at Umass Amherst

announcements
Announcements
  • Project 2 progress report due one week from Nov. 9th
coalescing
Coalescing

kernel

stack

brk

malloc(200)

allocated

free

Text region

3

jobs of a memory allocator like malloc
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
memory layout of the heap
Memory layout of the heap

this linked list can be ordered in different ways

http://gee.cs.oswego.edu/dl/html/malloc.html

selecting the free block to allocate fragmentation
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
classical algorithms
Classical Algorithms
  • First-fit
    • find first chunk of desired size
classical algorithms1
Classical Algorithms
  • Best-fit
    • find chunk that fits best
      • Minimizes wasted space
classical algorithms2
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
a day in the life of a page
A Day in the Life of a Page
  • Allocate some memory

0x40001000

0x40001040 → 0x4000104F

virtual memory layout

char * x = new char[16];

a day in the life of a page1
A Day in the Life of a Page
  • Update page tables

0x40001000

0x40001040 → 0x4000104F

virtual memory layout

physical

memory layout

char * x = new char[16];

a day in the life of a page2
A Day in the Life of a Page
  • Write contents – dirty page

0x40001000

0x40001040 → 0x4000104F

virtual memory layout

physical

memory layout

strcpy(x, “hello”);

a day in the life of a page3
A Day in the Life of a Page
  • Other processes fill up memory…

virtual memory layout

physical

memory layout

a day in the life of a page4
A Day in the Life of a Page
  • Forcing our page to be evicted (paged out)

virtual memory layout

physical

memory layout

swapspace(disk)

a day in the life of a page5
A Day in the Life of a Page
  • Now page nonresident & protected

virtual memory layout

physical

memory layout

swapspace(disk)

a day in the life of a page6
A Day in the Life of a Page
  • Touch page – swap it in

0x40001000

0x40001040 → 0x4000104F

virtual memory layout

physical

memory layout

swapspace(disk)

y[0] = x[0];

a day in the life of a page7
A Day in the Life of a Page
  • Touch page – swap it in

0x40001000

0x40001040 → 0x4000104F

virtual memory layout

physical

memory layout

swapspace(disk)

y[0] = x[0];

talked about malloc what about physical frame mgmt
Talked about malloc? What about physical frame mgmt?
  • malloc works in virtual memory (works in user space)
    • Manages free blocks
    • Allocates virtual address on the heap
  • Remember the OS still has to manage physical frames
    • The problem that the OS faces with physical frame allocation is the similar to malloc
    • Manage physical frames that all processes in the system requests.
  • Difference with malloc
    • Has to work across all processes
      • Each process perceives 4GB of space, but in actuality there is only 4GB of physical memory space
tasks of the os physical page management unit
Tasks of the OS physical page management unit
  • Allocate new pages to applications
    • OS do this lazily
    • malloc call would usually return immediately
    • OS allocates a new physical only when the process reads/writes to the page
    • Similar to the Copy-on-Write policy for fork()
  • In the event that all physical frames are taken
    • OS needs to evict pages
      • Take page from main memory and store it on swap space
    • Needs a policy for evicting pages
page replacement policy for demand paging
Page replacement policy for Demand Paging?

What is the optimal page replacement policy?

optimal page replacement policy
Optimal Page Replacement policy
  • Find the page that is going to used farthest into the future
    • Evict the page from main memory to swap space
    • Allocate the freed page to the new process
    • Problems: it is impossible to predict the future
  • Approximation is LRU (least recently used page)
    • Find the page that is least recently used and evict it
    • Remember this has to be super-fast
    • What would be techniques to implement this in the kernel?
implementing exact lru
Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A, B, C, B, C, C, D

implementing exact lru1
Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A

1

A, B, C, B, C, C, D

implementing exact lru2
Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A

1

B

2

A, B, C, B, C, C, D

implementing exact lru3
Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A

1

B

2

C

3

A, B, C, B, C, C, D

implementing exact lru4
Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A

1

B

4

C

3

A, B, C, B, C, C, D

implementing exact lru5
Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A

1

B

4

C

5

A, B, C, B, C, C, D

implementing exact lru6
Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A

1

B

4

C

6

A, B, C, B, C, C, D

implementing exact lru7

LRU page

Implementing Exact LRU
  • On each reference, time stamp page
  • When we need to evict: select oldest page= least-recently used

A, B, C, B, C, C D,

A

1

D

7

B

4

C

6

A

0

How should we implement this?

implementing exact lru8
Implementing Exact LRU
  • Could keep pages in order– optimizes eviction
    • Priority queue:update = O(log n), eviction = O(log n)
  • Optimize for common case!
    • Common case: hits, not misses
    • Hash table:update = O(1), eviction = O(n)
cost of maintaining exact lru
Cost of Maintaining Exact LRU
  • Hash tables: too expensive
    • On every reference:
      • Compute hash of page address
      • Update time stamp
cost of maintaining exact lru1
Cost of Maintaining Exact LRU
  • Alternative: doubly-linked list
    • Move items to front when referenced
    • LRU items at end of list
    • Still too expensive
      • 4-6 pointer updates per reference
  • Can we do better?
hardware support and approximate lru linux kernel
Hardware Support and approximate LRU (Linux Kernel)
  • Maintain reference bits for every page
    • On each access, set reference bit to 1
    • Page replacement algorithm periodically resets reference bits

A, B, C, B, C, C, D

A

1

B

1

C

1

hardware support
Hardware Support
  • Maintain reference bits for every page
    • On each access, set reference bit to 1
    • Page replacement algorithm periodically resets reference bits

A, B, C, B, C, C, D

A

0

B

0

C

0

reset reference bits

hardware support1
Hardware Support
  • Maintain reference bits for every page
    • On each access, set reference bit to 1
    • Page replacement algorithm periodically resets reference bits

A, B, C, B, C, C, D

A

0

B

1

C

0

hardware support2
Hardware Support
  • Maintain reference bits for every page
    • On each access, set reference bit to 1
    • Page replacement algorithm periodically resets reference bits

A, B, C, B, C, C, D

A

0

B

1

C

1

hardware support3
Hardware Support
  • Maintain reference bits for every page
    • On each access, set reference bit to 1
    • Page replacement algorithm periodically resets reference bits

A, B, C, B, C, C, D

A

0

B

1

C

1

hardware support4
Hardware Support
  • Maintain reference bits for every page
    • On each access, set reference bit to 1
    • Page replacement algorithm periodically resets reference bits
    • Evict page with reference bit = 0
  • Cost per miss = O(n)

A, B, C, B, C, C, D

D

1

B

1

C

1

A

0

most recently used mru
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case:

A, B, C, D, A, B, C, D, ...

size of available memory

most recently used mru1
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case: loop that exceeds RAM size

A

A, B, C, D, A, B, C, D, ...

size of available memory

most recently used mru2
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case: loop that exceeds RAM size

A

B

A, B, C, D, A, B, C, D, ...

size of available memory

most recently used mru3
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case: loop that exceeds RAM size

A

B

C

A, B, C, D, A, B, C, D, ...

size of available memory

most recently used mru4
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case: loop that exceeds RAM size

A

B

D

A, B, C, D, A, B, C, D, ...

size of available memory

most recently used mru5
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case: loop that exceeds RAM size

A

B

D

A, B, C, D, A, B, C, D, ...

size of available memory

most recently used mru6
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case: loop that exceeds RAM size

A

B

D

A, B, C, D, A, B, C, D, ...

size of available memory

most recently used mru7
Most-Recently Used (MRU)
  • Evict most-recently used page
  • Shines for LRU’s worst-case: loop that exceeds RAM size

A

B

D

A, B, C, D, A, B, C, D, ...

size of available memory

slide47
FIFO
  • First-in, first-out: evict oldest page
  • As competitive as LRU, butperforms miserably in practice!
    • Ignores locality
    • Suffers from Belady’s anomaly:
      • More memory can mean more paging!
    • LRU & similar algs. do not
      • Stack algorithms – more memory means ≥ hits
tricks with page tables sharing
Tricks with Page Tables: Sharing
  • Paging allows sharing of memory across processes
    • Reduces memory requirements
  • Shared stuff includes code, data
    • Code typically R/O
tricks with page tables cow
Tricks with Page Tables: COW
  • Copy on write (COW)
    • Just copy page tables
    • Make all pages read-only
  • What if process changes mem?
  • All processes are created this way!