OS Lecture - Segmentation
OS Lecture - Segmentation
OS Lecture #22
Operating Systems
Start Lecture #22
Remark: Lab 4 is available and is due in 2 weeks (23 April 2009).
3.7 Segmentation
Up to now, the virtual address space has been contiguous. In segmentation the virtual address space is
divided into a number of variable-size segments. One can view the designs we have studied so far as having
just one segment, the entire process.
Among other issues this makes memory management difficult when there are more that two
dynamically growing regions.
With two regions you start them on opposite sides of the virtual space as we did before.
Better is to have many virtual address spaces each starting at zero.
This split up is user visible. So a segment is a logical split up of the address space. Unlike with (userinvisible) paging, segment boundaries occur at logical point, e.g., at the end of a procedure.
Imagine a system with several large, dynamically-growing, data structures. The same problem we
mentioned for the OS when there are more than two growing regions, occurs as well for user
programs. The user (or some user-mode tool) must decide how much virtual space to leave between
the different tables. With segmentation
Eases flexible protection and sharing: One places in a single segment a unit that is logically shared. This
would be the natural method to implement shared libraries.
When shared libraries are implemented on paging systems, the design essentially mimics segmentation
by treating a collection of pages as a segment. This is more complicated since one must ensure that the
end of the unit to be shared occurs on a page boundary (this is done by padding).
Without segmentation (equivalently said with just one segment) all procedures are packed together so,
if one changes in size, all the virtual addresses following this procedure are changed and the program
must be re-linked. With each procedure in a separate segment this relinking would be limited to the
symbols defined or used in the modified procedure.
Homework: Explain the difference between internal fragmentation and external fragmentation. Which one
occurs in paging systems? Which one occurs in systems using pure segmentation?
** Two Segments
Late PDP-10s and TOPS-10
Each process has one shared text segment, that can also contain shared (normally read only) data. As
the name indicates, all process running the same executable share the same text segment.
The process also contains one (private) writable data segment.
Permission bits define for each segment.
** Three Segments
Traditional (early) Unix had three segments as shown on the right.
http://www.cs.nyu.edu/courses/spring09/V22.0202-002/lectures/lecture-22.html
1/8
6/8/2014
OS Lecture #22
Consideration
Programmer aware
http://www.cs.nyu.edu/courses/spring09/V22.0202-002/lectures/lecture-22.html
Demand
Paging
No
Demand
Segmentation
Yes
2/8
6/8/2014
OS Lecture #22
3/8
6/8/2014
OS Lecture #22
The basic idea is to employ (non-demand) paging on each segment. A segmentation plus paging scheme has
the following properties.
A virtual address becomes a triple: (seg#, page#, offset).
Each segment table entry (STE) points to the page table for that segment. Compare this with a
multilevel page table.
The physical size of each segment is a multiple of the page size (since the segment consists of pages).
The logical size is not; instead we keep the exact size in the STE (limit value) and terminate the process
if it references beyond the limit. In this case the last page of each segment is partially wasted (internal
fragmentation).
The page# field in the address gives the entry in the chosen page table and the offset gives the offset in
the page.
From the limit field, one can easily compute the size of the segment in pages (which equals the size of
the corresponding page table in PTEs).
A straightforward implementation of segmentation with paging would requires 3 memory references
http://www.cs.nyu.edu/courses/spring09/V22.0202-002/lectures/lecture-22.html
4/8
6/8/2014
OS Lecture #22
5/8
6/8/2014
OS Lecture #22
The Multics hardware (GE-645) was word addressable, with 36-bit words (the 645 predates bytes).
Each virtual address was 34-bits in length and was divided into three parts as mentioned above. The
seg# field was the high-order 18 bits; the page# field was the next 6 bits; and the offset was the loworder 10 bits.
The actual implementation was more complicated and the full 34-bit virtual address was not present in
one place in an instruction.
Thus the system supported up to 218=256K segments, each of size up to 26=64 pages. Each page is
of size 210 (36-bit) words.
Since the segment table can have 256K STEs (called descriptors), the table itself can be large and was
itself demand-paged.
Multics permits some segments to be demand-paged while other segments are not paged; a bit in each
STE distinguishes the two cases.
The Pentium Scheme
The Pentium design implements a trifecta: Depending on the setting of a various control bits the Pentium
scheme can be pure demand-paging, pure segmentation, or segmentation with demand-paging.
The Pentium supports 214=16K segments, each of size up to 232 bytes.
This would seem to require a 14+32=46 bit virtual address, but that is not how the Pentium works.
The segment number is not part of the virtual address found in normal instructions.
Instead separate instructions are used to specify which are the currently active "code segment" and
"data segment" (and other less important segments). Technically, the CS register is loaded with the
"selector" of the active code segment and the DS register is loaded with the "selector" of the active
data register.
When the selectors are loaded, the base and limit values are obtained from the corresponding STEs
(called descriptors).
There are actually two flavors of segments, some are private to the process; others are system
segments (including the OS itself), which are addressable (but not necessarily accessible) by all
processes.
Once the 32-bit segment base and the segment limit are determined, the 32-bit address from the instruction
itself is compared with the limit and, if valid, is added to the base and the sum is called the 32-bit "linear
address". Now we have three possibilities depending on whether the system is running in pure segmentation,
pure demand-paging, or segmentation plus demand-paging mode.
1. In pure segmentation mode the linear address is treated as the physical address and memory is
accessed.
2. In segmentation plus demand-paging mode, the linear address is broken into three parts since the
system implements 2-level-paging. That is, the high-order 10 bits are used to index into the 1st-level
page table (called the page directory). The directory entry found points to a 2nd-level page table and
the next 10 bits index that table (called the page table). The PTE referenced points to the frame
containing the desired page and the lowest 12 bits of the linear address (the offset) finally point to the
referenced word. If either the 2nd-level page table or the desired page are not resident, a page fault
occurs and the page is made resident using the standard demand paging model.
3. In pure demand-paging mode all the segment bases are zero and the limits are set to the maximum.
Thus the 32-bit address in the instruction become the linear address without change (i.e., the
http://www.cs.nyu.edu/courses/spring09/V22.0202-002/lectures/lecture-22.html
6/8
6/8/2014
OS Lecture #22
segmentation part is effectively) disabled. Then the (2-level) demand paging procedure just described
is applied.
Current operating systems for the Pentium use this last mode.
3.9 Summary
Read
4.1 Files
4.1.1 File Naming
Very important. A major function of the file system is to supply uniform naming. As with files themselves,
important characteristics of the file name space are that it is persistent and concurrently accessible.
Unix-like operating systems extend the file name space to encompass devices as well
Does each file have a unique name?
Answer: Often no. We will discuss this below when we study links.
File Name Extensions
The extensions are suffixes attached to the file names and are intended to in some why describe the high-level
http://www.cs.nyu.edu/courses/spring09/V22.0202-002/lectures/lecture-22.html
7/8
6/8/2014
OS Lecture #22
http://www.cs.nyu.edu/courses/spring09/V22.0202-002/lectures/lecture-22.html
8/8