Sie sind auf Seite 1von 36

UNIT – 4 MEMORY MANAGEMENT

Main Memory:
Background
Swapping
Memory allocation:
Contiguous Memory Allocation
- Single partition allocation
- Multiple partition allocation
Non – contiguous allocation
- Paging
- Segmentation
- Segmentation with paging
Fragmentation and defragmentation
Structure of the Page table
Virtual Memory:
Background
Demand Paging
Copy-on-Write
Page Replacement
Allocation of Frames
Thrashing
UNIT – 4
Memory management
The main purpose of a computer is to execute programs.
These programs, together with the data they access, must be in main memory during
execution.
Variety of different memory units are:
1. Internal memory
- Registers
- Cache
- Main memory (RAM)
2. Secondary memory

Many memory management schemes exist, reflecting various approaches, and the effectiveness
of each algorithm depends on situation.
UNIT – 4
Memory hierarchy of the system:

Access time in (ns) Managed by


Registers ( Vo )
 0. 25 – 0.5 Compiler

Cache ( Vo )
0.5 – 25 H/W

Main Memory ( Vo )
80 – 250 O/S

Secondary Memory (Non - Vo ) 5000 O/S


About RAM:
• Computer uses ram memory to store actively running program on the computer including
operating system.
• Ram can 100 times faster than hard drive
• Program (or) data residing in the ram then only it can accessed by CPU other wise not.
UNIT – 4
Memory management:
All form of memory provide an array of bytes each with its own address.

A verity of technologies are used to implement memory systems:


1. Faster access time, Greater cost per bit
2. Greater capacity, Smaller cost per bit
3. Greater capacity, Slower access speed

Memory hierarchy goes down:


 Decreasing cost per bit
 Increasing capacity
 Increasing access time
 Decreasing the frequency of the memory accessed by CPU
UNIT – 4
Main memory and the registers are general-purpose storage that the CPU
can access directly.

Main memory divided into two parts:


 OS area [ Never available for user ]

 User area

 User area may try to access (or) write something into OS area leads to system crash.
 We must ensure correct operation to protect the OS from access by user processes.

This protection must be provided by hardware and software. [ S/W is slower process]

H/W Solution:
By Using two registers
1. Base Register
Hold the smallest legal physical memory address
2. Limit Register
Specifies the size of range
UNIT – 4
 If the base register holds 300040 and the limit register is 120900, then the program
can legally access all addresses from 300040 through 420939

 Make sure that each process has a separate memory space and process can access
only these legal space.
UNIT – 4
Protection of memory space is accomplished by having the CPU hardware compare
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 user’s memory results in a trap to the operating system, which
treats the attempt as a fatal error.
This scheme prevents a user program from modifying the code or data structures of
either the operating system or other users.
UNIT – 4
Address binding:
 System allows the user to create user program and store it in secondary memory.
 The program must be brought into memory within a process for it to be executed.
 During execution of the program passes through different phases such as compilation,
loading, running etc..

Problem is that,
 Where should as OS store the results of program after their execution ?
Example:
x = ( a+ b) * ( a – c )
 Compute above instruction and store the result in variable x. such address given by
the user,
like x in our example are called as symbolic or logical address.
 These address need to be mapped to real physical address in the memory. This
process of mapping logical address to real address in the memory is called as address
binding.
UNIT – 4
This binding addresses of instruction and data to
actual physical address may take place during:

Compile time:
If you know at compile time where the process will
reside in memory, then absolute code can be generated.

Load time:
If it is not know at compile time where the process
will reside in memory, then the compiler must
generate relocatable code.

Execution time:
If the process can be moved during its execution from
one memory segment to another, then binding must be
delayed until run time.
UNIT – 4
Two types of address:
1. Logical address
Generated by CPU also referred to as virtual address.
2. Physical address
Generated by memory unit.

 The run-time mapping from virtual to physical addresses is done by a H/W device
called as memory management unit (MMU)
UNIT – 4
User program deals with logical address never sees real physical address.

MMU Memory

Base register called OS


Logical Physical
as relocation P1
Address register Address
346 14346 P2
CPU +
P3
14000
Pn

Physical address space = logical address space + relocation register


Logical address range starts from 0 to max.

 The set of all logical address are generated by program is referred to as logical address
space.
 The set of all physical address corresponding to these logical addresses is a physical
address space
UNIT – 4
Swapping:
Swapping is a method to improve the main memory utilization.

 Switch a process from main memory to disk said to be “ SWAPOUT ”


 Switch a process from disk to main memory said to be “ SWAP IN “
This type of mechanism is called as swapping.
Let us assume that the user process is of size 100KB and the backing store is a
standard hard disk with transfer rate of 1 MB per second.
=100KB / 1024KB per second
= 0.097 milliseconds
UNIT – 4
Memory allocation techniques:

1. Contiguous allocation [Each process is contained in a single contiguous section of memory]


- Single partition allocation
- Multiple partition allocation
- Fixed equal
- Fixed variable
- First fit
- Best fit
- Worst fit
- Dynamic
-Fragmentation
2. Non – contiguous allocation
- Paging
- Segmentation
- Segmentation with paging

Two challenges:
1. Os code and data are protected from changes made by the user processes.
2. Protect the user process from one another.
UNIT – 4
Single partition allocation:
OS resides in low memory, remain memory treated as single partition.
 Only one process can be loaded in user space.

0
OS
500
P1
 4999

Merits:
 Simplicity
Demerits:
 Poor utilization of memory
UNIT – 4
Multiple partition:
Fixed equal
In this memory management scheme the OS occupies the low memory and rest of memory
available for user.
Memory is divided into fixed single partition each partition can have exactly one process.
Merits:
- Simple and easy implementation.
- This scheme support multi programming.
-It require no special costly hardware.
Demerits:
- This scheme suffer from internal as well as external fragmentation.
[REFER PROBLEM]
UNIT – 4
Fixed variable
Main memory is divided into number of partitions, but the partition sizes are different length.
OS keeps the table indicating which partitions of memory are available called as hole.
Three algorithm are available:
1. First fit
Allocate the first hole that is big enough. [ Search starts from beginning of the hole]
Merits and demerits:
Usually fastest, but not always best use of memory space.
2. Best fit
Allocate the smallest hole that is big enough. [ Search entire list]
3. Worst fit
Search entire partition allocate largest hole.
Merits and demerits:
First and best better than worst in terms of speed and storage utilization. [REFER PROBLEM]
Dynamic
In this method partitions are created dynamically, So that each process loaded into partition of
exactly same size .
UNIT – 4
Fragmentation:
As processes are loaded and removed from memory, the free memory space is
broken into little pieces. It happens after sometimes that processes can not be
allocated to memory blocks considering their small size and memory blocks remains
unused. This problem is known as Fragmentation.
Fragmentation is of two types:
Internal fragmentation Memory block assigned to process is bigger. Some portion
of memory is left unused as it can not be used by another process.
UNIT – 4
External fragmentation Total memory space is enough to satisfy a request or to
reside a process in it, but it is not contiguous so it can not be used.
UNIT – 4
One solution to the problem of external fragmentation is compaction.
UNIT – 4
Another possible solution to the external-fragmentation problem is to permit the logical address space of the
processes to be noncontiguous, thus allowing a process to be allocated physical memory wherever such
memory is available.

Paging [ Non- Contiguous memory allocation method ]


 Paging is a storage mechanism used to retrieve processes from the secondary storage into the main memory in the
form of pages.
 Main memory is divided into fixed size of blocks called as FRAMES. (Size should be a power of two)
Processes divided into fixed size of blocks called as PAGES.

 Pages of the process are brought into the main memory only when
they are required otherwise they reside in the secondary storage.

 This translation is done by address mapping in a paging system.


UNIT – 4
Paging:
UNIT – 4
Paging:
UNIT – 4
 Address mapping in a paging system

LA: Logical Address


PA: Physical Address
PMT: Page map table / Page table
MMU: Memory Management Unit
Offset (d) –Displacement within the page
[ Combined with base address to define the physical memory address that is sent to the memory unit]
UNIT – 4
Operation:
 During process execution, CPU generated a logical address, that comprises of page number (p)
and offset within the page (d).
 The page number (p) is used to index into the page table and fetch corresponding frame number
(f).
 The physical address is obtained by combining the frame number (f) with the offset (d).
 PMT – Page Map Table that contains the starting address or base address of each page stored in
physical memory.
 Page size and frame size is defined between 512 bytes to 4 KB depending upon the computer
architecture.
 The size of a page is typically powers of 2. [ 29 bits to 214 bits ]
 The two memory access ( Data / Instruction ) problem can be solved by the use of a special fast
lookup hardware called as associative registers or translation look-aside buffers (TLBs) available
in page map table.
UNIT – 4
Memory Protection in a paged environment:
Memory protection in a paged environment is accomplished by protection bits
associated with each frame. Normally, these bits are kept in the page table.
UNIT – 4
Structure of the Page Table:
1. Hierarchical Paging
2. Hashed Page Tables
3. Inverted Page Tables
<Refer Book>
UNIT – 4
Segmentation:
Segmentation can be defined as a logical grouping of instruction such as:
- Subroutines (or) Procedures
- Functions
- Data area and so on…
Every program is a collection of these segments
Example: Programmer’s view of there program
[ Break the program into number of segments, Segments are different in size unique number
allotted to every segment, address mapping done by segment table with help of MMU]
UNIT – 4
Logical address to physical mapping in a Segmented system
UNIT – 4
Operation:
 An important table that is used during address mapping is a segment table.
 Segment table used by MMU. It contains entry for each segment of a process.
 Segment table contains the base [Starting address of the physical address where the
segment resides in memory] and segment size [size of the segment]
 Logical address contains two part: segment number and offset into that segment
 The segment number provided in the virtual address used as an index into segment
table.
 The offset of the logical address must be less than or equal to size of the segment.
UNIT – 4
Advantages of paging:
 No external fragmentation.
 Simple memory management algorithm.
 Simple to SWAP IN, SWAP OUT portions of memory to disk and vise versa.
 Paging is faster than segmentation.
 Divide pages and frames are managed by operating system.
Disadvantages of paging:
 Some situations suffer with internal fragmentation
 Page table consumes lot of memory and equal in size, so memory reference is hard.
Advantages of segmentation:
 No internal fragmentation. [ due to dynamic allocation ]
 Segment table consumes less of memory, so memory reference is easy.
 User is responsible for dividing the program into segments.
Disadvantages of segmentation:
 Some situation suffer with external fragmentation [ Need compaction ]
 Cost of memory management algorithm is high.
 Unequal size of segment is not good in case of swapping segmentation.
 Slower than paging.
UNIT – 4
Virtual memory:
 Aim of the memory management technique to keep many processes in memory simultaneously
to allow multiprogramming.
 Process size is not exceeds size of the main memory, in some situation it exceeds the size of the
main memory.
Solution is: Virtual memory
 Virtual memory is a technique of executing program instruction (process) that may not fit
entirely in the main memory.
 Process is divided into equally sized pages that initially reside in secondary memory.
 When the process begin to run, pages are brought into memory as they needed.

Demerits:
 Virtual memory is not easy to implement if it is used careless means leads to decrease the
performance of the system.
UNIT – 4
Demand paging: Is the application of virtual memory
 Virtual memory can be implemented as a extension of paged memory scheme called as demand
paging [ Similar to swapping]
 Pages are only loaded when they are demanded during program execution. Otherwise never
accessed and never loaded into main memory.
UNIT – 4
Page fault:
 When the processor needs to execute a page, and if that page is not available in main
memory then the situation is called as “PAGE FAULT”.

 When the page fault occurs. The page replacement will be done.

 For bringing in the required page into main memory, if the space is not available in
memory then we need to remove the page from main memory for allocating the space
to the new page which needs to be executed.

 In this process the page which we need to remove from the main memory is called as
“Victim page”.

 For the selection of victim page in main memory different algorithms are available
called page replacement algorithm.

 An algorithm having the least page fault rate will be the best one.
UNIT – 4
Some of the widely using page replacement algorithms are as follows:

 First-In, First-Out (FIFO) algorithm


Algorithm says: To Replace the page that has been in memory longest.

 Optimal algorithm
Algorithm says: Replace a page that will not be used for the longest period
of time. [it requires future knowledge of reference string]

 Least Recently Used (LRU) algorithm


Algorithm says: Replace the page that has not been used for the longest
period of time (or) looking backward in time, rather than forward.

 Counting-based Page replacement


 Least Frequently Used (LFU) algorithm
 Most Frequently Used (MFU) algorithm [REFER PROBLEM]
UNIT – 4
Be lady's Anomaly:
General principle is if the number of frames increased, the page fault rate will be
decreased.
Example one:
Reference string are:
70304230321201701
With 3 frames, 15 page fault
With 4 frames, 12 page fault

Example two:
Reference string are:
123412512345
With 3 frames, 9 page fault
With 4 frames, 10 page fault

Note: Only applicable for FIFO algorithm


UNIT – 4
------------------------------------------- REFER BOOK -------------------------------------------
o Segmentation with paging
o Allocation of frames
o Thrashing

Das könnte Ihnen auch gefallen