CISC 3595 — Operating System Spring, 2015 Chapter 3: Important Concepts (3/29/2015) 1 Memory from programmer’s perspective: you already know these: • Code (functions) and data are loaded into memory when the program is being executed. • Address of variables and functions (recall & operator): location of the variable/data and code in memory: they are different for different run (concept of relocation: the same program can be loaded to different part of program and is able to run regardlessly. ). • A process’s address space (recall the figure) made up of text/code , data , heap , and stack segment. It is not necessarily a consecutive address space. (Note: you can use gdb, or just cout statement in the code to examine the address of various variables, functions, ...). • Variable’s lifetime can be static (located in data segment), automatic (located in stack), and dynatmic (located in heap). • stack and heap can grow, and there is an user setting that specify the maximum size of stack or heap. (You can use command ulimit -a to view your current settings). • OS provides protection: typically a process cannot access a memory address within another process’s address space. (Segmentation fault or bus error are generated when a process tries to access memory using an illegal address). 2 Memory from programmer’s perspective: there are more to know: • One can create memory segment to be shared by multiple processes, in a way similar to the case where mutliple threads share global variables. It’s up to the programmer to ensure mutual exclusion in order to avoid race condtion. (The system call to create shared memory segment is shmget in Unix.) • Libraries: collections of commonly used functions and data, e.g., C++ standard library, STL (Standard Template Library), POSIX thread library. Library’s code (functions’ implementation) can be linked to the progam in different ways: – static library is linked to the program’s code at compilation’s linking stage. This means a larger executable code, taking up more disk and memory space. (static library has a suffix of .a). – shared library: a stub function (instead of the actual function) is linked to the program’s code, which will binds to the actual function’s code (might be shared by multiple programs) when being called at run time. (Note: command ldd can be used to show the shared libraries that a program needs). Demo of lab5, see Makefile for details. – dynamically loaded: programmer can load and unload infrequently used library to save storage space, and to allow switching library implementation while the program’s running. (plugin behavior). 3 What we know about the memory, RAM, the hardware • RAM stands for random access memory, meaning that you can access any random address of the memory (in contrast, magnetic tape is a sequential access media, you need to wind (forward or backword) the tape to a location in order to access a certain part of the tape). • The size of RAM is usually given in byte. RAM can be viewed as an array of bytes, with each byte’s index being its address.
• When accessing memory, CPU puts the physical address of the memory location to be accessed on the bus (more particular, the address lines) of the bus. 4 OS Memory Management: hide messy and ugly details, and take care of tedious works for programmers in managing main memory storage resource: • allocate and deallocate memory • keep track of memory usage • provide protection • provide abstraction • provide virtualization From no abstraction to swapping to paging, virtual memory to segmentation . Question 1: What is physical address space, or what does the bus see? (a)Suppose the system bus (or bus) has 64 address lines, then what are the physical address space? (b)On some computers, part of the above physical address space is used for I/O controller, so that when CPU reads/writes from/to a memory address belonging to an I/O controller to perform Input/Output. (Please read book, page 28 for more details). Question 2: What is a process’s address space referring to? 5 No abstraction Programmer (assembly langauge programs) sees the physical address, Mapping function from logic address to physical address is an identity function: physical_address_of (logic_address) = logic_address; • there is no abstraction: it’s up to the programmer to do tricks to run large program that cannot fit into memory. • static relocation: change all reference to memory at loading time 6 Swapping 2
Mapping function from logic address to physical address: physical_address_of (logic_address) = base + logic_address; // if base+logic_address > limit, a fault/software interrupt is generated, // 1. Save context of current process (registers) // 2. Trap to kernel mode, and interrupt number is used to look up // interrupt handler // 3. Call the corresponding handler (function that processes this type // of interrupt) // for this type of interrupt (or, trap, exception, fault), usually the process will // be aborted due to memory fault • OS allocates a chunk of consecutive memory to each process, i.e., the process’s address space is mapped to a contiguous chunk of physical memory. Two registers, base and limit , are used to store the starting address and the size of this memory chunk. • Protection: using limit and base , OS makes sure memory reference does not go beyond or below the range of address allocated to the process • Problem: Memory fragmentation (internal and external) Question: In swapping (or consecutive memory allocation) scheme, OS alloates memory in multiples of {\bf allocation unit}. If allocation unit is 2KB, a process asks for 5KB is allocated 6KB (leading to an internal fragment of 1KB). If allocation unit is 10KB, a process asks for 2KB is allocated 10KB (leading to an internal fragment of 8KB). What are the pros and cons of using small or large allocation unit? • Swapping or memory compaction (when there is no consectutive memory block big enough to allocate to new process): – memory compaction: to move used memory blocks to lower/higher ends of memory, in order to remove external fragmentation and create large consecutive unused memory blocks – swapping: pick a process to evict from memory, and save its image (address space) into disk (swapping area). 7 Paging or Virtual Memory Mapping from logic address to physical address: physical_address_of (logic_address) = MMU (logic_address) //i.e., the translation is done by Memory Management Unit, using page tables and TLB // the translation might lead to page fault, and disk read/write operation, and page // replacement algorithm to evict some page from RAM When the page table showing the page is not present in memory, MMU generates a software interrupt, page fault, which leads to // 1. Save context of current process (registers) // 2. Trap to kernel mode, and interrupt number is used to look up 3
// interrupt handler // 3. Call the corresponding handler (function that processes this type // of interrupt) // for this type of interrupt, page fault, kernel allocates a page frame (if none // available, starts page replacement algorithm), and loads the page from disk Ideas: • break down the physical address space (decided by number of address lines in bus) into page frames of equal size. • break down a process’s logic address space into pages , of same size as page frame • Each page (in a process’s address space) can be either loaded in memory (mapped onto a page frame in RAM), or not loaded (not laoded yet, or has been loaded but was later replaced on a page fault) and therefore is kept in disk (in a special partition, backing store in page 231). – Pages belonging to a process do not need to be mapped to adjacent page frames – Not all pages of a process needs to be loaded into memory for the process to be executed • Address translation by MMU: for each process, there is a page table (indexed using the page number in the logic address) where each entry stores: whether this page is present in memory; if so, the physical page frame number that this page is mapped to; Modification bit; Reference bit; and so on. • TLB, Translation Lookaside Buffer , caches the recently or mostly frequently used page table entries. It’s an associate memory, supporting parallel lookup of all entries. Question: Recall caching is used everywhere in computer system. List all different places (hardware or software) that caches are used? (a) the size of page (page frame) is a design parameter, which decides how many bits in the address is page offset , and how many bits is page number . Exercise: draw a 64 bits long address, and mark page offsetand page number parts of the address. Question 3: Why the size of page (and page frame) needs to be a power of 2? 4
Recommend
More recommend