Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

8.1. Memory Management

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Memory Management

Memory Management Tasks


• Accommodation of multiple processes in memory

• Memory needs to be allocated efficiently


• Allow as many processes into memory as possible.
• To fit as larger processes into memory as possible.
• Improve the overall performance of the system.

Memory Operations
• Fetch instruction from the Memory
• Operands fetching from the memory
• Store the result back into the memory

• Sometimes we can ignore how a memory address is generated


by a program.
• and be interested in only the sequence of memory addresses
generated by the running program.
How the process interacts with the
memory during its lifetime

Swapping
Relocating in
memory
Execution Swap
Allocating
Input memory space
Queue
Process
accesses
The collection of processes instructions and
waiting to be brought into data from
Termination
memory Freeing memory
memory for execution
Address Binding
• Most systems allow a user process to reside in any part of the physical
memory.
• Although the address space of the computer starts at 00000, the first
address of the user process does not need to be 00000.

• Addresses in the source program are generally symbolic (such as count).

• A compiler will typically bind these symbolic addresses to relocatable


addresses (such as "14 bytes from the beginning of this module").

• The linkage editor or loader will in turn bind these relocatable addresses
to absolute addresses (such as 74014).

• Each binding is a mapping from one address space to another.


How the binary code could be relocatable ?
.text 0x00400000 recall
.globl main Code could be However the whole code is not
. . . relocatable because relocatable because this
.data 0x10010000 this address forming is address forming is absolute. We
x: .word 4 relative need to recompile for
value: .word 1 relocation.
a: .word 20
Address Binding at compile time
Binary code is different (non relocatable)
Code could be relocatable However the whole code is not
because this address relocatable because this address
forming is relative forming is absolute. We need to
recompile for relocation.

.text 0x00420000 .data 0x10020000


.globl main x: .word 4
.• . If. the instructions contain absolute
value: .word 1
addresses then that codea: is not
.word 20
bb: .word -2
relocatable.

• To make the code relocatable the


compiler should use only relative
addresses based on some base register(s)
value.
Address Binding at link time
Load Module (Executable File)
Header (parameters and a small program -
loader)
------------------------------------------------
0040 0000 Variable 1
Subroutine 1()
Object Module 1
Other Subroutines ...

Load Address Variable2 AddressV2


Jal Subroutine2 AddressS2

Other Subroutines ...


-----------------------------------------------------------------------
0040 1000 Variable2
Object Module 2 Subroutine 2()

Other Subroutines ...

Load Address Variable 1 AddressV1


Jal Subroutine 1 AddressS1

Other Subroutines ...

 Object Modules can be relocatable


 But the linker can bind the module addresses to the absolute addresses
Address Binding
Compile time: If you know at compile time
Address binding where the process will reside in memory, then
could be done at
absolute code can be generated. If, at some
(code becomes
later time, the starting location changes, then it
not relocatable) will be necessary to recompile this code.
Static Link time: . . .

Address binding could be Load time: If it is not known at compile time


done at static link time
(code should be compiled
where the process will reside in memory, then
as relocatable) the compiler must generate relocatable code.
Address binding In this case, final binding is delayed until load
could be done at
time. If the starting address changes, we need
only to reload the user code to incorporate this
(code should be
compiled and linked changed value.
as relocatable)

Execution time: If the process can be moved


Address binding
during its execution from one memory
could be done at segment to another, then binding must be
delayed until run time. Special hardware must
(needs special
be available for this scheme to work. Most
Address binding could hardware) general-purpose OS use this method.
be done at dynamic link
time Dynamic Link time: . . . (again run time)
Logical Versus• Physical Address
How many relocation Space
registers we need for 3
processes running concurrently on one CPU?
• an address generated by the CPU is commonly referred to as a logical address (virtual
address)
• How many relocation registers we need for 3
• whereas an address seen by the memory unit is commonly referred to as a physical
address. processes running on two CPUs?

• The set of all logical


Logical Address 000000 addresses generated by a
Space program is a logical-address
000 space
... Physical • The set of all physical
346 Address addresses corresponding to
... Space
these logical addresses is a
500 14000
... physical-address space.

Virtual 14500 • Thus, in the execution-time


Address address-binding scheme, the
logical- and physical-address
spaces differ.
FFFFFF
• The run-time mapping from
virtual to physical addresses
Base or relocation register scheme is done by a hardware device
called the memory-
000 - 500 R+000 .. R+500 management unit (MMU)
Memory Protection
The goals
• The operating system should be protected from access by user processes
• User processes should be protected one from another

How to implement
• We first need to make sure that each process has a
separate memory space
• and can access only that memory space
• we need the ability to determine the range of
legal addresses
• We can provide this protection by using two
registers, usually a base and a limit
• The base register holds the smallest legal
physical memory address;
• the limit register specifies the size of the
range
• This protection must be provided by the hardware.
Memory Protection Hardware
• hardware compares every address generated in user mode with the registers.
• Any attempt by a program executing in user mode to access operating-system
memory or other users' memory results in a trap to the operating system, which treats
the attempt as a fatal error.
• This scheme prevents a user program from (accidentally or deliberately) modifying
the code or data structures of either the operating system or other users.

• The base and limit registers can be loaded only by the operating system, which uses a special
privileged instruction.
• The operating system, executing in kernel mode, is given unrestricted access to both operating
system memory and users' memory.
• This provision allows the operating system to load users' programs into users' memory, to dump out
those programs in case of errors, to access and modify parameters of system calls, and so on.
Memory Protection and
• How Relocation
many Hardware
relocation and limit registers we
need for 3 processes running concurrently on
• With relocation and limit registers one CPU?
• each logical address must be less than the limit register;
• the MMU maps the logical• address dynamically
How many by adding
relocation and the value
limit in the we
registers
relocation register. This mapped address is sent to memory

need for 3 processes running on two CPUs?
When the CPU scheduler selects a process for execution, the dispatcher loads the
relocation and limit registers with the correct values as part of the context switch.
• Can the user program change the relocation
74600 and limit registers values to access the
Max logical physical storage wherever it needs?
address 100040

0-74600 100040

100145
105

100040 + 74600
Dynamic Loading
Loading of some modules or libraries is being postponed until their
execution time.
• Routine is not loaded until it is called
• Better memory-space utilization; unused routine is never loaded.
• Useful when large amounts of code are needed to handle infrequently occurring
cases.
• OS not involved in the modules organization. The user program is responsible for it.
So module sharing between different protected processes is not possible

Symbol
Table

Common
Routines
Linked final Linked final
binary module binary module
Relocatable
loader
Module
1 Module
Modules are not loaded 2
until they are called
Dynamic Linking and Shared Libraries
Rather than loading being postponed until execution time, linking is postponed.
OS involved in the modules organization. So module sharing between different protected
processes is possible. This is shared libraries approach.

Static Linking Dynamic Linking


Main Main Main Main Main Main Not finally linked
modules modules modules modules modules modules binary modules
of of of of of of
Program1 Program2 Program3 Program1 Program2 Program3

Statically Statically Statically


linked linked linked Final binary code
Library1 Library1 Library1 Dynamically is formed by
Linked linking at runtime
Library1 when the library is
Statically Statically Statically
linked linked linked Not finally linked Dynamically called
Library2 Library2 Library2 binary modules Linked
Library2

Advantages of DLL
 Fast loading of the main module and of the modules needed at this moment
 Saving resources using one library instead of many
 Modular architecture
 Flexibility of module update

Disadvantage: The sharing of the same library between different processes and the
frequent updates cause version conflict and complexity of update implementation.
Swapping
• A process needs to be in memory to be executed.
• A process, however, can be swapped temporarily out of memory to a backing
store
• and then brought back into memory for continued execution
• Normally a process that is swapped out will be swapped back into the same
memory space that it occupied previously.

• If binding is done at assembly


or load time, then the process
cannot be moved to different
locations.

• If execution-time binding is
being used, then a process can
be swapped into a different
memory space, because the
physical addresses are
computed during execution
time.

You might also like