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

Lecture_11 - Introduction to memory management

The document provides an overview of memory management in operating systems, focusing on process memory allocation techniques such as contiguous and non-contiguous memory allocation. It details various allocation strategies including first fit, best fit, worst fit, and next fit, along with the concepts of memory fragmentation and solutions like compaction and paging. Additionally, it discusses segmentation and paged segmentation as methods for managing memory effectively.

Uploaded by

rajithsjc
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)
3 views

Lecture_11 - Introduction to memory management

The document provides an overview of memory management in operating systems, focusing on process memory allocation techniques such as contiguous and non-contiguous memory allocation. It details various allocation strategies including first fit, best fit, worst fit, and next fit, along with the concepts of memory fragmentation and solutions like compaction and paging. Additionally, it discusses segmentation and paged segmentation as methods for managing memory effectively.

Uploaded by

rajithsjc
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/ 52

Computer Architecture and Operating

Systems
COSC 22073/BECS 22233
Lesson 11: Introduction Memory Management
A.M.K.S Aththanayake
Faculty of Science
University of Kelaniya

1
Kenel Data
Process in an OS (PCB)

Stack

• Process is an execution version of a computer program.

• Process Memory map must present in the main memory.


Heap

Data
(Global &
Static)

Text
(Instructions)
2
How to Allocate Main Memory to a Process?

• There are two types of memory allocations techniques:


1. Contiguous Memory Allocation

2. Non-Contiguous Memory Allocation

3
Contiguous Memory Allocation

• When the entire program is stored at a single continuous memory


location.

• It is known as centralized approach.

4
Contiguous Memory Allocation

Kenel Data
• Initial Idea: (PCB)

• Allocate one process at a time to main memory. Stack

• Not effiecient.

• Not supporting for multitasking. Heap

Data
(Global &
Static)
Text
(Instructions)

5
Types of Contiguous Memory Allocation

Contiguous
Memory
Allocation

Variable or
Fixed or Static
Dynamic
Allocation
Allocation
6
Fixed or Static Partition Contiguous 0
Memory Allocation
1

2
• All sectors of memory are of same size (For eg: 1kb)
3

• The degree of multi-programming depends on the


4
number of partitions.
• When a partition is free, a process is brought from the 5
ready queue and loaded in the memory.
6
• When a process terminates the partition becomes
available for another process. Main Memory

7
Variable (Dynamic) Partition Contiguous 0

Memory Allocation 1

2
• All sectors of memory are of different sizes.
• Say 1KB, 2 KB, some of 10KB and called as Hole. 3

4
• OS keeps a table which keeps track of which sector of
memory are free. 5

• Process comes and search the appropriate hole and


6
get settled in it.
• There are different dynamic storage allocation Main Memory

techniques. 8
Dynamic Storage Allocation Techniques

Dynamic
Allocation

First Fit Best Fit Worst Fit Next Fit

9
First Fit

• Allocate the first hole that is big enough.

• Searching starts at the begining of the memory.

• Stop the search as soon as it finds a free hole that is large enough.

10
Example

• On a system using first fit allocation, assume memory is allocated as


specified in the figure before the additional requests for 20 KB, 10
KB, and 5KB (in that order) are received. Specify which location will
be allocated by the OS for each of the new requests.

Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB

A B C D E F G H I J K L

11
Wrost Fit

• Allocate the largest hole.

• Search the entire list.

• This strategy produces the largest leftover hole which may be more
useful than the smaller leftover hole from a best-fit approach.

12
Example

• On a system using wrost fit allocation, assume memory is allocated


as specified in the figure before the additional request for 20 KB, 10
KB, and 5KB (in that order) are received. Specify the which location
will be allocated by the OS for each of the new requests.

Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB

A B C D E F G H I J K L

13
Best Fit

• Allocate the smallest hole that is big enough.

• We must search the entire list, unless the list is ordered by size.

• This strategy produces the smallest leftover hole.

14
Example

• On a system using best fit allocation, assume memory is allocated as


specified in the figure before the additional request for 20 KB, 10 KB,
and 5KB (in that order) are received. Specify the which location will
be allocated by the OS for each of the new requests.

Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB

A B C D E F G H I J K L

15
Next Fit

• In the first fit, since all the search starts from the beginning of
memory, holes at the beginning of memory are considered for
allocation more frequently.
• In the next fit, it keeps track of which hole was allocated.
• The next search starts at the last hole allocated, not at the beginning
of the memory.
• The next fit is a modified version of the ‘first fit’. It begins as
the first fit to find a free partition but when called next time it
starts searching from where it left off, not from the beginning

16
Example

• On a system using next fit allocation, assume memory is allocated as


specified in the figure before the additional request for 20 KB, 10 KB,
and 5KB (in that order) are received. Specify the which location will
be allocated by the OS for each of the new requests.

Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB

A B C D E F G H I J K L

17
Memory Fragmentation

• Memory Fragmentation is when most of your memory is allocated in


a large number of non-contiguous blocks: leaving a good percentage
of your total memory unallocated, but unusable.

• That is, fragmentation is the inability to use memory even if it is free.

18
Main Types of Fragmentation

Memory
Fragmentation

Internal External
Fragmentation Fragmentation
19
Internal Fragmentation

• Due to the rules governing memory allocation, more computer


memory is sometimes allocated than needed.

• Therefore, some portions of memory is left unused as it cannot be


used by another process.

20
External Fragmentation

• This arises when free memory is separated into small blocks and is
inter separated by allocated memory.

• That is, total memory is enough to satisfy a request or to reside a


process in it, but it is not contiguous.

21
Solutions for Fragmentation

Solutions

Non-
Compaction Contiguous
Allocation
22
Compaction

• Compaction is a process in which free spaces are collected to a


large memory chunks. That is, combine all the empty spaces
together.

U F U U U F U F U U U U U U U F F F
s r s s s r s r s s s s s s s r r r
e e e e e e e e e e e e e e e e e e
d e d d d e d e d d d d d d d e e e

23
Compaction

• Processes are on one side and free space is on the other side in the
memory.
• During memory compaction:
• Block processes for compaction
• Move them towards lower address space
• Allocate memory for pending processes
• Resumed blocked processes

• It comsumes CPU time.

24
Non-Contiguous Allocation

Non-
Contiguous
Allocation

Paging Segmentation

25
Paging

• Memory Management Technique that permits the physical address


space of a process to be non-contiguous.

• Physical memory is divided into fixed size-block called FRAMES.

• Logical memory is divided into blocks of the same size called


PAGES.

26
Paging Model
Process Memory Main Memory

27
Paging: Address Translation
Process Memory Main Memory

30
Paging: Address Translation
Process Memory Main Memory

31
Paging: Hardware

Logical Address =Page Number +Offset

Physical Address =Frame Number +Offset

30
Page Table

• Each process has its own page table stored in memory.

• Page table contains frame numbers

• To make usage more efficient the page table is kept in the main
memory & Page Table Base Register (PBTR)

31
Page Table

• Page table implementation maintains a four-bit per page table entry


• Use bit: It is a flag associated with a page in memory that indicates whether
the page has been accessed (read from or written to) during a certain period.

• Modified bit: is a flag associated with a memory page that indicates whether
the contents of the page have been modified (written to) since it was last
loaded into memory or since the bit was last cleared.

• Valid bit:is a flag associated with a page table entry that indicates whether the
corresponding page in memory is currently valid or not.

• Read-only bit:is a flag associated with a page table entry or a page in memory 32
that indicates whether the contents of the page are read-only or writable.
Example:

• Consider a system in which logical address equal to 27 bits and the


physical address requires 21 bits. The page size is 4K words.
Calculate the number of pages and number of frames.

33
Answer
Paging: Advantages

• No external fragmentation.

• Simple memory management scheme.

• Swapping easy

35
Paging: Disadvantages

• Internal fragmentation

• Page table may consume more memory

36
Solution to Access Time

• Translation Look-aside Buffer (TLB)


• It is a small fast cache memory.
• Reduces effective access time.
• It consists of two columns:
• Page Number
• Frame Number

37
Paging with TLB

38
More about TLB

• Only one TLB in the system.

• So, whenever context switch occurs, the entire content of the TLB is
flushed and deleted.

• TLB again updated with the currently running process.

• A TLB contains only few page table entries.

39
More about TLB

• If the page number is found, then it is TLB hit. Otherwise TLB miss.

• The percentage of times that a particular page number is found in


the table is called HIT RATIO.

40
More about TLB

• Effective Memory Access Time =


{Hit Ratio ∗ (access time for the TLB
+access time)
+Miss ration *Total Access time}

41
Example:

• If it takes 20 nanoseconds to search the TLB and 100 nanoseconds


to access the memory. If we fail to find the page in TLB then the
access time is 220 nanoseconds. What is the effective memory
access time with a hit ration of 80%?

42
Answer

EMAT=144 nanoseconds
Therefore, the effective memory access time is 144 nanoseconds with a TLB hit ratio of 80%.
Kenel Data
Beyond Paging (PCB)

Stack

• Paging does not preserve users view of the memory.

Heap

Data
(Global &
Static)
Text
(Instructions)
44
What happened in Paging?

45
What happened in Paging?

46
User’s View

47
Segmentation

• Segmentation is a memory management technique in which memory is


divided into variable sized chunks which can be allocated to processes.

• Each chunk is called a segment.

• A table store the information about all such segments called the segment
table.
• Segmentation supports user view of the memory.

48
Segmentation

49
Segmentation

50
Segmentation

51
Paged Segmentation

• It utilize both paging and segmentation

• User space is broken into a number of segments

• Each segment is broken into fixed size pages.

52

You might also like