Lecture 3 - Memory Management.ppt

33
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011 1 TAB2023/TBB2023 OPERATING SYSTEMS Memory Management

Transcript of Lecture 3 - Memory Management.ppt

STB2063 OPERATING SYSTEMTAB2023/TBB2023
Overview
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 /TAB2023/TBB2023 Operating Systems- Jan 2011
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 /TAB2023/TBB2023 Operating Systems- Jan 2011
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 /TAB2023/TBB2023 Operating Systems- Jan 2011
Storage Structure and Hierarchy
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
Issues in sharing memory
Transparency
Several process may co-exist, unaware of each other, in the main memory and run regardless of the number and location of process
Safety (or protection)
Efficiency
CPU utilisation must be preserved and memory must be fairly allocated
Relocation
Ability of a program to run in different memory allocation
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
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
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
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 /TAB2023/TBB2023 Operating Systems- Jan 2011
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
From source to executable code
2.psd
Address binding (relocation)
The process of association program instruction and data (addresses) to physical memory addresses is called address binding, or relocation
Static
Dynamic
Address binding (relocation)
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 /TAB2023/TBB2023 Operating Systems- Jan 2011
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 /TAB2023/TBB2023 Operating Systems- Jan 2011
Function of a linker
A linker collects (if possible) and puts together all the required pieces to form the executable code.
Issues:
Re-organisation: New memory layout
Cont’d...
The figure illustrated how a program is stored in a memory
3.psd
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
Relocatable loading
Dynamic (run time) loading
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
how much memory is available
how it is optimized while jobs are being processed
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
Single-User Contiguous Scheme
jobs processed sequentially
did not support multiprogramming
Each program loaded in its entirety into memory and allocated as much contiguous memory space as needed.
If program was too large - it couldn’t 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.
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
Algorithm to Load a Job
in a Single-user System
1. Store first memory location of program into base register
2. Set program counter equal to address of first memory location
3. Load instructions of program
4. Increment program counter by number of bytes in instructions
5. 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 /TAB2023/TBB2023 Operating Systems- Jan 2011
Fixed (Static) Partitions
Created within main memory where one partition for each job
size of partition designated by reconfiguring the system
partitions can’t be too small or too large.
Entire program stored contiguously in memory during entire execution.
Critical to protect job’s memory space
To protect job’s 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 /TAB2023/TBB2023 Operating Systems- Jan 2011
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 /TAB2023/TBB2023 Operating Systems- Jan 2011
Algorithm to Load a Job in a
Fixed Partition
2. If job_size > size of largest partition then reject job
Print appropriate message
Else continue with step 3
3. Set counter to 1
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
Algorithm to Load a Job in a
Fixed Partition
If job_size > mem_partition_size (counter)
then counter = counter + 1
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
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
Example 1: Fixed Partition
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
Table 2.1 : Main memory use during fixed partition allocation of Table 2.1. Job 3 must wait.
Original State
Simplified Fixed Partition
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
called external fragmentation, free memory blocks between allocated memory
Saipunidzam Mahamad /TAB2023/TBB2023 Operating Systems- Jan 2011
Example 2 - Dynamic Partition
Dynamic Partitioning of Main Memory & Fragmentation (Figure 2.2)
46.bin
Example 2 - Dynamic Partition
Given the following information:
Use the dynamic partition to allocate the memory blocks to the arriving job.
Operating System
Summary
Memory management
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
Read textbook chapter 2
Memory management - early system
Abraham Silberschatz, Peter B Galvin, john Wiley
Chapter 1, 2 and 8
Partition
size
Memory
address
Access
Partition
status
100K
200K