Module-4
Module-4
1
Memory Hierarchy
Memory management deals with the allocation and de allocation of space in main memory for programs or
processes.
Performance of computer system is high when the CPU is kept busy at all times.
To keep the CPU busy at all times, number of processes or programs is loaded into main memory at a time.
RAM
3
When a process is blocked due to an input/output operation during its execution then the CPU is allocated
to some other process.
Logical address
When the user wants to execute his/her program then the operating system loads the program into RAM.
To execute the program, CPU generates the addresses of statements in the program as 0, 1, 2, and so on.
When the CPU wants to execute 1st statement of the program then it generates the address 0.
Similarly, when the CPU wants to execute 2nd statement of the program then it generates the address 1.
This generation of addresses is continued till the last statement of the program.
The addresses (0, 1, 2, ….n-1) generated by the CPU for executing the statements of program are called
logical addresses.
Physical address
Physical addresses are the addresses of locations inside RAM where the statements of program are loaded.
5
Memory Management Unit (MMU)
6
Before starting the execution of a process, the address of starting location of process in the RAM is loaded
into Relocation or Base Register and the number of statements in the process is loaded into the Limit
Register.
For example, if a process P1 is loaded into RAM at the address 1200 and the number of statements in the
process P1 is 5, when the execution of process P1 is started, the operating system loads the address 1200
into Relocation Register and value 5 into Limit Register.
While executing the process, the logical address generated by the CPU for executing a statement is
compared with the value in Limit Register.
If the logical address value is less than the value in Limit Register then the logical address is added to the
value in Relocation Register to generate the corresponding physical address.
7
Memory Management Techniques
The operating system uses the following techniques to allocate space for programs in the main memory or
RAM.
Operating system loads the entire program as a single unit into the RAM.
Operating system divides the space in RAM into number of equal size parts before loading any program into
RAM.
Operating system can load a program into any free partition of RAM.
9
Advantage:
Operating system can select any free partition of RAM to load a program.
Disadvantages:
1) Some memory is wasted inside the partition when the size of program is less than the size of partition.
For example, when program P1 is loaded into 2nd partition of RAM as shown in the above figure, 212 MB of
memory in that partition is wasted.
2) When the size of program to be loaded is greater than the size of partition then it is not possible to load
the program into RAM.
For example, if the size of program P5 is 600 MB then it is not possible to load P5 into RAM.
10
Un-equal size fixed partition
Operating system divides the space in RAM into number of parts with different sizes before loading any
program into RAM.
Operating system has to select a suitable partition of RAM for loading a program.
11
Advantage
Disadvantages
1) Operating System has to select a suitable partition for loading a program into RAM.
2) Some memory is wasted inside the partition when the size of process is less than the size of partition.
This wastage of memory is called “internal fragmentation”.
3) When the size of process is greater than the size of all partitions then it is not possible to load the process
into RAM.
For example, if the size of process P5 is 600 MB then it is not possible to load P5 into RAM.
12
Dynamic partitions
Operating system divides the space in RAM into parts at the time of loading programs into RAM.
Into one part, Operating System is loaded and the other part is used for loading user programs.
When a program say P1 is loaded into RAM then the RAM is divided into three parts.
When another program say P2 is loaded then the RAM is divided into four parts as shown in below figure.
13
Advantage
Disadvantages:
Consider the following position of RAM which is the result of loading some programs into RAM and
removing completed programs from RAM.
If the operating system wants to load a program say P20 with size 250 KB, then it is not possible to load that
program into RAM as there is no free part with size greater than or equal to 250KB in the RAM.
14
To avoid external fragmentation, compaction technique can be used.
15
Compaction
Compaction technique moves all free spaces in the RAM together. After applying compaction technique to
the above position of RAM, the position of RAM becomes
Now, the operating system can load program P20 into RAM.
16
With unequal size fixed partition and dynamic partition techniques, the operating system has to select
suitable partition in the RAM for loading a program.
Operating System uses any of the following placement algorithms to select the suitable partition.
Placement Algorithms
1) FIRST FIT
2) BEST FIT
3) WORST FIT
First fit algorithm scans the RAM from starting to ending and selects the first free partition whose size is
greater than or equal to the size of program.
Best fit algorithm scans the RAM from starting to ending and selects the free partition whose size is very
close to the size of program.
Worst fit algorithm scans the RAM from starting to ending and selects the largest free partition.
17
Consider the following position of RAM. To load the program P10 with size 150 KB, the partition selected by
operating system with different algorithms is indicated in below diagram.
18
Assume the memory is divided into six partitions with size 200 KB, 400 KB, 100 KB, 600 KB, 300 KB, 500 KB (in
order). Calculate the total internal fragmentation when the processes with size 336 KB, 96 KB, 173 KB, 415 KB,
245 KB (in order) are placed with
i) first-fit algorithm
ii) best-fit algorithm
RAM is divided into a number of equal size frames before loading any program into RAM.
To load a program into RAM, the operating system divides the program into a number of equal size pages.
Then, the operating system loads pages of the program into RAM wherever free frames are available.
Later, the operating system creates a page table for the program.
Number of entries in the page table is equal to the number of pages in the program.
The operating system stores frame numbers of RAM into the entries of the page table.
21
The following diagram shows how programs are loaded into RAM with the paging technique.
22
Mapping Logical Address to Physical Address in Paging (or) Hardware support for Paging
23
To start the execution of any process or program, the operating system divides the program into equal size
pages, loads these pages into free frames of RAM, creates a page table for the program and stores frame
numbers into the entries of the page table.
To execute a statement of the program, the CPU generates the logical address of that statement.
This logical address consists of two parts: page number (p) and offset (d).
The page number is used as an index into the page table of the process and the corresponding frame number is
identified.
The identified frame number is appended with offset of logical address to get the physical address of the
statement.
The statement in the RAM at the generated physical address is transferred to the CPU for execution.
24
25
Consider a logical address space of 256 pages with 64 words per page mapped onto a physical memory of 4096
frames.
i. How many bits are required in the logical address?
ii. How many bits are required in the physical address?
Size of logical address = size of page number field + size of offset field = 8 bits (2 8 =256) + 6 bits (26 =64) = 14 bits
Size of physical address = size of frame number field + size of offset field = 12 bits (2 12 =4096) + 6 bits (26 =64) =
18 bits
Ex: Consider a machine with 128 MB physical memory and a 32 bit virtual address space. If the page size is 8 KB.
i) Find the number of bits in physical address
ii) Find the number of frames in the Main memory
iii) Find the number of bits allocated for offset
iv) Find the number of entries in page table
If the page number is found in the TLB (TLB hit) then the corresponding frame number is appended with the
offset of logical address to generate the corresponding physical address.
Otherwise (TLB miss), the page number of logical address is used as an index into the page table, the
corresponding frame number is identified and then appended with the offset of logical address to generate
the physical address.
Hit ratio is the percentage of times that a particular page number is found in the TLB.
An 80 percent hit ratio means that we find the desired page number in the TLB 80 percent of the time.
Effective access time = probability of finding the page in the TLB x time to access the statement from the
memory + probability of not finding the page in the TLB x time to access the statement from the memory
If the hit ratio is 80 percent and it takes 20 nanoseconds to search the TLB, 100 nanoseconds to access the
memory then the effective memory access time is
Effective access time = 0.80 x (20+100) + 0.20 x (20+100+100) = 0.8 x 120 + 0.2 x 220 = 140 nanoseconds
If the hit ratio is 98 percent and it takes 20 nanoseconds to search the TLB, 100 nanoseconds to access the
memory then the effective memory access time is
To load a program into RAM, the operating system divides the program into a number of segments of either equal
or unequal size.
Divides the RAM into a number of parts at the time of loading the segments of the program.
The operating system loads the segments of program into RAM wherever free space is available.
Then, create a segment table for the program.
The number of entries in the segment table is equal to the number of segments in the program.
Each entry of the segment table contains a base which indicates the starting address of the segment in the RAM,
and a limit which indicates the number of statements in that segment.
35
In the following figure, a program containing 2000 statements is divided into four segments.
The number of statements in segment 0, segment 1, segment 2 and segment 3 are 500, 400, 300 and 800
respectively.
The segments are loaded into RAM and then a segment table is created for the program.
36
Mapping Logical Address to Physical Address
To execute a statement of the process, the CPU generates the logical address of the statement.
The logical address consists of two parts: segment number and offset.
The segment number is used as an index into the segment table to identify an entry of the segment table.
Then, the offset value of logical address is compared with the limit value in the identified entry.
If the offset value is less than the limit value, then the offset value is added to the base value to generate the
physical address.
What are the physical addresses for the following logical addresses?
a. 0, 512
b. 1, 47
c. 2, 13
d. 3, 185
The operating system divides the RAM into equal size frames before loading any program into the RAM.
To load a program into RAM, the operating system divides the program into a number of segments.
Later, divides each segment into a number of pages.
Then, loads the pages of the program into free frames of RAM.
Creates a segment table for the program.
Each entry of the segment table points to the page table of the corresponding segment.
Each entry of the page table contains the corresponding frame number.
The logical address generated by the CPU contains 3 parts: segment number, page number, offset
Address space
If a higher-priority process arrives and wants service, the memory manager can swap out the lower-priority process
and then load and execute the higher-priority process.
When the higher-priority process finishes, the lower-priority process can be swapped back in and continued.
The system maintains a ready queue consisting of all processes that are ready to run.
Whenever the CPU scheduler decides to execute a process, it calls the dispatcher.
The dispatcher checks to see whether the next process in the queue is in RAM.
If it is not, and if there is no free memory region, the dispatcher swaps out a process currently in RAM and swaps
in the desired process.
A process that is swapped out will be swapped back into either the same memory space it occupied previously or
to a different location.
Relocation