View Single Post
  #7  
Old Thursday, March 27, 2008
Janeeta's Avatar
Janeeta Janeeta is offline
Member
 
Join Date: Dec 2006
Location: Karachi
Posts: 96
Thanks: 26
Thanked 121 Times in 39 Posts
Janeeta is on a distinguished road
Default Segmentation

Segmented memory management


In a segmented memory management system the blocks to be replaced in main memory are potentially of unequal length and correspond to program and data ``segments.'' A program segment might be, for example, a subroutine or procedure. A data segment might be a data structure or an array. In both cases, segments correspond to logical blocks of code or data. Segments, then, are ``atomic,'' in the sense that either the whole segment should be in main memory, or none of the segment should be there. The segments may be placed anywhere in main memory, but the instructions or data in one segment should be contiguous


Using segmented memory management, the memory controller needs to know where in physical memory is the start and the end of each segment. When segments are replaced, a single segment can only be replaced by a segment of the same size, or by a smaller segment. After a time this results in a ``memory fragmentation'', with many small segments residing in memory, having small gaps between them. Because the probability that two adjacent segments can be replaced simultaneously is quite low, large segments may not get a chance to be placed in memory very often. In systems with segmented memory management, segments are often ``pushed together'' occasionally to limit the amount of fragmentation and allow large segments to be loaded.

This organization appears to be efficient because an entire block of code is available to the processor. Also, it is easy for two processes to share the same code in a segmented memory system; if the same procedure is used by two processes concurrently, there need only be a single copy of the code segment in memory. (Each process would maintain its own, distinct data segment for the code to access, however.)

Segmented memory management is not as popular as paged memory management, however. In fact, most processors which presently claim to support segmented memory management actually support a hybrid of paged and segmented memory management, where the segments consist of multiples of fixed size blocks.

In a segmented system, the blocks of memory are variable in size. Each process may have one or more segments. The segments may be visible to the process, that is, it may be aware that there are code, data, stack, and heap segments (whereas pages are usually transparent). A segment table is used that specifies the base of the physical addresses associated with a segment and the valid range. The combination of this base and the offset specified by the address means that two words must be used by the MMU to identify the physical address.

One advantage to the operating system is that once a segment has been allocated, there won't be any access faults from the process except those that actually try to access beyond the segment boundaries. Thus, once a process has started, it can run at memory rates until it returns control to the OS. Of course, this leads to the problem that if a segment is large, the time for a context switch can be excessive.

An access outside of the segment is detected as exceeding the valid range, and is trapped. Segmented virtual addressing is otherwise similar to paging. One problem with pure segmentation is external fragmentation (holes in the address map following a series of allocations and deallocations) that can lead to low memory utilization. External fragmentation is also called checkerboarding, and its correction requires a phase of memory compaction that reduces processing efficiency.

Advantages of segmentation over paging:
  • Speed. Reloading segment registers to change address spaces is much faster than switching page tables.
  • Segment descriptor tables consume less memory than page tables.
  • x86 page table entries do not have an 'Executable' bit. With segmentation, you can make a region of memory executable (code) or not (data).
  • Segment size can be byte-granular (size 1 byte to 1Meg in units of 1 byte); pages are always page-granular (size 4K to 4Gig in units of 4K).
  • Segmentation lets you make the segment as large as necessary, with no excess (there is no internal fragmentation).


Advantages of paging over segmentation:
  • Page-based address translation turns non-contiguous physical addresses into contiguous virtual addresses (the external fragmentation of free memory does not matter).
  • Many CPUs support paging, only a few support segmentation
  • Some things that are easy to do with paging but hard to do with segmentation unless you have multiple segments
  1. Efficient support for sparse address spaces. You can have large 'holes' in the virtual address space, like the hole between the top of the heap and the top of the stack.
  2. Shared memory between tasks
  • Some things are easier to do with paging because x86 page fault, unlike general protection fault, stores the faulting address in register CR2
  1. Demand-loading. No page of memory is allocated for a task until the task actually accesses the memory. This prevents a heavy load on the CPU when a task first starts up. It also conserves RAM.
  2. Memory-mapped files. This lets you read and write a file by reading and writing memory locations
  • Swapping. If RAM runs low, the kernel can copy pages that haven't been
accessed recently to a swapfile on the disk, to free RAM for more active tasks.
Reply With Quote