0% found this document useful (0 votes)
0 views

Chapter 3 - Memory Managment

The document provides an overview of memory management in operating systems, discussing concepts such as physical memory, virtual memory, paging, and segmentation. It outlines the responsibilities of the memory manager, including tracking memory usage, allocation, and deallocation, as well as various memory management techniques like swapping and page replacement policies. Additionally, it covers the importance of efficient memory management to optimize CPU utilization and system performance.

Uploaded by

naemader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter 3 - Memory Managment

The document provides an overview of memory management in operating systems, discussing concepts such as physical memory, virtual memory, paging, and segmentation. It outlines the responsibilities of the memory manager, including tracking memory usage, allocation, and deallocation, as well as various memory management techniques like swapping and page replacement policies. Additionally, it covers the importance of efficient memory management to optimize CPU utilization and system performance.

Uploaded by

naemader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1

CHAPTER 3
Memory management

Over view of physical memory and


memory management
•Hardware overlays
•Partitioning
•Swapping
Introduction to Virtual Memory
•Paging and segmentation
•Page Replacement Policies.
3

Introduction
• Memory is an important resource that needs to be
managed properly.
• Why?-
• they are limited in nature!
• What is programmers/expert users need?
• Private, infinitely large and fast memory that is also nonvolatile.
• Can the technology provide that?
• NO but, may be in the future.
• Hence the operating system owes to manage the
memory.
• How?
4

Introduction…
 The OS has a part called Memory manager that manipulates
and perform an operation on memory hierarchy.
<1
Registers
nsec

2 nsec
Cache Memory
10 nsec
Main Memory
10 msec
Magnetic Disk
100 sec
Magnetic Tape

◦ Hence the MM has a responsibility to


 Keeps track of which is in use or not.
 Allocate and De allocate
 Manage Swapping and etc.
5

Memory Management
 Memory management systems are divided in to
two classes.
◦ Those that move processes back and forth b/n main
memory and disk (paging and swapping) b/c of
memory insufficiency
◦ Those that do not (simpler)

 Also the realm of software tech. persuade the


creation of efficient memory.
6

Simpler ones… mono-programming

0X FFF Devices drivers


OS in ROM in ROM

User Program
User Program

User program
OS in RAM OS in RAM
0

Formerly in main Palm tops, Early PC w/r


frames and embedded portion of a
minicomputers systems and Smart system is called
cards BIOS

 In all cases only one process can run at a time


◦ No Swapping and Paging (ex. Three level scheduling in batch system)
7

Multiprogramming with Fixed Partitions


• An easy way to achieve MP is to
divide Memory in to Unequal
partitions.
• For efficient CPU utilization
• Partitioning can be done
manually.
• Fixed memory partitions
• Some space may not be used by
a job hence will be lost
• Multiple input queue
• Disadvantageous in the case of P3
and P1
• Single input queue
• Small Jobs can be unworthy of
having the whole partitions.
• Another approach is implemented
• Always reserve a small partition and
maximum skip time
8

Swapping

• Memory allocation changes as processes come into


memory and leave it.
• The shaded regions are unused memory.
9

Swapping…
 Allocating space
for a growing data
segment.
10

Swapping…
• Allocating space for a
growing stack and a
growing data segment.
11

MM with Bit Maps and Linked List


• As memory is assigned dynamically
OS must manage it.
• There are two ways of keep track of
memory usage
• Bitmaps
• Linked list/free list.
12

MM using Bitmaps
 The Size of
the bitmap
depends on
the size of
memory and
size of the
allocation unit.

 a) A part of memory with five processes and three holes. The tick marks
show the memory allocation units. The shaded regions (0 in the bitmap) are
free.
 (b) The corresponding bitmap.
 (c) The same information as a list.
13

Activity

Represent the following memory status using


bitmap and linked list:
P1 P2 P3 P4

0 8 16 24 32
14

MM using Linked List


• Four neighbor combinations for the terminating process, X.
15

Virtual Memory
 Formerly Overlays were used as a virtual memory.
◦ Splitting done manually by the programmer
◦ Swapping done by the system
But some overlay systems were so complex and expensive to
work with. And the first task, by nature, is time consuming and
boring.
 Hence in 1961 a method of doing every thing using system
is advised.
◦ i.e. Virtual Memory.
 The main idea is to put currently used data, program and
stack in the main memory and the rest in the disk.
◦ i.e. X MB Program can run on YMB machine where X>>Y and X and
Y are size of power of 2
16

Paging
• VM uses a technique called paging.
• For instance- the instruction MOV REG,1000 –copy the content of
memory address 1000 in to REG
17

Paging…
 Pages and page frames are
always equal. Here 4KB
◦ How much pages and page
frames can we get from 64 KB
of Virtual address space and
32 KB of physical memory?
 Lets do the following
instruction.
 MOV REG 0
 MOV REG 8192
will be transformed in to
___, and ____, output will
be moved to bus
What happens if the
program tries to use MOV
REG, 32780
See answer next slide:
18

Answer to instructions MOV REG, 0 and MOV REG 8192:


• Virtual address 0 is sent to the MMU.
• The MMU sees that this virtual address falls in page 0 (0 to
4095), which according to its mapping is page frame 2 (8192
to 12287).
• It thus transforms the address to 8192 and outputs address
8192 onto the bus.
• The memory knows nothing at all about the MMU and just
sees a request for reading or writing address 8192, which it
honors.
• Thus, the MMU has effectively mapped all virtual addresses
between 0 and 4095 onto physical addresses 8192 to 12287.
19

What happens if the program tries to use MOV REG, 32780?


Answer:
• MOV REG, 32780:
• Which is byte 12 within virtual page 8 (starting at 32768);
• The MMU notices that the page is unmapped (indicated by a
cross in the figure) and causes the CPU to trap to the
operating system.
• This trap is called a page fault.
• The operating system picks a little-used page frame and
writes its contents back to the disk.
• It then fetches the page just referenced into the page frame
just freed, changes the map, and restarts the trapped
instruction.
20

Paging … page tables


 Purpose : map virtual
pages onto page Frames
 Major issues to be faced
1. The page table can be
extremely large
2. The mapping must be
fast.
22

Paging … multilevel page tables


• A simple example is shown in Fig of last slide:
• (a), we have a 32-bit virtual address that is
• partitioned into a 10-bit PT1 field, a 10-bit PT2 field, and a 12-
bit Offset field.
• Since offsets are 12 bits, pages are 4 KB, and there are a
total of 220 of them.
• Entry 0 of the top-level page table points to the page table for
the program text, entry 1 points to the page table for the data,
and entry 1023 points to the page table for the stack.
• The shaded entries are not used.
• The PT2 field is now used as an index into the selected
second-level page table to find the page frame number for the
page itself.
23

Page table Entry Structure - sample

 The exact layout of an entry in the page table is highly machine dependent, but
the kind of information present is roughly the same from machine to machine.
 The size varies from computer to computer, but 32 bits is a common size The
most important field is the Pa g e frame number.
 After all, the goal of the page mapping is to output this value.
 Next to it we have the Present/absent bit. If this bit is 1, the entry is valid and can
be used, else the virtual page to which the entry belongs is not currently in
memory.
24

Page table Entry Structure – sample, …. (Key)

• The Protection bits tell what kinds of access are permitted.


• In the simplest form, this field contains 1 bit, with 0 for
read/write and 1 for read only.
• The Modified and Referenced bits keep track of page usage.
• When a page is written to, the hardware automatically sets
the Modified bit.
• The Referenced bit is set whenever a page is referenced,
either for reading or for writing.
• Finally, the last bit allows caching to be disabled for the page.
• This feature is important for pages that map onto device
registers rather than memory.
25

What happens if there is no free frame?


• Page replacement – find some page in
memory, but not really in use, swap it out.
• Algorithm- want an algorithm which will result in
minimum number of page faults.
• performance – reduce overhead
• Same page may be brought into memory
several times.
• Same with cache memory cleaning and
removing heavily used web pages from the
server
26

Page Replacement
• Prevent over-allocation of memory by modifying page-fault service routine to include

page replacement.
• A process may have 10 pages but only 5 are in use.

• Demand paging only brings in those 5.

• This allows us to bring more programs into memory – increase degree of multiprogramming

or over-allocating memory.

• If have 40 frames, run 8 processes using 5 pages each instead of 4 processes using 10

pages.

• What happens when a process suddenly needs all 10 pages? All memory is in use.

• Terminate process – why, it is running normally

• Swap out a process – will be considered later

• Page replacement
27

Need For Page Replacement


30

Page Replacement Algorithms


• Want lowest page-fault rate.
• Evaluate algorithm by running it on a
particular string of memory references
(reference string) and computing the
number of page faults on that string.
• In all our examples, the reference string is
31

What happens when we have 4 frames?


32

 Replace page that will not be used for longest period of


time.

 Each page will be labeled the number of instruction that


will be executed before that page is needed/ first
referenced.

 The only problem with this algorithm is unrealizable…


THE ONLY PROBLEM?

 How do you know this? Requires future knowledge!

 Used as yardstick to evaluate how well other algorithms


perform.
33
34

 The optimal algorithm uses the time when a page is to be used


next
 FIFO uses the time when a page was brought into memory
 LRU – use the recent past as an approximation of the near
future.
 Replace the page that has not been used for the longest period of
time.
 Counter implementation
◦ Every page entry has a counter; every time page is referenced through
this entry, copy the clock into the counter.
◦ When a page needs to be changed, look at the counters to determine
which one is the smallest value and use that frame.
35
36

 Global replacement – process selects a


replacement frame from the set of all frames; one
process can take a frame from another.
◦ A process is no longer in control of its page-fault rate,
usually results in greater system throughput and its
therefore more commonly used
 Local replacement – each process selects from
only its own set of allocated frames.
◦ Does not take advantage of less used pages in other
processes which could improve overall system
throughput

You might also like