Segmentation, like
paging, divides a program into a number of smaller blocks called segments, each
of which is allocated to memory independently. Unlike paging, segments are
variable-size. Responsibility for dividing the program into segments lies with
the user (or compiler) the operating system is uninvolved. The hardware sets an
upper limit on the size of any segment.
A segment table is very
similar to a page table. However, since segments are variable –size, memory
cannot be predivided into anything like a page frame. The operating system must
maintain a free list and allocate segments to memory holes. The issues of how
the allocation is performed are the same as those found in the variable partition
scheme.
A segment table entry
must have fields for storing the segments starting address in main memory and
the size of the segment. If the maximum segment size in m bits, the last m bits
of the logical address specify the segment offset. The remaining bits specify
the segment number. The logical address is translated into a physical address
by extracting the segment number and offset from the logical address. The segment
number is used as an index into the segment table. The offset is compared to
the segment size. If the offset is greater than the segment size, an
invalid-address fault is generates, causing the program to be aborted. Otherwise,
the offset is added to the segment’s starting address to generate the physical
address. To increase speed, the size check and the physical address generation can
be performed concurrently.
Because segments are
user defined, it is possible to define certain segments to be read-only. By adding
a read-only bit into a segment table entry, the memory management system can
check for write operations to read-only segments and generate a fault if such
an operation is detected.
If support for read-only
segments is present, it may also be possible for multiple processes to share
read-only segments, thereby decreasing memory usage. Typically, this occurs
when two processes are executing the same program and the code for that program
is designated o be in one or more read-only segments.
Shared segments may
also be used by processes executing different programs but using the same subroutine
library. In this situation, care must be taken that addresses within this
shared segment will work with both programs. For addresses to locations within
the segments the simplest solution is to use relative addressing.
Relative addressing
uses the offset from the current value of the program counter to generate the
destination address. For all addresses, indirect addressing though a register pointing
to the appropriate segment is also a possibility. Direct addressing, which
specifies the segment and offset of the destination address, is possible only
if all programs use the same segment number for the segment being access.
Advantages:
· Eliminates
fragmentation
· Provides
large virtual memory
· Allows
dynamic segment growth
· Assists
dynamic linking
· Facilitates
shared segments
· Enforces
controlled access.
Disadvantages:
· Considerable
compaction overhead is incurred in order to support dynamic segment growth and
eliminate fragmentation.
· There
is difficulty in managing variable size segments on secondary storage.
· The
maximum size of a segment is limited by the size of the main memory.
· It
is necessary to develop techniques or constraints to prevent segment thrashing.