Memory Management

来源:互联网 发布:mac的无线设置路由器 编辑:程序博客网 时间:2024/06/09 20:54

1. Address spaces

The set of addresses that a process can use to address memory, each process has its own address space.

Base and Limit Registers( disadvantage: need to perform an addition and a comparison on every memory reference.)

2. Swapping

Consists of bringing in each process in its entirety, running for a while, then putting it back on the disk.

  • Swapping creates multiple holes in memory, memory compaction combine them all into one big one by moving all the processes downward as far as possible.
  • Many processes will grow as they run, so allocate a little extra memory whenever a process is swapped in or moved.

3. Manage Free Memory

  • Bitmaps: Memory is divided into allocation units, each allocation unit is a bit in bitmap, 0 is free and 1 is occupied.

             Disadvantage: Searching a bitmap for a run of a given length is a slow operation)

  • Linked Lists: Maintain a linked list of allocated and free memory segments. 

             Hole (H) or Porcess (P), the address at which it starts, the length, and a pointer to the next entry.

             Several algorithms can be used to allocate memory for a created process:

  • First fit : The memory manager scans along the list of segments until it finds a hole that is big enough.
  • Next fit : Works the same way as first fit, the next time it is called to find a hole, it starts searching the list from the place where it left off last time, instead of always at the beginning.
  • Best fit : Best fit searches the entire list, from beginning to end, and takes the smallest hole that is adequate.(disadvantage: results in more wasted memory and generates tiny useless holes)
  • Worst fit : It takes the largest available hole.
  • Quick fit : Maintains separate lists for some of the more common sizes requested.

4. Virtual Memory

Each program has its own address space, which is broken up into chunks called pages.

These pages are mapped onto physical memory, but not all pages have to be in physical memory to run the program.

  • Paging

             Pages ------>  Memory Management Unit ------> Page frames

             Present/absent bit keeps track of which pages are physically present in memory.

  • Page Fault

             When the page is unmapped and causes the CPU to trap to the operating system. OS picks a little-used page frame and writes its contents back to the disk, then fetches the page just referenced into the page frame, change the map and restarts the trapped instruction.

  • Page Tables

             Virtual address is split into a virtual page number and an offset.

             virtual page number ------> page table index ------> page frame ------>  + offset ------> physical address

  • TLB ( Translation Lookaside Buffer )  

             virtual page number ------> TLB ------yes--->execute process

                                                                      ------no --->TLB delete one row, then read one new row from page table

  • Page Tables for Large Memories

             Multilevel Page Tables/ Inverted Page Tables

5. Page replacement Algorithms

  • The Optimal Page Replacement Algorithm

             When page fault occurs, the page with the highest label should be removed.(Label:number of instructions that will be executed before that page is first referenced)

             Impossible to implement, but for specific program and input it should be simulated.

  • The Not Recently Used Page Replacement Algorithm

             R is set whenever the page is referenced (read or written).M is set when the page is written to (i.e., modified).

             Class 0: not referenced, not modified.
             Class 1: not referenced, modified.
             Class 2: referenced, not modified.
             Class 3: referenced, modified.

             When page fault occurs, removes a page at random from the lowest-numbered nonempty class.

  • The First- In, First-Out (FIFO) Page Replacement Algorithm

              The new one goes on the back of the list; the one at the front of the list is dropped.

  • The Second-Chance Page Replacement Algorithm

             When page fault occurs, check oldest page R bit ----- 0 ------> evicted

                                                                                                      ------1 ------>oldest page is put onto the end of the list and R bit is set to 0

  • The Clock Page Replacement Algorithm

             Keep all the page frames on a circular list in the form of a clock, the hand points to the oldest page.           

             When a page fault occurs, the page being pointed to by the hand is inspected.

             Check oldest page R bit --- 0 --->the page is evicted, the new page is inserted into the clock in its place, and the hand is advanced one position. 

                                                         --- 1 ---> it is cleared and the hand is advanced to the next page.

  • The Least Recently Used(LRU) Page Replacement Algorithm

             Hardware realization:

  • After each memory reference, the current value of counter add one.

             When a page fault occurs, the OS examines all the counters in the page table to find the lowest one.

  • Maintain matrix of nxn bits, initially all zero. When page frame k referenced, the hardware first sets all bits of row k to 1, all bits of column k to 0.

             When a page fault occurs, the row whose binary value is lowest is the least recently used.

             Software realization:

  • NFU (Not Frequently Used) Algorithm

             After each memory reference, the current value of counter add page R bit.

  • Aging Algorithm
  • The Woking Set Page Replacement Algorithm        
  •  Working Set : The set of pages that a process is currently using.
  •  Thrashing : A program causing page faults every few instructions
  • The WSClock Page Replacement Algorithm
0 0
原创粉丝点击