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

Operating Systems: Memory Management Assoc. Prof. Dr. Latifah Munirah Kamarudin SEM 2 2017/2018

How many bits can represent 8 frame? 0x09 8:I F2 0x18 à Need 3 bits (23 = 8) 0x0a 9:j 0x19 0x0b 10 : k 0x1a 0x0c 11 : l 0x1b Total bits needed for physical address = 5 bits 0x0d Represent the frame number
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)
41 views

Operating Systems: Memory Management Assoc. Prof. Dr. Latifah Munirah Kamarudin SEM 2 2017/2018

How many bits can represent 8 frame? 0x09 8:I F2 0x18 à Need 3 bits (23 = 8) 0x0a 9:j 0x19 0x0b 10 : k 0x1a 0x0c 11 : l 0x1b Total bits needed for physical address = 5 bits 0x0d Represent the frame number
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/ 40

OPERATING SYSTEMS

MEMORY MANAGEMENT
ASSOC. PROF. DR. LATIFAH MUNIRAH
KAMARUDIN
SEM 2 2017/2018
MEMORY MANAGEMENT

Is the task carried out by the OS and hardware to


accommodate multiple processes in main memory

How do multiple programs share memory?


Firefox Word javac
MEMORY MANAGEMENT

• Memory management is the functionality of an operating system which


handles or manages primary memory and moves processes back and
forth between main memory and disk during execution.
MEMORY MANAGEMENT: TERMINOLOGY

• Segment: A chunk of memory assigned to a process.


• Physical Address: a real address in memory
• Virtual Address: an address relative to the start of a process's address space.
MEMORY MANAGEMENT
Transparency:
• We want multiple processes to coexist in memory.
• No process should be aware that memory is shared.
• Processes should not care what physical portion of memory they are assigned to.
Safety:
• Processes must not be able to corrupt each other.
• Processes must not be able to corrupt the OS.
Efficiency:
• Performance of CPU and memory should not be degraded badly due to sharing
MEMORY MANAGEMENT TECHNIQUE

Fixed Dynamic
Partitioning Partitioning

Simple Simple
Paging Segmentation
FIXED PARTITIONING

• The main memory is partitioned


into a set of non-overlapping
blocks.
• Partition can be of equal or
unequal size
• A process can be loaded into a
partition with higher or equal
size
FIXED PARTITIONING PROBLEMS
• A program may not fit in a
partition.
• Main memory use is inefficient.
• Any program, no matter how
small, occupies an entire
partition.
• Results in internal fragmentation.
DYNAMIC PARTITIONING
• Partitions are of variable length and number
• Process is allocated exactly the memory as required – no internal fragmentation
• Hole – block of available memory; holes of various size are scattered
throughout memory.
• Results in external fragmentation.
• Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
DYNAMIC PARTITIONING
• Termination of process releases its memory blocks
DYNAMIC PARTITIONING

Allocation algorithm

Best-fit
algorithm: Allocate the smallest hole that fits
Worst-fit
algorithm: Allocate the largest hole
First-fit Allocate the first hole that fits from the
algorithm: beginning

Next-fit: Allocate the first hole that fits from the last
placement
DYNAMIC PARTITIONING
DYNAMIC PARTITIONING

• Eventually main memory holes too small to hold any process - external
fragmentation.
• Total memory space exist but not continuous
• Compaction reduce external fragmentation by shuffling memory contents to place
all free memory together in one large block
COMPACTION
EXERCISES

The shaded areas are allocated blocks; the white areas are free blocks. Using the
following placement algorithms, show the partition allocated for the processes of size
110K, 150K, 300K and 200K (loaded in that order):
• First-fit
• Best-fit
• Next-fit
EXERCISES
EXERCISES
A dynamic partitioning scheme is being used, and the following is the memory
configuration at a given point in time:

The shaded areas are allocated blocks. The next three memory request are for:
40M, 20M and 10M. Indicate the starting address of each of the three blocks using
the indicated place algorithm
1. First Fit
2. Best Fit
3. Next Fit
4. Worst Fit
EXERCISES
A dynamic partitioning scheme is being used, and the following is the memory
configuration at a given point in time:
BUDDY SYSTEM
• A reasonable compromise between fixed and dynamic partitioning
schemes.
• Entire space available is treated as a single block of 2U
• If a request of size s where 2U-1 < s <= 2U
• entire block is allocated
• Otherwise block is split into two equal buddies
• Process continues until smallest block greater than or equal to s is generated
EXAMPLE OF BUDDY SYSTEM
TREE REPRESENTATION OF BUDDY SYSTEM
BUDDY SYSTEM

• No External Fragmentation
• No need for compaction
• On average, internal fragmentation is 25%
• Each block is at least 50% occupied
SWAPPING

C C C C C
B B B B
A
A A A
D D D
OS OS OS OS OS OS OS

• Memory allocation changes as


• Processes come into memory
• Processes leave memory
• Swapped to disk
• Complete execution
• Gray regions are unused memory 23
RELOCATION
Because of swapping and compaction, a process may be (often)
relocated in main memory.

24
MEMORY MANAGEMENT
PAGING
PAGING
Paging is a memory management technique that:
• does not let the amount of physical memory limit the size of process.
• allows memory allocation to be non-contiguous and has no external
fragmentation problems, simplifying memory management and obviating
any need for compaction.
• lets us keep more processes in memory than the sum of their memory needs
so that we can keep the CPU utilization as high as possible.
• lets us us keep just the parts of a process that we're using in memory and
the rest on the disk. 26
MEMORY
PAGING 0x00 0x10
PROGRAM 0x01 0x11
PAGE
0:a 0x02 0x12
1:b 0x03 0x13
2:c 0x04 0x14
3:d 0x15
0x05
4:e FRAME 0x06 0x16
5:f 0x07 0x17
6:g 0x08 0x18
7:h 0x19
0x09
8:I 0x0a 0x1a
9:j 0x0b 0x1b
10 : k 0x0c 0x1c
11 : l 0x1d
0x0d
12 : m 0x0e 0x1e
13 : n 0x0f 0x1f
14 : o
15 : p Memory is divided into small equal
Program/Process is divided into small fixed-size chunks called frames
equal fixed-size chunks called pages * Blocks in physical address spaces
* Blocks in logical address spaces
MEMORY
PAGING PAGE TABLE 0x00 F0 0x10 F4
0x01 0x11
PAGE FRAME
0x02 0x12
PROGRAM 0 1 0x03 0x13
0:a P0 1 4 0x04 F1 0x14 F5
1:b 0x05 0x15
2:c 2 3 0x16
0x06
3:d 3 5 0x07 0x17
4:e P1 0x08 F2 0x18 F6
5:f 0x09 0x19
6:g 0x0a 0x1a
7:h 0x0b 0x1b
8:I P2 0x0c F3 0x1c F7
9:j 0x0d 0x1d
10 : k 0x0e 0x1e
11 : l 0x0f 0x1f
12 : m P3
13 : n
14 : o
15 : p
PAGING
PROGRAM
00 00 0:a P0
00 01 1:b How to calculate Logical address?
00 10 2:c
00 11 3:d Size of Page = 4 bytes
01 00 4:e P1 How many bits can represent 4 bytes? à
01 01 5:f
0110 6:g
Need 2 bits (22 = 4)
01 11 7:h
10 00 8:I P2 Number of Page = 4
10 01 9:j
10 10 10 : k How many bits can represent 4 page?
10 11 11 : l à Need 2 bits (22 = 4)
11 00 12 : m P3
11 01 13 : n
11 10 14 : o Total bits needed for logical address = 4 bits
11 11 15 : p

Represent the page number


PAGING
MEMORY

0x00 F0 0x10 4:e F4


How to calculate Physical address? 0x01 0x11 5:f
0x02 0x12 6:g
Size of Frame= 4 bytes 0x13 7:h
0x03
How many bits can represent 4 bytes? à 0x04 0:a F1 0x14 12 : m F5
Need 2 bits (22 = 4) 0x05 1:b 0x15 13 : n
0x06 2:c 0x16 14 : o
0x07 3:d 0x17 15 : p
Number of Frame= 8 0x08 F2 0x18 F6
0x09 0x19
How many bits can represent 8 Frame? 0x1a
0x0a
à Need 3 bits (23 = 8) 0x0b 0x1b
0x0c 8:I F3 0x1c F7
0x0d 9:j 0x1d
Total bits needed for Physical address = 5 bits 0x0e 10 : k 0x1e
11 : l 0x1f
0x0f
MEMORY
PAGING PAGE TABLE 0x00 F0 0x10 4:e F4
0x01 0x11 5:f
PAGE FRAME
0x02 0x12 6:g
PROGRAM 0 1 0x03 0x13 7:h
0000 0:a P0 1 4 0x04 0:a F1 0x14 12 : m F5
0001 1:b 0x05 1:b 0x15 13 : n
0010 2:c 2 3 2:c 0x16 14 : o
0x06
0011 3:d 3 5 0x07 3:d 0x17 15 : p
0100 4:e P1 0x08 F2 0x18 F6
Frame number of 2 is 3 (011)
0101 5:f 0x09 0x19
0110 6:g 0x0a 0x1a
0111 7:h 0x0b 0x1b
1000 8:I P2 10 00 0x0c 8:I F3 0x1c F7
1001 9:j 0x0d 9:j 0x1d
1010 10 : k 0x0e 10 : k 0x1e
011 + 00 = 011 00
1011 11 : l 11 : l 0x1f
0x0f
1100 12 : m P3 Take offset as it is
1101 13 : n How to calculate Physical address?
1110 14 : o
1111 15 : p
EXERCISES
In a system that supports paging, let the logical address consist of 11 bits and the physical address
consists of 14 bits. Let the page size = frame size = 256 bytes.

a) How many pages can the logical address space contain?

Page size = 256 bytes = 28 . Hence, 8 bits are required for the page offset. So 11-­‐8 = 3 bits are
for the page number in the logical address =>number of pages=23 = 8

b) How many frames does the physical memory contain?


14-­‐8=6 bits are for the frame number in the physical address =>number of frames=26 = 64

c) How many bits are required for the page offset?


8
MEMORY MANAGEMENT
SEGMENTATION
SEGMENTATION

• Like Paging, Segmentation is also a memory management


scheme.
• The process is divided into the variable size segments and
loaded to the logical memory address space.
• The logical address space is the collection of variable size
segments. Each segment has its name and length.
• For the execution, the segments from logical memory space
are loaded to the physical memory space.
MEMORY
SEGMENTATION 0x00 0x10
PROGRAM 0x01 0x11
0x00 : a SEGMENT TABLE 0x02 0x12
0x01 : b 0x03 0x13
0x02 : c Segment Base Length
Number 0x04 0x14
0x05 0x15
1 0x06 3
0x00 : d 0x06 0x16
0x01 : e 2 0x10 5 0x07 0x17
0x02 : f 0x08 0x18
0x03 : g 3 0x01 1
0x09 0x19
0x04 : h
0x0a 0x1a
0x0b 0x1b
0x00 : i 0x0c 0x1c
0x0d 0x1d
0x0e 0x1e
0x0f 0x1f
SEGMENTATION MEMORY

0x00 0x10 0x00 : d


PROGRAM 0x01 0x00 : i 0x11 0x01 : e
If we want to access this data
0x00 : a <1,2> 0x02 0x12 0x02 : f
0x01 : b 0x03 0x13 0x03 : g
0x02 : c SEGMENT TABLE 0x14 0x04 : h
0x04
Segment Base Length 0x05 0x15
0x00 : d Number 0x06 0x00 : a 0x16
0x01 : e 1 0x06 3 0x07 0x01 : b 0x17
0x02 : f 0x08 0x02 : c 0x18
0x03 : g 2 0x10 5
0x09 0x19
0x04 : h 3 0x01 1 0x0a 0x1a
0x0b 0x1b
0x00 : i 0x0c 0x1c
1. Base address fetched 0x0d 0x1d
2. Off set must be between 0 0x0e 0x1e
and the limit of that segment 0x0f 0x1f
3. If second step is completed
without any error then add
base with offset 0x06 + 2 = 0x08
EXERCISES
Segment Base Length
0 660 248
1 1752 422
2 222 198
3 996 604

What are the physical addresses for the following logical addresses or indicate if a
segment fault occurs

1. 0, 198 à in Segment 0 => 660 + 198 = 858


2. 1, 530
3. 2, 156
4. 3, 444
5. 0, 222
EXERCISES
Segment Base Length
0 660 248
1 1752 422
2 222 198
3 996 604

What are the physical addresses for the following logical addresses or indicate if a
segment fault occurs

1. 0, 198 à in Segment 0 => 660 + 198 = 858


2. 1, 530 à in Segment 1 => Segment fault
3. 2, 156 à in Segment 2 => 222 + 156 = 378
4. 3, 444 à in Segment 3 => 996 + 444 = 1440
5. 0, 222 à in Segment 0 => 660 + 222 = 882
SUMMARY
Memory Management
1. One of the most important and complex tasks of an operating
system
2. Needs to be treated as a resource to be allocated to and shared
among a number of active processes
3. Desirable to maintain as many processes in main memory as
possible
4. Desirable to free programmers from size restriction in program
development
5. If the blocks that we divide memory into are all of equal size, then
we have a paging system. If the blocks are of different sizes, then
we have a segmentation system.
MEMORY
SEGMENTATION 0x00 0x10
0x01 0x11
PAGING: US 0x12
0x02
0x03 0x13
0x04 0x14
USER’S VIEW 0x05 0x15
0x06 0x16
0x07 0x17
Main Program
0x08 0x18
0x09 0x19
Stack 0x0a 0x1a
Library 0x0b 0x1b
0x0c 0x1c
0x0d 0x1d
0x0e 0x1e
0x0f 0x1f

You might also like