Вы находитесь на странице: 1из 33

TAB2023/TBB2023

OPERATING SYSTEMS
Memory Management

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Overview
Memory allocation and protection
Memory Management, Early Systems

Single-user
Fixed partitions
Dynamic partitions
Relocatable partitions

Memory Management, Recent Systems

Swapping
Paging and Segmentation
Page replacement algorithms
Virtual Memory

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Introduction
The main purpose of a computer system is
to execute programs and together with the
data they access, must be in main memory
(at least partially) during execution.
To improve both the utilisation of CPU and
the speed of its response to its user, the
computer must keep several processes in
memory
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Introduction
There are many different memory management
schemes. These schemes reflects various
approaches to memory management, and
effectiveness of the different algorithm depends
on the particular situation.
Selection of a memory management scheme for
a specific system depends on many factors,
especially on the hardware design of the system
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Background
Memory is central to the operation of a modern
computer system. It is a large array of words or
bytes, each with its own address.
The CPU fetches instruction from memory
according to the value of the program counter.
Then, this instruction may cause additional
loading from and storing to specific memory
addresses

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Storage Structure and Hierarchy

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Memory Management
A fundamental task of memory management
component of an O/S is to ensure safe
execution of programs by providing:
Sharing of memory
Memory protection

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Issues in sharing memory


Transparency

Efficiency

Several process may co CPU utilisation must be


exist, unaware of each
preserved and memory
other, in the main memory
must be fairly allocated
and run regardless of the

Relocation
number and location of
Ability of a program to
process
run in different memory
Safety (or protection)
allocation
Processes must not corrupt
each other (nor the OS)
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Memory Protection
The second fundamental task of a memory
management system is to protect program sharing the
memory from each other. This protection also covers
the operating system itself. Memory protection can be
provided at either of the two levels:
Hardware
Address translation

Software
Language dependent: strong typing
Language independent: software fault isolation
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

Memory Allocation
Information stored in main memory can be
classified in a variety of ways:
Program(code) and data (variables, constants)
Read-only (code, constants) and read-write (variables)
Address (e.g., pointer) or data (other variables); binding
(when memory is allocated for the object): static or
dynamic

The compiler, linker, loader and run-time


libraries all co-operate to manage this
information
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

10

Creating an executable code


Before a program can be executed by the
CPU, it must go through several steps:
Compiling (translating): generates the object
code
Linking: combines the object code into a single
self-sufficient executable code
Loading: Copies the executable code into
memory
Execution: dynamic memory allocation
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

11

From source to executable code

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

12

Address binding (relocation)


The process of association program
instruction and data (addresses) to physical
memory addresses is called address binding,
or relocation
Static
Dynamic

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

13

Address binding (relocation)


Static: new location are determined before
execution
Compile time: The compiler or assembler
translates symbolic addresses (e.g. variables) to
absolute address
Load time: The compiler translate symbolic
addresses to relative (relocatable) addresses.
The loader translates these to absolute
addresses.
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

14

Address binding (relocation)


Dynamic: New locations are determined
during execution
Run time: The program retains its relative
addresses. The absolute addresses are generated
by hardware

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

15

Function of a linker
A linker collects (if possible) and puts
together all the required pieces to form the
executable code.
Issues:
Relocation: where to put pieces
Cross-reference: where to find pieces
Re-organisation: New memory layout
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

16

Contd...
The figure illustrated
how a program is
stored in a memory

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

17

Function of a loader
A loader places the executable code in main memory
starting at a pre-determined location (base or start
address). This can be done in several ways, depending
on hardware architecture:
Absolute loading
Always loads programs into a designated memory location

Relocatable loading
Allows loading programs in different memory locations

Dynamic (run time) loading


Loads functions when first called (if ever)
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

18

Memory Allocation Scheme:


Early System
The management of main memory is critical.
Four types of memory allocation schemes

Single-user systems
Fixed partitions
Dynamic partitions
Re-locatable dynamic partitions

Memory performance is dependent on two things:


how much memory is available
how it is optimized while jobs are being processed
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

19

Single-User Contiguous Scheme


First memory allocation
scheme
early computers had limited
memory
jobs processed sequentially
OS algorithm used to allocate
memory

did not support


multiprogramming

Each program loaded in its


entirety into memory and
allocated as much contiguous
memory space as needed.
Saipunidzam Mahamad
2011

If program was too large it couldnt be executed.


Minimal amount of work
done by Memory Manager.
Hardware needed :
register to store base
address;
accumulator to track size of
program as it is loaded into
memory.

/TAB2023/TBB2023 Operating Systems- Jan

20

Algorithm to Load a Job


in a Single-user System
1.
2.
3.
4.
5.

Store first memory location of program into base register


Set program counter equal to address of first memory location
Load instructions of program
Increment program counter by number of bytes in instructions
Has the last instruction been reached?
If yes, then stop loading program
If no, then continue with step 6
6. Is program counter greater than memory size?
If yes, then stop loading.
If no, then continue with step 7
7. Load instruction in memory
8. Go to step 3.
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

21

Fixed (Static) Partitions


Attempt at multiprogramming using fixed partitions
Created within main memory where one partition for each job
size of partition designated by reconfiguring the system
partitions cant be too small or too large.

Entire program stored contiguously in memory during entire


execution.
Critical to protect jobs memory space
To protect jobs memory space critical
algorithm used to store jobs in memory, size of job must match size
of partition
major drawback: internal fragmentation is unused space within the
fixed partition
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

22

Fixed-Partitions
Once partition was assigned to a job, no other
job could be allowed to enter its boundaries,
either accidentally or intentionally
The algorithm require a few more steps than the
one used for a single-user system because the
size job must be matched with the size of the
partition to make sure it fits completely. Then,
when a block is allocated, the status of the
partition must be checked to see if its available
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

23

Algorithm to Load a Job in a


Fixed Partition
1. Determine jobs requested memory
size
2. If job_size > size of largest
partition then reject job
Print appropriate message
Go to step 1 to handle next job
Else continue with step 3
3. Set counter to 1
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

24

Algorithm to Load a Job in a


Fixed Partition
4. Do while counter <=
number of partitions in
memory
If job_size >
mem_partition_size
(counter)
then counter =
counter + 1
Else
If mem_partition_status
(counter) = free
Saipunidzam Mahamad
2011

then load job into


mem_partition(counter)
change
mem_partition_status(counter)
to busy
go to step 1
Else counter = counter + 1
End do
5. No partition available at this
time, put job in waiting queue
6. Go to step 1

/TAB2023/TBB2023 Operating Systems- Jan

25

Example 1: Fixed Partition


200K
100K

Job List:
Job 1
Job 2
Job 3
Job 4

30K
50K
30K
25K

25K
25K

Partition 1

300K
325K
350K

Partition 2
Partition 3
Partition 4

50K
400K
Main Memory

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

26

Table 2.1 : Main memory use during


fixed partition allocation of Table 2.1.
Job 3 must wait.

Job List :
J1
30K
J2
50K
J3
30K
J4
25K

Original State
100K

After Job Entry


Job 1 (30K)

Partition 1

Partition 1
Partition 2

25K

Partition 3

25K

Partition 4

Saipunidzam Mahamad
2011

50K

Job 4 (25K)

Partition 2
Partition 3

Job 2 (50K)

Partition 4

/TAB2023/TBB2023 Operating Systems- Jan

27

Simplified Fixed Partition


Memory Table (Table 2.1)
Partition
size
100K
25K
25K
50K

Saipunidzam Mahamad
2011

Memory
address
200K
300K
325K
350K

Access
Job 1
Job 4
Job 2

/TAB2023/TBB2023 Operating Systems- Jan

Partition
status
Busy
Busy
Free
Busy

28

Basic Dynamic Partition


Available memory kept in contiguous blocks and jobs
given only as much memory as they request when
loaded.
Improves memory use over fixed partitions.
Performance deteriorates as new jobs enter the system
fragments of free memory are created between blocks of allocated
memory (external fragmentation).

first-come first-served priority basis


subsequent allocation of memory creates fragments
called external fragmentation, free memory blocks between
allocated memory
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

29

Example 2 - Dynamic Partition


Job List
Job 1
Job 2
Job 3
Job 4
Job 5
Job 6
Job 7
Job 8
Saipunidzam Mahamad
2011

Memory Size Time


10K
5
15K
20
20K
10
50K
5
5K
10
30K
10
10K
5
15K
10

/TAB2023/TBB2023 Operating Systems- Jan

30

Dynamic Partitioning of Main


Memory & Fragmentation (Figure 2.2)

Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

31

Example 2 - Dynamic Partition


Given the following information:
Job

Size

Time

J1

500K

15

J2

700K

J3

1000K

J4

300K

10

J5

600K

Operating
System

0
400K

Free
Space =
2160K

2560K

Use the dynamic partition to allocate the memory


blocks to the arriving job.
Saipunidzam Mahamad
2011

/TAB2023/TBB2023 Operating Systems- Jan

32

Summary
View of OS architecture

System Call
System structure
OS kernel
MS DOS and Unix Structure

Memory management

Introduction
Issues in sharing memory
Memory Allocation
Several step before program can be
executed by the CPU; compiling,
address binding, linker, loader
Single-user systems,Fixed
partitions and Dynamic Partition
Saipunidzam Mahamad
2011

Read textbook chapter 2


Memory management early system
Adv Dynamic Partition

Extra reading: Operating


System Concepts
Abraham Silberschatz,
Peter B Galvin, john Wiley
Chapter 1, 2 and 8

/TAB2023/TBB2023 Operating Systems- Jan

33

Вам также может понравиться