CSC 310
CSC 310
The main memory is central to the operations of a modern computer. Main Memory is a large
array of words or bytes ranging in size from hundreds of thousands to billions. Main memory
is a repository of rapidly available information shared by the CPU and I/O devices. Main
memory is the place where programs and information are kept when the processor is
effectively utilizing them. Main memory is associated with the processor, so moving
instructions and information into and out of the processor is extremely fast. Main memory is
also known as RAM (Random Access Memory). This memory is a volatile memory. RAM
loses its data when a power interruption occurs.
Memory Management
In a multiprogramming computer, the operating system resides in a part of memory and the rest
is used by multiple processes. The task of subdividing the memory among different processes
is called memory management. Memory management is a method in the operating system to
manage operations between main memory and disk during process execution. The main aim of
memory management is to achieve efficient utilization of memory.
It can also be defined as the process of controlling and coordinating computer memory, assigning
portions known as blocks to various running programs to optimize the overall performance of the
system.
It is the most important function of an operating system that manages primary memory. It helps
processes to move back and forward between the main memory and execution disk. It helps
operating system to keep track of every memory location, irrespective of whether it is allocated
to some process or it remains free.
Why use Memory Management?
It allows you to check how much memory needs to be allocated to processes that decide
which processor should get memory at what time.
Tracks whenever inventory gets freed or unallocated. According to it will update the
status.
It allocates the space to application routines.
It also makes sure that applications do not interfere with each other.
Helps protect different processes from each other
It places the programs in memory so that memory is utilized to its full extent.
Allocates and de-allocate memory before and after process execution.
To keep track of used memory space by processes.
To minimize fragmentation issues.
To proper utilization of main memory.
To maintain data integrity while executing of process.
Memory Allocation
To gain proper memory utilization, memory must be allocated in an efficient manner. One of
the simplest methods for allocating memory is to divide memory into several fixed-sized
partitions and each partition contains exactly one process. Thus, the degree of
multiprogramming is obtained by the number of partitions.
Multiple Partition Allocation: In this method, a process is selected from the input queue and
loaded into the free partition. When the process terminates, the partition becomes available for
other processes.
Fixed Partition Allocation: In this method, the operating system maintains a table that
indicates which parts of memory are available and which are occupied by processes. Initially,
all memory are available for user processes and are considered one large block of available
memory. This available memory is known as “Hole”. When the process arrives and needs
memory, we search for a hole that is large enough to store this process. If the requirement is
fulfilled then we allocate memory to process. Otherwise, we keep the rest available to satisfy
future requests. While allocating a memory sometimes dynamic storage allocation problems
occur which concerns how to satisfy a request of size n from a list of free holes. There are
some solutions to this problem:
First Fit
In the first fit, the first available free hole that fulfills the requirement of the process allocated.
First Fit Algorithm
1- Input memory blocks with size and processes with size.
2- Initialize all memory blocks as free.
3- Start by picking each process and check if it can
be assigned to current block.
4- If size-of-process <= size-of-block if yes then
assign and check for next process.
5- If not then keep checking the other blocks.
Best Fit
In the best fit, it allocates the process to the smallest hole that is big enough to process its
requirements. For this, we search the entire list, unless the list is ordered by size. In this
method memory utilization is maximum as compared to other memory allocation techniques.
Best Fit Algorithm
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
minimum block size that can be assigned to
current process i.e., find min(bockSize[1],
blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign
it to the current process.
5- If not then leave that process and keep checking
the other processes.
Worst Fit
It allocates the process to the partition, which is the largest sufficient freely available partition in
the main memory. Inefficient memory utilization is a major issue in the worst fit.
What is Swapping?
Before a process is executed it must have resided in memory. Swapping is a process of swapping
a process temporarily into a secondary memory from the main memory, which is fast as
compared to secondary memory. It will be later brought back into the memory to continue
execution.
A swapping allows more processes to be run and can be fit into memory at one time. Swapping
is also known as roll-out, roll in because 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. After finishing higher priority work, the lower priority process swapped
back in memory and continued to the execution process.
Benefits of Swapping
Here are major benefits/pros of swapping:
What is Fragmentation?
Processes are stored and removed from memory which creates free memory space or hole, and
which are too small to be used by other processes. After sometime, that processes are not able to
get memory blocks allocated to them because of the small size of the memory blocks, and these
memory blocks that always remain unused are called fragments. When free blocks are quite
small, they are not able to fulfill any request. Therefore, Fragmentation is defined as when the
process is loaded and removed after execution from memory, it creates a small free hole. These
holes cannot be assigned to new processes because holes are not combined or do not fulfill the
memory requirement of the process. To achieve a degree of multiprogramming, we must reduce
the waste of memory or fragmentation problem. In operating system two types of fragmentation
1. Internal fragmentation
2. External fragmentation
Internal Fragmentation:
Internal fragmentation occurs when memory blocks are allocated to the process more than their
requested size. Due to this, some unused space is leftover and create an internal fragmentation
problem.
Example: Suppose there is a fixed partitioning that is used for memory allocation and then
different sizes of blocks 3MB, 6MB, and 7MB space in memory. Now a new process p4 of size
2MB comes and demand for the block of memory. It gets a memory block of 3MB but 1MB
block memory is a waste, and it cannot be allocated to other processes too. This is called internal
fragmentation.
External Fragmentation:
In external fragmentation, we have a free memory block, but we cannot assign it to process
because the blocks are not contiguous.
Example: Suppose (consider above example) three processes p1, p2, p3 come with size 2MB,
4MB, and 7MB respectively. Now they get memory blocks of size 3MB, 6MB, and 7MB
allocated respectively. After allocation, process p1 and process p2 left 1MB and 2MB
respectively. Suppose a new process p4 comes and demands a 3MB block of memory, which is
available, but we cannot assign it because the free memory space is not contiguous. This is
called external fragmentation.
Both the first fit and best-fit systems for memory allocation are affected by external
fragmentation. To overcome the external fragmentation problem, compaction is used. In the
compaction technique, all free memory spaces combine, and makes one large block. So, this
space can be used by other processes effectively.
Internal fragmentation can be reduced by assigning the smallest partition, which is still
good enough to carry the entire process.
External fragmentation can be reduced by rearranging memory contents to place all free
memory together in a single block (compaction).
Assignment
Write a short note on segmentation