1.61k likes | 2.58k Views
UNIT III STORAGE MANAGEMENT. UNIT III STORAGE MANAGEMENT Main Memory-Contiguous Memory Allocation, Segmentation, Paging, 32 and 64 bit architecture Examples; Virtual Memory- Demand Paging, Page Replacement, Allocation, Thrashing; Allocating Kernel Memory, OS Examples.
E N D
UNIT III STORAGE MANAGEMENT Main Memory-Contiguous Memory Allocation, Segmentation, Paging, 32 and 64 bit architecture Examples; Virtual Memory- Demand Paging, Page Replacement, Allocation, Thrashing; Allocating Kernel Memory, OS Examples.
Main Memory-Introduction • Background • Address Binding • Logical Versus Physical Address Space • Dynamic Loading • Dynamic Linking and Shared Libraries
Background • Memory is central to the operation of a modern computer system. • Memory consists of a large array of bytes, each with its own address. • The CPU fetches instructions from memory according to the value of the program counter. • These instructions may cause additional loading from and storing to specific memory addresses. • Program must be brought (from disk) into memory and placed within a process for it to be run • Main memory and registers are only storage built in processor, CPU can access directly • Memory unit only sees a stream of addresses + read requests, or address + data and write requests • Register access in one CPU clock (or less) • Main memory can take many cycles, causing a stall • Cachesits between main memory and CPU registers • Protection of memory required to ensure correct operation
Base and Limit Registers • A pair of baseandlimitregisters define the logical address space. • The base register holds the smallest legal physical memory address; • the limit register specifies the size of the range • For example, if the base register holds 300040 and the limit register is 120900, then the program can legally access all addresses from 300040 through 420939 (inclusive) • CPU must check every memory access generated in user mode to be sure it is between base and limit for that user
Hardware Address Protection • Protection of memory space is accomplished by comparing every address generated in user mode with the registers. • Any attempt by a user mode program to access operating-system memory or other users’ memory results in a trap to the operating system, which treats the attempt as a fatal error (Figure). • The base and limit registers can be loaded only by the operating system(done by special privileged instruction-> can be executed only in kernel mode)
Address Binding • Programs on disk, ready to execute are brought into memory from an input queue • Inconvenient to have first user process physical address always at 0000 • Further, addresses represented in different ways at different stages of a program’s life • Source code addresses usually symbolic • Compiled code addresses bind to relocatable addresses • i.e. “14 bytes from beginning of this module” • Linker or loader will bind relocatable addresses to absolute addresses • i.e. 74014 • Each binding maps one address space to another
Binding of Instructions and Data to Memory • Address binding of instructions and data to memory addresses can happen at three different stages • Compile time: If memory location known a priori, absolute codecan be generated; must recompile code if starting location changes • Load time: Must generate relocatable code if memory location is not known at compile time • Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another • Need hardware support for address maps (e.g., base and limitregisters)
Logical vs. Physical Address Space • An address generated by the CPU is commonly referred to as a logical address, • An address seen by the memory unit—that is, the one loaded into the memory-address register of the memory—is commonly referred to as a physical address • The concept of a logical address space that is bound to a separate physical address spaceis central to proper memory management • Logical address– generated by the CPU; also referred to as virtual address • Physical address– address seen by the memory unit • Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme • Logical address space is the set of all logical addresses generated by a program • Physical address space is the set of all physical addresses generated by a program
Memory-Management Unit (MMU) • The run-time mapping from virtual to physical addresses is done by a hardware device called the memory-management unit (MMU). • consider simple scheme where the value in the relocation register is added to every address generated by a user process at the time it is sent to memory(Fig) • Base register now called relocation register • MS-DOS on Intel 80x86 used 4 relocation registers • The user program deals with logical addresses; it never sees the real physical addresses • Execution-time binding occurs when reference is made to location in memory • Logical address bound to physical addresses
Dynamic Loading • Routine is not loaded until it is called • Better memory-space utilization; unused routine is never loaded • All routines kept on disk in relocatable load format • Useful when large amounts of code are needed to handle infrequently occurring cases • No special support from the operating system is required • Implemented through program design • OS can help by providing libraries to implement dynamic loading
Dynamic Linking and Shared Libraries • Dynamically linked libraries are system libraries that are linked to user programs when the programs are running. • Some operating systems support only static linking • Static linking – system libraries and program code combined by the loader into the binary program image • Dynamic linking –linking postponed until execution time • Small piece of code, stub, used to locate the appropriate memory-resident library routine • Stub replaces itself with the address of the routine, and executes the routine • Operating system checks if routine is in processes’ memory address • If not in address space, add to address space • Dynamic linking is particularly useful for libraries • System also known as shared libraries
Overlay • To enable a process to be larger than the amount of memory allocated to it,we can use overlay • The idea of overlay is to keep in memory only those instructions and data that are needed at any given time • Eg: consider two pass assembler • During pass1->construct symbol table • Then pass2 ->generate -> machine language code • Memory needed: pass1->70kb pass 2-> 80kb Symbol table-> 20kb --------------- 200kb->to load everything at once we need 200kb
If only 150 kb available? • We go for overlay concept • Create 2 overlay-> overlay A & overlay B • Overlay A->symbol table,common routine,pass1 • Overlay B-> symbol table,common routine,pass2 • Next we add an overlay driver(10kb) • When we finish pass1,we jump to overlay driver,which reads overlayB into memory, overwriting overlay A & then transfer ctrl to pass 2. • Therefore Overlay A needs only 120kb Overlay B needs only 130kb therefore we can run in 150kb
Diagram: overlay for two pass assembler Pass 2 Pass 1
Swapping • Standard Swapping • Swapping on Mobile Systems
Swapping • A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution • Swapping makes total physical memory space of processes can exceed physical memory, thus increasing the degree of multiprogramming in a system Standard Swapping • Standard swapping involves moving processes between main memory and a backing store • Backing store– fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images • Roll out, roll in– swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed • Major part of swap time is transfer time; • total transfer time is directly proportional to the amount of memory swapped • System maintains a ready queueof ready-to-run processes which have memory images on disk
Does the swapped out process need to swap back in to same physical addresses? • Depends on address binding method • Plus consider pending I/O to / from process memory space • Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows) • Swapping normally disabled • Started if more than threshold amount of memory allocated • Disabled again once memory demand reduced below threshold
Context Switch Time including Swapping • If next processes to be put on CPU is not in memory, need to swap out a process and swap in target process • context-switch time in such a swapping system is fairly high. • To get an idea of the context-switch time, let’s assume that the user process is 100 MB in size and the backing store is a standard hard disk with a transfer rate of 50 MB per second. The actual transfer of the 100-MB process to or from main memory takes 100 MB/50 MB per second = 2 seconds • The swap time is 200 milliseconds. • Since we must swap both out and in, the total swap time is about 4,000 milliseconds.ie 4s • Can reduce if reduce size of memory swapped – by knowing how much memory really being used • System calls to inform OS of memory use via request_memory() and release_memory()
Other constraints as well on swapping • Pending I/O – can’t swap out as I/O would occur to wrong process • Or always transfer I/O to kernel space, then to I/O device • Known as double buffering, adds overhead • Standard swapping not used in modern operating systems • But modified version common • Swap only when free memory extremely low
Swapping on Mobile Systems • Not typically supported • Flash memory based • Small amount of space • Limited number of write cycles • Poor throughput between flash memory and CPU on mobile platform • Instead use other methods to free memory if low • iOSasks apps to voluntarily relinquish allocated memory • Read-only data thrown out and reloaded from flash if needed • Failure to free can result in termination • Android terminates apps if low free memory, but first writes application state to flash for fast restart • Both Operating Systems support paging
Contiguous Memory Allocation • Memory Protection • Memory Allocation • Fragmentation
Main memory must support/accomodate both OS and user processes • Main memory is a Limited resource, must allocate efficiently • Contiguous allocation is one early method • Main memory usually divided into two partitions: • Resident operating system, usually held in low memory with interrupt vector • User processes then held in high memory • Each process contained in single contiguous section of memory
Memory Protection • Before discussing memory allocation, we must discuss the issue of memory protection, because operating system code & user code should not be modified-> ie protecting operating system from user process and protecting user process from one another • Relocation registers used to protect user processes from each other, and from changing operating-system code and data • Base register(relocation register) contains value of smallest physical address • Limit register contains range of logical addresses – each logical address must be less than the limit register • MMU maps logical address dynamically by adding the value in relocation register • When the CPU scheduler selects a process for execution, the dispatcher loads the relocation and limit registers with the correct values as part of the context switch. Because every address generated by a CPU is checked against these registers
The relocation-register allow the operating system’s size to change dynamically • Eg: operating system contains code and buffer space for device drivers. • If a device driver (or other operating-system service) is not commonly used, we do not want to keep the code and data in memory, as their code might be used for other purposes. Such code is sometimes called transient operating-system code; it comes and goes as needed.
Memory Allocation • One of the simplest methods for allocating memory is to divide memory into several fixed-sized partitions. • Each partition may contain exactly one process. • Thus, Degree of multiprogramming limited by number of partitions • In this multiplepartition method, when a partition is free, a process is selected from the input queue and is loaded into the free partition. • When the process terminates, the partition becomes available for another process. • In variable-partition scheme, the operating system keeps a table indicating which parts of memory are available and which are occupied. • Initially, all memory is available for user processes and is considered one large block of available memory, a hole. • Hole – block of available memory; holes of various size are scattered throughout memory
As processes enter the system, they are put into an input queue. • When a process arrives, it is allocated memory from a hole, large enough to accommodate it • When a process is allocated space, it is loaded into memory, and it can then compete for CPU time • Process exiting frees its partition, adjacent free partitions combined • Operating system maintains information about:a) allocated partitions b) free partitions (hole)
Dynamic Storage-Allocation Problem • This procedure is a particular instance of the general dynamic storage allocation problem. • How to satisfy a request of size n from a list of free holes? • The first-fit, best-fit,and worst-fit strategies are the ones most commonly used to select a free hole from the set of available holes • First-fit: Allocate the first hole that is big enough • Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size • Produces the smallest leftover hole • Worst-fit: Allocate the largest hole; must also search entire list • Produces the largest leftover hole
Fragmentation • Memory fragmentation can be internal as well as external • Eg:hole size 502 bytes, a process request 500 bytes, if we allocate mean, 2 bytes left over. • External Fragmentation– total memory space exists to satisfy a request, but it is not contiguous • Internal Fragmentation– allocated memory may be slightly larger than requested memory; this size difference in memory internal to a partition, but not being used • First fit analysis reveals that given N blocks allocated, 0.5 N blocks lost to fragmentation • 1/3 may be unusable -> 50-percent rule
Can Reduce external fragmentation by compaction • The goal is to Shuffle memory contents to place all free memory together in one large block • Compaction is possible only if relocation is dynamic, and is done at execution time • I/O problem • Latch job in memory while it is involved in I/O • Do I/O only into OS buffers • Another possible solution to the external-fragmentation problem is to permit the logical address space of the processes to be noncontiguous, • Thus allowing a process to be allocated physical memory wherever such memory is available. • Two complementary techniques achieve this solution: segmentation and paging. These techniques can also be combined.
Segmentation • Basic Method • Segmentation Hardware
Memory-management scheme that supports user view of memory • The user’s view of memory is not the same as the actual physical memory. • This is equally true of the programmer’s view of memory. • Dealing with memory in terms of its physical properties is inconvenient to both the operating system and the programmer. • What if the hardware could provide a memory mechanism that mapped the programmer’s view to the actual physical memory? • Segmentation provides such a mechanism.
Basic Method • When writing a program, a programmer thinks of it as a main program with a set of methods, procedures, or functions. • Segmentation is a memory-management scheme that supports this programmer view of memory. • A logical address space is a collection of segments. • A program is a collection of segments • A segment is a logical unit such as: main program, procedure ,function, method, object local variables, global variables, common block, stack symbol table,arrays
1 4 2 3 • Logical View of Segmentation 1 2 3 4 user space physical memory space
Each segment has a name and a length. • The addressesspecify both the segment name and the offset within the segment. • The programmer specifies each address by two quantities: a segment name and an offset. • For simple of implementation, segments are numbered -> referred by a segment number, rather than by a segment name. Thus, a logical address consists of a two tuple: <segment-number, offset> • Normally, when a program is compiled, the compiler automatically constructs segments reflecting the input program. C compiler might create separate segments for the following: 1. The code 2. Global variables 3. The heap, from which memory is allocated 4. The stacks used by each thread 5. The standard C library • Libraries that are linked in during compile time might be assigned separate segments. • The loader would take all these segments and assign them segment numbers.
Segmentation hardware/Architecture • Each entry in the segment table has a segment base and a segment limit. • The segment base contains the starting physical address where the segment resides in memory, • The segment limit specifies the length of the segment. • A logical address consists of two parts: a segment number, s, and an offset into that segment, d. • The segment number is used as an index to the segment table. • The offset d of the logical address must be between 0 and the segment limit. • When an offset is legal, it is added to the segment base to produce the address in physical memory of the desired byte. • Segment-table base register (STBR)points to the segment table’s location in memory • Segment-table length register (STLR)indicates number of segments used by a program; segment number s is legal if s < STLR
Paging • Paging is memory-management scheme that permits the physical address space of a process to be noncontiguous. • Basic Method • Hardware Support • Protection • Shared Pages • Structure of the Page Table • Hierarchical Paging • Hashed Page Tables • Inverted Page Tables
Basic Method • Physical memory broken into fixed-size of blocks called frames • Logical memory broken into blocks of same size called pages. • When a process is to be executed, its pages are loaded into any available memory frames from their source (a file system or the backing store). • The backing store is divided into fixed-sized blocks that are the same size as the memory frames or clusters of multiple frames. • Address Translation Scheme: • Every address generated by the CPU is divided into two parts: a page number (p) -used as an index into a page table-> contains the base address of each page in physical memory. a page offset (d)-combined with base address to define the physical memory address that is sent to the memory unit logical address space 2m and page size 2n
The page size (like the frame size) is defined by the hardware. • The size of a page is a power of 2, varying between 512 bytes and 1 GB per page, depending on the computer architecture • If the size of the logical address space is 2m, and a page size is 2n bytes, then the high-order m− n bits of a logical address designate the page number, and the n low-order bits designate the page offset • where p is an index into the page table and d is the displacement within the page.
Paging example: with 32 byte memory with 4 byte pages Offset 0 1 2 3 Page 0 0 1 2 3 Page 1 0 1 2 3 Page 2 0 1 2 3 Page 3 Logical address 3 (page 0, offset 3) maps to physical address 23 [= (5 × 4) +3].
This figure has page size of 4 bytes & physical memory of 32 byte(8 pages) • We show , user view of memory mapped into physical memory. • Logical address 0 is page 0, offset 0. Indexing into the page table, we find that page 0 is in frame 5. • Thus, logical address 0 maps to physical address 20 [= (5 × 4) +0]. • Logical address 3 (page 0, offset 3) maps to physical address 23 [= (5 × 4) +3]. • Logical address 4 is page 1, offset 0; according to the page table, page 1 is mapped to frame 6. Thus, logical address 4 maps to physical address 24 [= (6× 4) + 0]. • Logical address 13 maps to physical address 9. page 3,offset 1-> maps to physical address=9[(2x4)+1] In paging no external fragmentation , however may be internal fragmentation
When a process arrives in the system to be executed, its size, expressed in pages • Each page of the process needs one frame. • Thus, if the process requires n pages, at least n frames must be available in memory. • If n frames are available, they are allocated to this arriving process. • The first page of the process is loaded into one of the allocated frames, and the frame number is put in the page table for this process. • The next page is loaded into another frame, its frame number is put into the page table, and so on (Figure) • Since the operating system manages physical memory, it must be aware of the allocation details of physical memory—which frames are allocated, which frames are available, how many total frames there are, and so on. • This information is generally kept in a data structure called a frame table.