TDC 311 Memory Management Dr. C.M.White Why do we need

advertisement
TDC 311
Memory Management
Dr. C.M.White
Why do we need memory management?
Protection - To protect one process from another; the OS from processes; resident
libraries from processes.
Sharing code - What if two different processes want to share a piece of code?
Logical organization - Allows for multiple, independent modules to reside in memory at
the same time.
Virtual memory - Processes are bigger than main memory. Memory management allows
a large process to partially occupy main memory.
Historical perspective of memory management
Bare Machine C No memory management. User has entire machine.
Resident Monitor - Memory divided into 2 sections - monitor and user. Protection hardware
consists of one fence register.
User memory does not start at location 0. OS starts at 0. User starts at location n. Are
programs compiled/assembled at location n? Probably not, so need relocation.
Static relocation - IBM 370 style.
Dynamic relocation - CDC 6600 style with relocation register.
Logical address space : What the user sees : 0 to max.
Physical address space : What the OS uses: R+0 to R+max for fence register value R.
Swapping - Memory still divided into monitor space and user space, but you may have more
than one job/process, so swap one in, execute it, swap it out, swap next one, etc.
Overlapped swapping - Memory divided into monitor, buffer1, buffer2, and user space. Keep
two jobs in buffer spaces and swap them in and out of user space as needed.
Multiple partitions - Memory is divided into regions, or partitions. For a job to execute, it is
TDC 311 Memory Management
1
loaded into a free partition. When done, the job is removed and the partition is marked free.
Can have fixed (static) sized partitions (MFT) and variable (dynamic) sized partitions
(MVT) (IBM OS/360).
MFT
Problems: Asking for more memory. Internal fragmentation.
TDC 311 Memory Management
2
MVT C No fixed sized partitions. Fit job wherever it will fit. Best fit? First fit? Worst fit?
Problems: Fragmentation. Garbage collection (compaction).
Simple paging - Divide memory into fixed sized frames. Divide user process into fixed sized
pages. Take a page of user program and plug into empty frame. Each job has its own page table.
No external fragmentation, only internal fragmentation. On average, 2 of a frame will be
wasted.
TDC 311 Memory Management
3
Shared pages - Possible to share pages (like editor, compiler, database code) if code is
reentrant (does not modify itself).
Protection - Can add read only / read write / execute bits to each page in the page table.
Simple segmentation - Like paging, but user job not broken into fixed sized pages (physical
division) but left as procedures / functions / data sets.
Protection C Can now place access bits on entire segment. A code segment can be
accessed by many users. A data segment can be accessed by only one user. Put an array
in a segment by itself and the hardware will check the array bounds automatically!
Sharing C Same as with paging.
Virtual memory - Why load the entire program into memory at the start? Just load the
TDC 311 Memory Management
4
necessary routines / modules. Why? Error routines are seldom called. Don=t always need entire
array, list or table. Certain sections of code may be rarely used.
Resident set - portion of user process that is currently in main memory.
Advantages of virtual memory - May have more processes in memory since only part of
each process is resident. It is possible for a process to be larger than main memory.
Disadvantages - If part of a process is not in memory, process is moved to blocked state,
disk I/O reads in necessary piece of code, process is moved to Ready state, process
resumes execution. Thrashing : removing a piece to satisfy some other process, but then
needing that piece next.
Demand paging - Page table for each process must also include 1 bit for each page - In memory?
Also, one Modify bit for each page - tells if the page has been modified.
What happens if page is not resident?
1. Take page number, look in page table.
2. Is the page resident? If yes, get frame number. If no, page fault.
3. Find a free frame. No free frame? Call replacement algorithm.
4. Load desired page from disk into memory.
5. When disk read completed, update page table.
6. Restart instruction.
TDC 311 Memory Management
5
Paging issues
Page size - The smaller the page, the less internal fragmentation, but the bigger the page table.
If a page is 512 bytes in size (29) and we are using a memory space 231, we will need 222 page
table entries per process! How do you handle such a large page table? Keep only part of the
page table resident, or use an inverted page table.
Typical page sizes: IBM 370: 2048 / 4096; VAX: 512; INTEL 486: 4096; Motorola 68040: 4096.
The paging process can be time intensive. Make sure the paging process is fast and doesn=t
occur constantly.
Pure demand paging vs. prepaging.
If you need to bring a page in, but there are no available frames, you have to remove a page and
put it back to disk. Who do you remove? Page replacement algorithms: FIFO, Least Recently
Used (LRU), and optimal.
For example:
Let=s say we are given the following page reference string:
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 and 3 frames of memory
FIFO:
LRU:
Optimal:
How many pages at one time do you give to each process (resident set size)? Can the resident
set size change as the process=s demands grow and shrink?
TDC 311 Memory Management
6
When you take a page out to make room for another page, check its Modify bit. If it has not been
set, the page has not been modified (is not dirty) and there is no need to write the page back to
disk.
There is also demand segmentation.
Example C Windows NT
Page size = 4096 bytes. Segmentation and paging are optional and in combinations.
With unsegmented memory, user=s virtual space is 232 = 4 Gb.
With segmentation, there is a 16-bit segment reference (of which 2 bits are used for protection)
and a 32 bit offset, for 246 = 64Tb !
Paging is done via 2 levels - first level is a page directory containing 1024 entries. Each entry is
a page table with 1024 entries. Each entry is 4 Kb.
TDC 311 Memory Management
7
Download