Professional Documents
Culture Documents
Basic Memory Management y Relocation and protection y Swapping y Virtual Memory y Paging y Segmentation y Paging with segmentation
y
Memory hierarchy
small amount of fast, expensive memory cache some medium-speed, medium price, volatile main memory RAM) Tens of hundreds of gigabytes of slow, cheap , nonvolatile disk storage
Memory manager :
which parts of memory are in use and which parts are not in use Allocate and deallocate memory Manage swapping between main memory and disk when main memory is too small to hold all the processes.
1.
2.
When job arrives, it can be put into the input queue for the smallest partition large enough o hold it. May be small jobs have to wait to get into memory, even though plenty of memory is free.
Whenever a partition becomes free, the job closest to the front of the queue that fits in it could be loaded into the empty partition and run. y Search the whole input queue and pick the largest job that fits. y At least one small partition y Job may not be skipped over more than k times.
y
y Input queue collection of processes on the disk that are waiting to be brought into memory to run the program. y User programs go through several steps before being run.
y Logical and physical addresses are the same in compile-time and load-time address-binding schemes; y logical (virtual) and physical addresses differ in execution-time address-binding scheme.
11
Relocation
y When a program is linked the linker must know at what address the program will begin in memory. y Call to a procedure at absolute address 100 y Program is Loaded in partition 1 y 100K+100
Protection
y y y y
Divide memory into blocks of 2-KB bytes 4-bit protection code to each block (IBM) PSW contained 4-bit key Use base and limit values
address locations added to base value to map to physical address address locations larger than limit value is an error
14
Swapping
y A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution. y Backing store fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images.
Swapping (1)
swapping
Complicates allocation and deallocation of memory as well as keeping track of it. y Memory compaction : moving one by one all the processes downward as far as possible. y It is usually not done because it requires a lot of CPU time.
y
When memory is assigned dynamically os must manage it. y Two ways to keep track of memory usage
y
20
y y
Size of allocation unit is important design issue y Smaller the allocation unit the larger the bitmap y Larger the allocation unit , smaller bitmap , but memory may be wasted in the last unit of the process. y Problem : searching a bitmap for a given length is slow operation
y
When the processes and holes are kept on a list sorted by address, several algorithms can be used to allocate memory for a newly created process. y First fit y Next fit y Best fit y Worst fit y Quick fit
y
Operating System Concepts
ALGORITHM:
y
First fit : scan the list of segments until it finds a hole that is big enough to hold process. The hole is then broken up into two pieces, one for the process and one for the unused memory. Next fit : It works the same way as first fit, except that it keeps track of where it is finds a suitable hole. The next time it is called to find the hole, it starts searching the list from the place where it left off last time, instead of always at the begging as first fit does.
Best fit :
The memory manager scans entire list of segments and takes the smallest hole that is adequate.
Worst fit :
The memory manager scans entire list of segments and takes the largest available hole .
Quick fit : The memory manager maintains separate lists for common size requested. With quick fit, finding a hole of the required size is extremely fast when a process terminates or is swapped out, finding its neighbours to see if a merge is possible is expensive.
Example
y
Consider a swapping system in which memory consists of the following hole sizes in memory order : 10 4 20 18 7 9 12 15 which hole is taken for successive requests of a. 12 b. 10 c. 9 for first fit? Repeat the question for best fit, worst fit, and next fit.
Virtual Memory
Split program into pieces called overlays. y Swapping of overlays in and out done by system
y y
Virtual Memory
y
Virtual Memory
Paging (1)
Paging (2)
The relation between virtual addresses and physical memory addresses given by page table
Operating System Concepts
Page Table
The page number is used as an index into the page table, yielding the number of the page frame y P/A bit is o, trap to the OS. y P/A bit is 1, the page frame number found in the page table is copied to the high order 3 bits of the output register
y
y y
Page frame number: Goal of page mapping to locate this value. Present/absent bit: If 1-entry is valid and can be used. If 0-virtual page to which the entry belongs is not currently in memory. Protection bit: It tells that what kind of access are permitted.
Operating System Concepts
Modified and referenced bits : It keep track of page usage. When a page is written to ,the h/w automatically sets the modified bit.
if the page in it has been modified (i.e. is dirty), it must be written back. If it has not been modified (i.e. is clean), it can be just be abandoned.
Referenced Bit: It is set whenever a page is referenced either for reading or writing. Its value help the O.S choose a page to evict when a page fault occurs. y Caching : This feature is important for page that map onto device reg. rather than memory. Machine that have separate I/O space and do not use memory mapped I/O do not need this bit.
y
Page Table
Page table can be extremely large y The mapping must be fast
y
Each process needs its own page table Mapping must be done on every memory reference Solution : single page table consisting of an array of fast hardware register Expensive It can be entirely in main memory
Operating System Concepts
y Since the page table is paged, the page number is further divided into:
a 10-bit page number. a 10-bit page offset.
y here pi is an index into the outer page table, and p2 is the displacement 10 12 within the page of the outer page table. 10
In this method avoid keeping all the page tables in memory all time. Those that are not needed should not be kept around.
Operating System Concepts
A TLB to speed up paging To see virtual page is present in TLB comparing all the entries simultaneously.
Operating System Concepts
y y
48
52
At the time of page fault , the OS has no way of knowing when each of the page will be referenced next.
When a process is started up, both page bits for all its pages are set to 0 by the O.S. Periodically (e.g. on each clock interrupt), the R bit is cleared, to distinguish pages that have not been referenced recently from those that have been.
Best page to replace Not quite good , because the page will need to be written out before replacement It probably will be used again soon It probably will be used again soon , and the page will need to be written out to disk before replacement
2.
3.
4.
referenced, modified(1 1)
1.
56
0,1,2,3,2,1,0,3,2,3: the row whose value is lowest is the last recently used.
59
It requires a software counter associated with each page, initially zero. At each clock interrupt the o.s. scans all the pages in memory. For each page the R bit ,which is 0 or 1 , is added to the counter. The counters are an attempt to keep track of how often each page has been referenced. When page fault occurs, the page with the lowest counter is chosen for replacement. Problem: It never forgets anything.
60
y y
The aging algorithm simulates LRU in software Note 6 pages for 5 clock ticks, (a) (e)
61
Demand paging: Pages are loaded only on demand not in advance. During any phase of execution the process reference only a relatively small fraction of its pages. Working set: The set of pages that a process is currently using is called its working set.
62
Thrashing : A program causing page fault every few instruction is said to be thrashing. Working set model: Many paging systems try to keep track of each process working set and make sure that it is in memory before letting the process run. This approach is called the working set model.
It is greatly reduce the page fault rate.
Current Virtual Time: Amount of CPU time a process has actually used has since it started Working set of process is the set of pages it has referenced during the past (T) seconds of virtual time.
64
65
67
I. II. III.
Example:
y
Given a system with four page frames, the following table indicates page, load time, last reference time, dirty bit, and reference bit. L.T. Last reference 374 321 306 331 Dirty bit 1 0 1 0 Ref. bit
Page
0 1 2 3
1 0 0 1
Which page will FIFO replace? Ans : 3 FIFO selects the page loaded first, page 3 at time 154 y Which page will LRU replace? Ans : 2 y Which page will NRU replace? Ans:1 NRU first looks for a clean page that was not recently referenced. y Which page will second chance replace?
y
Second chance first checks the oldest page page 3 Reference bit 1, it is reset and load time replace with current time
Given reference to the following pages by a program, 09018 18787 12827 82383 How many page fault will occur if the program has three page frames available to it and uses: 1. FIFO 2. LRU 3. optimal replacement
a. b. c. d. e.
On a simple paging system with 224 bytes of physical memory, 256 pages of logical address space and a page size of 210 bytes, How many bytes are in page frame? How many bits in the physical address specify the page frame number? How many entries are in the page table? What is the size of the logical address space? How many bits are needed to store an entry in the page table? Assume each page table entry contains a valid/invalid bit in addition to the page frame number.
a. b. c. d. e.
On a simple paging system with a page table containing 64 entries of 11 bits (including valid/invalid bit) each and a page size of 512 bytes, How many bits are in the logical address specify the offset within the page? How many bits are in the a logical address? What is the size of the logical address space? How many bits in the physical address specify the page frame number? How many bits in the physical address specify the offset within the page frame?
Before
Page
0 9 0 1 8 1 8 7 8 7 1 2 8
Fault 1 2 2 3 4 4 4 5 5 5 5 6 6
after 0-9090190 819 819 781 781 781 781 278 278 278
Before
Page
2 7 8 2 3 8 3
Fault 6 6 6 6 7 8 8
y y y
yfixed yIf
working set grows thrashing will result even if free page frame ,If working set shrinks local algorithm waste memory The system must continually decide how many page frames to assign to each process.
y y y
No of pages proportional to process size. Allocation must be updated dynamically PFF(page fault frequency) : It tells when to increase or decrease a process page allocation but says nothing about which page to replace..
Load Control
y y
Despite good designs, system may still thrash When PFF algorithm indicates
some processes need more memory but no processes need less
80
se p overhead ! p 2
Optimized when
s = average process size in bytes p = page size in bytes e = each page entry requires e bytes no. of pages needed per process =s/p Page table space=se/p bytes Internal fragmentation = p/2 Total overhead due to the page table and I.F. loss is given by page table space space internal fragmentation
p ! 2 se
81
Shared Pages
Shared Pages
y y y
Program text can be shared Separate table for I-space and D-space Each process has two pointers in its process table Special data structures are needed to keep track of shared pages.
Cleaning Policy
y
Paging works best when there are plenty of free page frames. Need for a background process, paging daemon
periodically inspects state of memory
85
Implementation Issues
Operating System Involvement with Paging
2.
Process execution
MMU reset for new process TLB flushed
3.
4.
3.
4.
5.
Hardware traps to kernel, saving the program counter on the stack. Assembly code routine is started to save the General registers and other info. OS determines page fault, tries to discover which virtual page needed OS checks validity of address, seeks page frame If selected frame is dirty, write it to disk , context switch take place, suspending faulting process and letting another process until disk transfer
88
o o o
OS brings schedules new page in from disk Page table updated Faulting instruction backed up to the state when it began Faulting process scheduled Registers restored Program continues
89
Backing Store
Swapping
Special area on the disk-swap area Allocate space in advance : copy entire process image to the swap area y Do not allocate in advance
y y
Allocate disk space for each page when it is swapped out and deallocate when swap back in There must be table per process telling for each page on disk where it is.
Segmentation (1)
y y
One-dimensional address space with growing tables One table may bump into another 92
93
Segmentation (3)
96
97
A Pentium selector
100