Docsity

Memory Management - Operating Systems - Lecture Notes, Study notes of Operating Systems

Partial preview of the text

Download Memory Management - Operating Systems - Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

So far, we have taken an application's view of the world. From now on, we're studying the operating system's view. Accordingly, we will consider the problems the OS has in managing use of various resources. First case study: memory. Shifting gears Tuesday, October 26, 2010 4:26 PM Docsity.com

The process by which memory is allocated, shared, and released.

Memory Management: File descriptors: describe how to communicate with devices

Process/thread descriptors: describe how to run a process.

Semaphores and mutexes: describe the locations of shared memory.

 File cache (later): describes what is on disk. ○ Internal tables  private: for this application only  shared (e.g., between threads)  copy-on-write (when forking) ○ Application memory The OS consumes memory for several purposes: How does the OS keep this straight? There are descriptors, that maintain memory state, and that describe how memory should be used. Deep issue Memory Management Wednesday, October 27, 2004 4:54 AM Docsity.com

Researchers can chew up terabytes of disk doing one experiment (e.g., weather forecasting) "Important" results should be saved for perusal by other researchers. This is a petascale data storage problem. This week I've been working on the problems of "Big Data" archiving prefix scale kilo 10 3 mega 10 6 tera 10 9 peta 10 12 exa 10 15 A cost-value problem: 10 cents per gigabyte/month; $1200/terabyte/year in the average cloud. This is the cost of storing the results for one experiment in some cases. Reliable, fast-access online storage on disk is expensive! So, we need to store results, and we can't An aside: near-line storage Thursday, October 25, 2012 10:50 AM Docsity.com

So, we need to store results, and we can't afford it! linear access cheap! When a researcher is done with an experiment, it is archived on tape ("near- line storage"). Put onto fast-access disk, overnight. Email when ready. When someone wants to see the results, they "check out" the data from near-line storage After a proscribed time, the online version times out and is deleted. One solution: near-line storage Robotic tape libraries: several tape drives, a cabinet full of tapes, and automatic loading. A petascale device. As data is checked in, it's streamed into a "write" drive, which robotically inserts new tapes as needed. Data that's checked in is immutable ; once checked in, the only thing we'll eventually do with it is to delete it when no longer needed. As data is checked out, it's read off of a How near-line storage is accomplished: Docsity.com

○ Physical: what is the real memory used for? ○ Logical: how do processes use memory? ○ Virtual: using disk to simulate extra memory. Three kinds of memory management exposed in the operating system: ○ Register allocations (controlled by compilers). ○ Cache (controlled by hardware) Alas, the OS has no control over: ○ A cache is for speed. Idea: keep the semantics the same, increase speed of processing.

The execution model of a serial segment of code is a polite fiction; the cache makes it extremely complex. If your program is "in cache", it runs at GHZ. even though physical memory can only be accessed at MHZ. What we control, and what we do not... Tuesday, October 26, 2010 12:19 PM Docsity.com

Logical address within the process. Page : a unit of memory that a process needs. Every page has a ○  Physical address : where it is in the machine Frame : a unit of memory that the computer contains. Every frame has a ○ Segment : a group of logically contiguous pages with a particular function within a process. ○  Page descriptor: properties of a process page.  Frame descriptor: where frame is allocated.  Segment descriptor: properties of a segment. Descriptor: a piece of memory (of some kind) that describes the properties of another piece of memory ○ We need a new language to understand how an operating system handles memory allocation A descriptor is an in-memory representation of things that actually are part of processor state. Nomenclature Thursday, October 28, 2004 6:17 AM Docsity.com

 Fragmentation exists in physical memory Fragmentation does not exist in mapped memory.  Internal fragmentation: ^ Fragmentation exists in mapped memory. Fragmentation may or may not exist in physical memory.  External fragmentation: Logical and physical memory Wednesday, October 27, 2004 4:54 AM Docsity.com

Fork: creates two copies of a process Loads private memory to logical pages. Links shared memory with private memory. Creates (one-page) stack and heap. Modifies the Process Control Block (PCB) to know where the logical pages are. Releases control to the new process. Execl: replaces one copy with another image Subroutine calling automatically expands and contracts stack space. malloc automatically expands heap space (but never compacts it). While running Recover all logical pages Put status code in PCB (in memory!) On exit Retrieve status code from PCB Deallocate PCB On reaping: The lifecycle of logical memory Tuesday, October 26, 2010 12:41 PM Docsity.com

Format: a piece of memory that describes how resources are being used. Intent: manage use and reuse of resources. Malloc's descriptors: describe how to allocate and de- allocate a small amount of memory! The concept of a descriptor Descriptors Tuesday, October 26, 2010 12:49 PM Docsity.com

In operating systems, the semantics of a thing are often much simpler than its implementation. foo = malloc(N); // give me N bytes of memory free(foo); // I don't need it anymore! The semantics of malloc and free are very straightforward: However, the implementation of malloc and free are quite involved, because of speed requirements. An intentional over-simplification Tuesday, October 26, 2010 12:51 PM Docsity.com

move its descriptor to the used list pass a pointer back to you if there is a descriptor of a block big enough in the free list, create a storage descriptor for the new block put the descriptor in the used list hand a pointer to the block back to you. else ask the operating system for more memory When you call malloc, find the block's descriptor in the used list move that descriptor to the free list When you call free: Naïve semantics of malloc Anything that is "freed" can be used as the return value of a subsequent malloc. So malloc only calls sbrk (to add another page) if the descriptors in the free list are not sufficient. Effectively: Naïve semantics of malloc Tuesday, October 26, 2010 12:56 PM Docsity.com

Naïve malloc model: one free list, one used list Tuesday, October 26, 2010 4:58 PM Docsity.com

Actually, malloc doesn't do things this way. Reason: speed is everything. O(1) malloc O(1) free Design requirements: But what it does is semantically equivalent to this! But speed is everything But speed is everything Tuesday, October 26, 2010 1:03 PM Docsity.com

Never allocate anything other than blocks with a power of two of bytes. When a process asks for n bytes, actually allocate the next higher power of two of bytes instead. Keep a free list for every power of two. Don't keep a used list: maintain a simple correspondence between actual addresses and descriptors. Malloc's actual algorithm: the buddy system The head of the free list (for a given power of two) always has the memory you want. Unlink at head is O(1). The functional relationship between logical memory and descriptor is also O(1) Why? The buddy system Tuesday, October 26, 2010 1:04 PM Docsity.com