You are on page 1of 3

Classifying Information Memory Management in a

Stored in Memory Uniprogrammed System


address main address main address main
n By role in program: 2400
memory
2400
memory
2400
memory

OS OS OS
● Program instructions (unchangeable) 2200 2200 2200

● Constants: (unchangeable) 1700


n pi, maxnum, strings used by printf/scanf 1200

● Variables: (changeable) 700


C
A
n Locals, globals, function parameters, B
dynamic storage (from malloc or new)
0 0 0
n Initialized or uninitialized
n OS gets a fixed segment of memory
n By protection status: (usually highest memory)
● Readable and writable: variables
● Read-only: code, constants n One process executes at a time in a
single memory segment
● Important for sharing data and/or code
● Process is always loaded at address 0
n Addresses vs. data: ● Compiler and linker generate physical
addresses
● Must modify addresses if program is
moved (relocation, garbage collection) ● Maximum address = memory size – OS
size
1 Fall 1998, Lecture 22 2 Fall 1998, Lecture 22

Classifying Information
Segments of a Process
Stored in Memory (cont.)

n Binding time (when is space allocated?): n Process’ memory is divided into logical
● Static: before program starts running
segments (text, data, bss, heap, stack)
n Program code, static global variables ● Some are read-only, others read-write
(initialized and uninitialized)
● Some are known at compile time, others
● Dynamic: as program runs grow dynamically as program runs
n Procedure stack, dynamic storage (space
allocated by malloc or new) n Who assigns memory to segments?

n UNIX view of a process’s memory ● Compiler and assembler generate an


object file (containing code and data
(uniprogramming only for now):
segments) from each source file
max
address
stack stack segment ● Linker combines all the object files for a
program into a single executable object
file, which is complete and self-sufficient
dynamic storage
heap
● Loader (part of OS) loads an executable
(from new, malloc)
object file into memory at location(s)
static variables
(uninitialized,
bss segment determined by the operating system
initialized) data segment
● Program (as it runs) uses new and malloc
code text segment
address
to dynamically allocate memory, gets
0 space on stack during function calls
3 Fall 1998, Lecture 22 4 Fall 1998, Lecture 22
Linking Why is Linking Difficult?

n Functions of a linker: n When assembler assembles a file, it may


● Combine all files and libraries of a program
find external references — symbols it
doesn’t know about (e.g., printf, scanf)
● Regroup all the segments from each file
together (one big data segment, etc.) ● Compiler just puts in an address of 0 when
producing the object code
● Adjust addresses to match regrouping
● Compiler records external symbols and
● Result is an executable program their location (in object file) in a cross-
reference list, and stores that list in the
n Contents of object files: object file
● File header — size and starting address ● Linker must resolve those external
(in memory) of each segment references as it links the files together
● Segments for code and initialized data
n Compiler doesn’t know where program will
● Symbol table (symbols, addresses) go in memory (if multiprogramming,
● External symbols (symbols, location) always 0 for uniprogramming)
● Relocation information (symbols, location) ● Compiler just assumes program starts at 0
● Debugging information ● Compiler records relocation information
(location of addresses to be adjusted
● For UNIX details, type “man a.out” later), and stores it in the object file
5 Fall 1998, Lecture 22 6 Fall 1998, Lecture 22

Running the Program —


Loading
Static Memory Allocation

n The loader loads the completed program n Compiling, linking, and loading is
into memory where it can be executed sufficient for static memory
● Loads code and initialized data segments ● Code, constants, static variables
into memory at specified location
● Leaves space for uninitialized data (bss) n In other cases, static allocation is not
sufficient:
● Returns value of start address to
operating system ● Need dynamic storage — programmer
may not know how much memory will be
n Alternatives in loading (next 2 lectures…) needed when program runs
n Use malloc or new to get what’s
● Absolute loader — loads executable file at necessary when it’s necessary
fixed location n For complex data structures (e.g., trees),
allocate space for nodes on demand
● Relocatable loader — loads the program
at an arbitrary memory location specified ● OS doesn’t know in advance which
by OS (needed for multiprogramming, not procedures will be called (would be
for uniprogramming) wasteful to allocate space for every
n Assembler and linker assume program will variable in every procedure in advance)
start at location 0
● OS must be able to handle recursive
n When program is loaded, loader modifies
procedures
all addresses by adding the real start
location to those addresses
7 Fall 1998, Lecture 22 8 Fall 1998, Lecture 22
Running the Program — Running the Program —
Dynamic Memory Allocation Dynamic Memory Allocation (cont.)

n Dynamic memory requires two n Two basic methods of allocation:


fundamental operations: ● Heap
● Allocate dynamic storage n Used when allocation and freeing are not
predictable
● Free memory when it’s no longer needed
n Typically used:
● Methods vary for stack and heap – for arbitrary list structures, complex data
organizations, etc.
n Two basic methods of allocation: n Use new or malloc to allocate space, use
delete or free to release space
● Stack (hierarchical) n System memory consists of allocated
n Good when allocation and freeing are areas and free areas (holes)
somewhat predictable n Problem: eventually end up with many
n Typically used: small holes, each too small to be useful
– to pass parameters to procedures – This is called fragmentation, and it leads to
– for allocating space for local variables wasted memory
inside a procedure – Fragmentation wasn’t a problem with stack
– for tree traversal, expression evaluation, allocation, since we always add/delete
parsing, etc. from top of stack
n Use stack operations: push and pop – Solution goal: reuse the space in the
holes in such a way as to keep the number
n Keeps all free space together in a of holes small, and their size large
structured organization
n Compared to stack: more general, less
n Simple and efficient, but restricted efficient, more difficult to implement
9 Fall 1998, Lecture 22 10 Fall 1998, Lecture 22

You might also like