Os Unit Iii-1
Os Unit Iii-1
Main Memory refers to a physical memory that is the internal memory to the
computer. The word main is used to distinguish it from external mass storage
devices such as disk drives. Main memory is also known as RAM.
MEMORY ALLOCATION
If the data sizes are not known before the execution of the process, then they
have to be guessed. If the data size guessed is larger than the required, then it
leads to wastage of memory. If the guessed size is smaller, then it leads to
inappropriate execution of the process.
Static memory allocation method does not need any memory allocation
operation during the execution of the process. As all the memory allocation
operation required for the process is done before the execution of the process
has started. So, it leads to faster execution of a process.
The actual size, of the data required, is known at the run time so, it allocates
the exact memory space to the program thereby, reducing the memory wastage.
Dynamic memory allocation does not require special support from the operating
system. It is the responsibility of the programmer to design the program in a
way to take advantage of dynamic memory allocation method.
Thus the dynamic memory allocation is flexible but slower than static memory
allocation.
Advantages of Dynamic Memory Allocation
A process is divided into Segments. The chunks that a program is divided into
which are not necessarily all are of the same sizes are called segments.
Segmentation gives user’s view of the process which paging does not give. Here
the user’s view is mapped to physical memory.
There are types of segmentation:
As processes are loaded and removed from the memory, the free memory
space is broken into little pieces, causing External fragmentation.
COMPACTION
Before Compaction
Before compaction, the main memory has some free space between
occupied spaces. This condition is known as external fragmentation.
Due to less free space between occupied spaces, large processes cannot
be loaded into them.
Main Memory
Occupied space
Free space
Occupied space
Occupied space
Free space
After Compaction
After compaction, all the occupied space has been moved up and the
free space at the bottom. This makes the space contiguous and removes
external fragmentation. Processes with large memory requirements can
be now loaded into the main memory.
Main Memory
Occupied space
Occupied space
Occupied space
Free space
Free space
DEMAND PAGING:
Demand paging follows that pages should only be brought into memory if the
executing process demands them. This is often referred to as lazy evaluation as
only those pages demanded by the process are swapped from secondary
storage to main memory.
Page Fault – A page fault happens when a running program accesses a memory
page that is mapped into the virtual address space, but not loaded in physical
memory.
Different page replacement algorithms suggest different ways to decide which
page to replace. The target for all algorithms is to reduce the number of page
faults.
TYPES:
1. First in first out (FIFO) page replacement algorithm
2. Optimal page replacement algorithm
3. Least Recently Used (LRU) page replacement algorithm
4. Not Recently Used (NRU) page replacement algorithm
5. Second Chance page replacement algorithm
1.First in first out (FIFO) page replacement algorithm
This is the simplest page replacement algorithm. In this algorithm, the operating
system keeps a track of all the pages present in the memory in a queue fashion.
When a page needs to be replaced, the oldest page in the queue is selected and
replaced with the new page.
Optimal Page Replacement algorithm says that the newly arrived page will
replace a page in memory which wouldn’t be used for the longest period of time
in the future.
Initially all the memory slots will be empty so (7, 0, 1, 2) will be allocated to the
memory with 4 page-faults. As (0) is already there, there’s no page fault. Next
in the string is (3), it’ll replace (7) as it’s not used for the longest period of time
in the future, with one page fault. Again, 0 is already there, so no page fault. 4
will replace 1 with one page fault. And for the rest of the string, there will be no
page fault as all the arriving pages are already there in the memory.
In this algorithm, the new page is replaced with the existing page that has been
used the least recently. In other words, the page which has not been referred for
a long time will be replaced with the newly arrived page.
In most computers with virtual memory, each page is associated with two status
bits.
In this algorithm, the operating system uses (R) and (M) bits to distinguish
between pages. When a process starts, both page bits (R) and (M) are set to 0 by
the operating system. If a page has been referenced, a page fault will occur and
the (R) bit is set by the OS.
If a page is modified, then another page fault will occur and the OS will set the
(M) bit.
Periodically the R bit is cleared by a clock interrupt. When a page fault occurs
and there are no empty frames, the operating system inspects all the pages and
divides them into 4 categories:
Based on the R and M bits, all the pages are categorized into the above 4
categories. The algorithm removes a random page with the lowest numbered
non-empty class. If class 0 is empty, then a random page from class 1 is
replaced and so on.
As the name suggests, the pages are given a second chance. The pages that
arrived are stored in a linked list. If the oldest page in the linked list gets
referenced, a page fault occurs and the R bit is cleared. The oldest page is now
cleared from memory and is pushed to the latest clock interval.
When the time arrives to replace a page, the operating system replaces the
oldest page that has also not been referenced.