Segmentation
16 May 2023 22:32
Segmentation is a memory-management scheme that supports the user view of memory.
Logical address space = collection of segments
Address specifies segment name and offset within segment both defined by the user.
A Logical address consists of a two tuple: <segment-number, offset >
The user program is compiled, and the compiler automatically constructs segments
reflecting the input program.
A Pascal compiler might create separate segments for the following
1. Global variables
2. Procedure call stack in order to store parameters and return addresses
3. The code portion of each procedure and function
4. Local variables of each procedure and function.
A Fortran compiler create a separate segment for
1. each common block.
2. Arrays might be assigned separate segments.
The loader would take all these segments and assign them segment numbers.
HARDWARE:
Mapping of the two-dimensional user-defined addresses into one-dimensional physical
addresses is affected by a segment table.
Entry : a segment base(starting physical address where the segment resides in memory)
and a segment limit(the length of the segment).
all sem 5 notes Page 1
A logical address consists of two parts:
1. a segment number, s, The segment number is used as an index into the segment
table.
2. an offset into that segment, d. The offset d of the logical address must be between 0
and the segment limit. If it is not, we trap to the operating system, i.e. , logical
addressing attempt beyond end of segment.
If the offset is legal, it is added to the segment base to produce the address in physical
memory of the desired byte.
The segment table is an array of base-limit register pairs.
PROTECTION AND SHARING
1. Because the segments represent a semantically defined portion of the program, it is
likely that all entries in the segment will be used the same way.
Some segments are instructions, whereas other segments are data.
In a modern architecture, instructions are non-self-modifying, so instruction segments can
be defined as read only or execute only. The memory mapping hardware will check the
protection bits associated with each segment table entry to prevent illegal accesses to
memory, such as attempts to write into a read-only segment, or to use an execute-only
segment as data.
By placing an array in its own segment, the memory-management hardware will
automatically check that array indexes are legal and do not stray outside the array
boundaries. Thus, many common program errors will be detected by the hardware before
they can cause serious damage.
2. Each process has a segment table associated with it, which the dispatcher uses to
define the hardware segment table when this process is given the CPU. Segments
are shared when entries in the segment tables of two different processes point to
the same physical location.
Sharing occurs at segment level. Any info defined to be a segment could be shared.
Code segments typically contain references to themselves.
Read-only data segments that contain no physical pointers shared as different
segment numbers as may code segments that refer to themselves not directly, but
rather only indirectly.
FRAGMENTATION :
cause external fragmentation, when all blocks of free memory are too small to
accommodate a segment
Process waits until more memory (or at least a larger hole) becomes available, or until
compaction creates a larger hole
If the CPU scheduler must wait for one process, because of a memory allocation problem,
it may (or may not) skip through the CPU queue looking for a smaller, lower-priority
process to run.
Solution:
all sem 5 notes Page 2
Solution:
1. reduces to the variable-sized partition scheme results if each process is defined one
segment
2. every byte could be put in its own segment and relocated separately. This
arrangement eliminates external fragmentation
In this every byte needs base register for its relocation, hence the memory used is
doubled.
1. Paging: the average segment size is small, external fragmentation will also be small.
And since individual segments are smaller than the overall process, they are more
likely to fit in the available memory blocks.
all sem 5 notes Page 3