Professional Documents
Culture Documents
OS GTU Study Material Presentations Unit-6 19052021060908PM
OS GTU Study Material Presentations Unit-6 19052021060908PM
GTU # 3140702
Unit-6
Memory
Management
▪ Computer uses special ROM called BIOS (Basic Input Output System)
which permanently stores the software needed to access computer
hardware such as hard disk and then load an operating system into
RAM and start to execute it.
• Faster
• Expensive
• Less Capacity
•Register
•L1 Cache
•L2 Cache
•Main Memory
•Local secondary storage
•Remote secondary storage
• Slower
• Cheaper
• More Capacity
User Program
User Program
User Program
OS in RAM 0 0 OS in RAM 0
Partition 5 Partition 5
Partition 4 Partition 4
Partition 3 Partition 3
Partition 2 Partition 2
Partition 1 Partition 1
OS OS
C C C C C
B B B B
A
A A A
D D D
OS OS OS OS OS OS OS
Process - B
Process - B
Process - A Process - A
OS OS
Program 2
relocation at program address is wrong 16408
load time here 16404
16400
Program 1 Program 2 16392
16388
JMP 28 16384
0 16380 0 16380 0 16380
. . .
. . .
ADD 28 CMP 28 ADD 28
Program 1
MOV 24 24 MOV 24
20 20 20
16 16 16
8 8 8
4 4 4
JMP 24 0 JMP 28 0 JMP 24 0
Program 2
16408
• Required extra information from program
16404
16400
Program 1 Program 2 16392
16388
JMP 16412 16384
0 16380 0 16380 0 16380
. . .
. . .
ADD 28 CMP 28 ADD 28
Program 1
MOV 24 24 MOV 24
20 20 20
16 16 16
8 8 8
4 4 4
JMP 24 0 JMP 28 0 JMP 24 0
Base Limit
Register Register
H 18 2 P 20 6 P 26 3 H 29 3 X
Another way to keep track of memory is to maintain a linked list of allocated and free memory
segments, where segment either contains a process or is an empty hole between two
processes.
Each entry in the list specifies a hole (H) or process (P), the address at which it starts the
length and a pointer to the next entry.
The segment list is kept sorted by address.
Sorting this way has the advantage that when a process terminates or is swapped out,
updating the list is straightforward.
A terminating process normally has two neighbors (except when it is at the very top or bottom
of memory).
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 24
Memory management with Linked List
P 0 5 H 5 3 P 8 6 P 14 4
H 18 2 P 20 6 P 26 3 H 29 3 X
Neighbors
Before P terminate After P terminate
A P B P is replaced by H A B
P is replaced by H
A P A
and two H are merged
P is replaced by H
P B B
and two H are merged
P is replaced by H
P
and three H are merged
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 25
Memory allocation
Section - 4
Memory allocation
Four memory allocation algorithms are as follow
1. First fit
2. Next fit
3. Best fit
4. Worst fit
Here process of size 426k will not get any partition for allocation.
Fastest algorithm because it searches as little as possible.
Memory loss is higher, as very large hole may be selected for small process.
Here process of size 426k will not get any partition for allocation.
Search time is smaller.
Memory manager must have to keep track of last allotted hole to process.
It gives slightly worse performance than first fit.
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 29
Best fit
Entire memory is searched here.
The smallest hole, which is large enough to hold the process, is selected for allocation.
Example: Processes of 212K, 417K, 112K and 426K arrives in order.
100k 500k 200k 300k 600k
Here process of size 426k will not get any partition for allocation.
Search time is high, as it searches entire memory every time.
This algorithm can be used only with dynamic partitioning.
These pages are mapped onto the physical memory but, to run the
Another
program, all pages are not required to be present in the physical HDD
Process’s
memory. Memory
The operating system keeps those parts of the program currently in
RAM
use in main memory, and the rest on the disk.
In a system using virtual memory, the physical memory is divided into
page frames and the virtual address space is divided in into
equally-sized partitions called pages. HDD RAM
Virtual memory works fine in a multiprogramming system, with bits
and pieces of many programs in memory at once. Virtual
Address space HDD
40K – 44K
• Only required pages are loaded in the physical
36K – 40K Virtual page
memory.
Virtual 32K – 36K 5
• Transfers between RAM and disk are always in
Address
28K – 32K
units of a page.
Space
24K – 28K
Physical
20K – 24K 3 Page frame 5 20K – 24K Memory
16K – 20K 4 4 16K – 20K Address
12K – 16K 0 3 12K – 16K
8K – 12K 2 8K – 12K
4K – 8K 1 1 4K – 8K
0K – 4K 2 0 0K – 4K
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 43
Internal operation of the MMU
Outgoing physical address (24580)
1 1 0 0 0 0 0 0 0 0 0 0 1 0 0
15 000 0
14 000 0
13 000 0
12 000 0
11 111 1
10 000 0
9 101 1
12 bit offset
Page 8 000 0
copied directly from
Table 7 000 0
6 000 0 input to output
Virtual page=2 5 011 1
is used as an 4 100 1
index into the 3 000 1
2 110 1 110
page table
1 001 1 Present/
0 010 1
Absent bit
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0
Incoming logical address (8196)
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 44
Internal operation of the MMU
The virtual address is split into a virtual page number (high order bits) and an offset (low-order
bits).
With a 16-bit address and a 4KB page size, the upper 4 bits could specify one of the 11 virtual
pages and the lower 12 bits would then specify the byte offset (0 to 4095) within the selected
page.
The virtual page number is used as an index into the Page table.
If the present/absent bit is 0, it is page-fault; a trap to the operating system is caused to bring
required page into main memory.
If the present/absent bit is 1, required page is there with main memory and page frame
number found in the page table is copied to the higher order bit of the output register along
with the offset.
Together Page frame number and offset creates physical address.
Physical Address = Page frame Number + offset of virtual address.
Logical Address
Physical Address
P D F3 D
P2
Performance is
reduced by half
CPU
For every instruction
Memory reference Memory
occur two time
Use a hardware
Page Table
TLB (Translation
Lookaside Buffer) Page Frame
P1 F2
P2 F3
P3 F1
Logical Address
Physical Address
P D F3
F1 D
P3 P2
TLB
Page Frame
CPU
P1 F2 Hardware
P2 F3 Memory
Page Table
Page Frame
Data Structure
P1 F2
Inside kernel
P2 F3
P3 F1
Logical Address
PT1 PT2 Offset Physical Address
F202 D
10 bit 10 bit 12 bits
2 P2
Page Frame
P1 F101
CPU Page Pointer P2 F102
1
Memory
2 Page Frame
3 P1 F201
P2 F202
Page Frame
P1 F301
P2 F302
Page Request 7 0 1 2 0 3 0 4 2 3 0 3 2 0 2 0 1 7 0 1
Frame – 1 7 7 7 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1
Frame – 2 0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
Frame – 3 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7
Page Faults (9) F F F F F F F F F
Page Request 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Frame – 1 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
Frame – 2 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
Frame – 3 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
Page Faults (15) F F F F F F F F F F F F F F F
0 1 0 1 0
Remove B C D E A Add at end
1 0 1 0 1
C D E A F
1 1 1 1 1
A B C D E
0 0 0 0 0
A B C D E
0 0 0 0 1
B C D E F
Page Request 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Frame – 1 7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
Frame – 2 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
Frame – 3 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7
Page Faults (12) F F F F F F F F F F F F
Page Request 1 2 3 4 1 2 5 1 2 3 4 5
Frame – 1 1 1 1 4 4 4 5 5 5 5 5 5
Frame – 2 2 2 2 1 1 1 1 1 3 3 3
Frames
Three
Frame – 3 3 3 3 2 2 2 2 2 4 4
Page Faults (9) F F F F F F F F F
Page Request 1 2 3 4 1 2 5 1 2 3 4 5
Frame – 1 1 1 1 1 1 1 5 5 5 5 4 4
Four Frames
Frame – 2 2 2 2 2 2 2 1 1 1 1 5
Frame – 3 3 3 3 3 3 3 2 2 2 2
Frame – 4 4 4 4 4 4 4 3 3 3
Page Faults (10) F F F F F F F F F F
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 72
Belady’s Anomaly (in FIFO Page Replacement Algorithm)
Belady’s anomaly is the phenomenon in which increasing the number of page frames results in
an increase in the number of page faults for certain memory access patterns.
This phenomenon is commonly experienced when using the First-In First-Out (FIFO) page
replacement algorithm.
In FIFO, the page fault may or may not increase as the page frames increase, but in Optimal and
stack-based algorithms like LRU, as the page frames increase the page fault decreases.
NRU:-
Page-0 is of class-2 (referenced, not modified)
Page-1 is of class-1 (not referenced, modified)
Page-2 is of class-0 ( not referenced, not modified)
Page-3 is of class-3 (referenced, modified)
So lowest class page-2 needs to be replaced by NRU
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 74
Segmentation
Section - 8
Segmentation
Process P Segment Map Table
Segmentation is a memory management Physical Memory
Sr Size Memory
technique in which each job is divided Segment 1 Address
into several segments of different sizes,
1 300 100 100
one for each module that contains
2 500 400 200
pieces that perform related functions.
3 100 900 300
Each segment is actually a different
Segment 2 N X NM 400
logical address space of the program.
500
When a process is to be executed, its 600
corresponding segmentation are loaded
700
into non-contiguous memory though
every segment is loaded into a Segment 3 800
contiguous block of available memory. 900
1000
Segmentation memory management Segment N
works very similar to paging but here 1100
segments are of variable-length where
as in paging pages are of fixed size.
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 76
Segmentation
Process P Segment Map Table
A program segment contains the Physical Memory
Sr Size Memory
program's main function, utility functions, Segment 1 Address
data structures, and so on.
1 300 100 100
The operating system maintains a 2 500 400 200
segment map table for every process. 3 100 900 300
Segment map table contains list of free Segment 2 N X NM 400
memory blocks along with segment 500
numbers, their size and corresponding 600
memory locations in main memory.
700
For each segment, the table stores the 800
Segment 3
starting address of the segment and the 900
length of the segment.
1000
A reference to a memory location Segment N
1100
includes a value that identifies a
segment and an offset.
Prof. Firoz A Sherasiya #3140702 (OS) ⬥ Unit 6 – Memory Management 77
Paging VS Segmentation
Paging Segmentation
Paging was invented to get large address space Segmentation was invented to allow programs
without having to buy more physical memory. and data to be broken up into logically
independent address space and to add sharing
and protection.
The programmer does not aware that paging is The programmer is aware that segmentation is
used. used.
Procedure and data cannot be distinguished Procedure and data can be distinguished and
and protected separately. protected separately.
Change in data or procedure requires compiling Change in data or procedure requires compiling
entire program. only affected segment not entire program.
Sharing of different procedures not available. Sharing of different procedures available.
Thank
You