Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.1Operating System Concepts
Operating Systems
Lecture 35Segmentation
Read Ch. 9.5 - 9.6
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.2Operating System Concepts
Operator Overrides
To use some of the list functions, such as display( ), search( ) and valueInsert( ) or valueRemove( ), you need to override the output and comparison operators for your Process struct. Here's how:
ostream& operator << (ostream& outs, const Process &myProcess) { outs << "Process ID: " << myProcess.pid << " Number of bursts: " << myProcess.numBursts << endl; return outs;}
bool operator < (const Process proc1, const Process proc2) { return proc1.pid < proc2.pid; //This sorts by pid. You could choose } //something else, e.g. burstTime
bool operator == (const Process proc1, const Process proc2) { return proc1.pid == proc2.pid;}
bool operator != (const Process proc1, const Process proc2) { return proc1.pid != proc2.pid;}
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.3Operating System Concepts
User's view of physical memory
Physical memory is a linear array of memory addresses. With paging, the user's view is a contiguous storage space that is
linear. The process is not actually stored contiguously in memory. Users don't usually think of memory in terms of a linear array. Users generally think in terms of a collection of items (segments) that
are stored in memory with no particular order among segments. Example: A C++ program may consist of:
A main function A set of functions or classes data structures etc.
We refer to each of these by name. We don't care where each piece is stored relative to another.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.4Operating System Concepts
User’s View of a Program
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.5Operating System Concepts
Segmentation
Segmentation is a memory-management scheme that supports user view of memory.
The logical address space is a collection of segments. A segment is a logical unit such as:
main program,function,method,object,local variables, global variables,stack,symbol table, arrays
Each segment has a name and a length. For simplicity, the segments are given numbers.
The logical address specifies the segment number and the offset: <segment-number, offset>
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.6Operating System Concepts
Logical View of Segmentation
1
3
2
4
1
4
2
3
user space physical memory space
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.7Operating System Concepts
Segmentation Architecture
Logical address consists of two numbers:
<segment-number, offset>, Segment table – maps two-dimensional physical addresses; each
table entry has: base – contains the starting physical address where the segments reside
in memory. limit – specifies the length of the segment.
The limit register is used to check for valid memory references. Segment-table base register (STBR) points to the segment table’s
location in memory. Segment-table length register (STLR) indicates number of
segments used by a program;
segment number s is legal if s < STLR.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.8Operating System Concepts
Segmentation Hardware
Diagram of segmentation hardware: We will draw this in class.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.9Operating System Concepts
Segmentation Hardware
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.10Operating System Concepts
Example
Suppose a program is divided into 5 segments: Segment 0: Subroutine Segment 1: sqrt function Segment 2: main program Segment 3: stack Segment 4: symbol table
The segment table is as follows:
Segment Limit Base
0 1000 1400
1 400 6300
2 400 4300
3 1100 3200
4 1000 4700
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.11Operating System Concepts
Example of Segmentation
We will draw a diagram in class.
Question: Where does segment 1, offset 43 map in physical memory? Where does segment 2, offset 567 map?
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.12Operating System Concepts
Example of Segmentation
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.13Operating System Concepts
Protection with Segmentation
Protection. With each entry in segment table associate: validation bit = 0 illegal segment read/write/execute privileges
Protection bits associated with segments. This makes more sense than with paging, because the
entire segment is likely to have the same read/write/execute privileges.
For example, instructions for non-self-modifying code may be read-only or execute only. These will belong to a single segment and will not be grouped with other types of data.
In the paging system, a page may be half filled with instructions and half filled with data.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.14Operating System Concepts
Sharing of Segments
Each process has a segment table associated with it. Segments can be shared when entries in the segment
tables of two different processes point to the same physical location.
Sharing occurs at the segment level. Any information can be shared if it is defined as a segment.
Full programs can be shared (even with multiple segments). For example, text editors.
Parts of programs can be shared (e.g. functions from function libraries).
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.15Operating System Concepts
Diagram of Segment Sharing
Diagram of a text editor shared between 2 processes.We will draw this in class.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.16Operating System Concepts
Sharing of Segments
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.17Operating System Concepts
Self Reference in Shared Code
What happens if a shared segment refers to itself? If it refers to itself by segment number, then
all processes using it must have the same segment number in their segment tables.
If it refers to itself indirectly, e.g. specifying an offset from the current program counter, then the processes can have different segment numbers for it in their segment tables.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.18Operating System Concepts
Fragmentation
Segments are of variable length. Memory allocation is a dynamic process that uses a best
fit or first fit algorithm. Is there internal fragmentation in a segmentation system?
Is there external fragmentation?
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.19Operating System Concepts
Segmentation vs. Paging
Paging and Segmentation each have different advantages and disadvantages.
What are advantages and disadvantages of paging? No external fragmentation (advantage) There is internal fragmentation (disadvantage) Units of code and data are broken up into separate pages
(disadvantage) What are advantages and disadvantages of segmentation?
No internal fragmentation (advantage) There is external fragmentation (disadvantage) Keeps blocks of code or data as single units (advantage.
Can get advantages of both systems by combining them.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.20Operating System Concepts
Segmentation with Paging – MULTICS
The MULTICS system solved problems of external fragmentation and lengthy search times by paging the segments.
Solution differs from pure segmentation in that the segment-table entry contains not the base address of the segment, but rather the base address of a page table for this segment.
The offset for the segment is translated to a page number and an offset for that page.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.21Operating System Concepts
MULTICS Address Translation Scheme
We will draw this diagram in class.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 2005
7.22Operating System Concepts
MULTICS Address Translation Scheme