UNIX: Memory Management

 

James Wright

 

Copyright © 2003 Saba Computer Consulting

 

 

 

 

Memory Management is the key behind the idea of sharing.  In order to increase performance and have several processes in memory at a time, you have to find a way to share the memory.

 

This Memory consists of large array of words or bytes, each having its own address.

 

UNIX uses two methods for  Memory Management:

 

·       Swapping

·       Paging


Method 1: Swapping Memory

·       Processes need to be in memory to be executed.

·       These processes can be put temporarily out of memory then brought back into memory for continued execution. (Swap)

 

 

·       Allocation of Main Memory and Swap Memory is done first.

·       If the size of a process increases it does not fragment – it relocates itself as a whole image into another part of memory.

·       If there is no piece of memory large enough then the process is swapped out till it can be swapped back in with a new size.

 

·       Scheduler Process decides what processes are swapped in and out.

·       This swapper will wake up once every 4 seconds to check the processes going in and out.

·       If the process is idle, been in main memory too long, or is too large - it will most likely be swapped out.

·       If a process is small or been swapped out for a long time – it will most likely be swapped in.

·       If processes do not need to be swapped at all – the process table will search for worthwhile processes to take its place.

 

 

·       Many UNIX systems use this kind of swapping method.  However, Berkeley designed systems depend on Paging for memory management.


Method 2: Paging

 

·       Paging allows for non contiguous address spacing.

·       Phys Memory is broken into fixed blocks: Frames

·       Log Memory is broken into fixed blocks: Pages

·       When a process executes, the pages are loaded into any available memory frames from the backing store.

·       This backing store then is divided into the fixed blocks so that they are the same size as the memory frames.

·       A Page table exists between Logical and Physical memory which contains base addresses for each page in the physical memory.

Berkeley Paging Methods:

 

·       Berkeley introduced something called demand-paged-virtual-memory system, which eliminated external fragmentation.

·       Swapping was kept minimal because more than one job could be kept in main memory.

·       Paging allows execution with only parts of a process in memory.

 

Demand Paging:

 

1.    When a process needs a page and the page is not present - a page fault to the kernel occurs.

2.    A frame of memory is then allocated.

3.    The proper disk page is read into a frame.

 

Interaction and Thrashing:

 

CPU scheduling, memory swapping, and paging all interact together.  If the priority is lower in a process, it is more likely that its paged will be paged/swapped entirely out.

Having priority preferences like this guards against overworking the CPU. (Thrashing).