Name Advait Parab
UID no. 2023300158
Experiment No. 9
AIM: To implement a program based on Memory Allocation.
THEORY: 1)What is Memory Management?
Memory management mostly involves the management of main memory. In a
multiprogramming computer, the Operating System resides in a part of the main
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.
Memory Management is Required:
● Allocate and deallocate memory before and after process execution.
● To keep track of the used memory space by processes.
● To minimize fragmentation issues.
● For the proper utilization of main memory.
● To maintain data integrity while executing of process.
2)What is Paging?
Paging is a memory management scheme that eliminates the need for a
contiguous allocation of physical memory. The process of retrieving processes
in the form of pages from the secondary storage into the main memory is known
as paging. The basic purpose of paging is to separate each procedure into pages.
The mapping between logical pages and physical page frames is maintained by
the page table, which is used by the memory management unit to translate
logical addresses into physical addresses. The page table maps each logical page
number to a physical page frame number. By using a Page Table, the operating
system keeps track of the mapping between logical addresses (used by
programs) and physical addresses (actual locations in memory).
Why Paging is used for memory Management?
Paging is a memory management technique that addresses common challenges
in allocating and managing memory efficiently. Here we can understand why
paging is needed as a Memory Management technique:
● Memory isn’t always available in a single block: Programs often need
more memory than what is available in a single continuous block. Paging
breaks memory into smaller, fixed-size pieces, making it easier to
allocate scattered free spaces.
● Processes size can increase or decrease: programs don’t need to
occupy continuous memory, so they can grow dynamically without the
need to be moved.
3)What is Segmentation?
A process is divided into Segments. The chunks that a program is divided into
which are not necessarily all of the exact sizes are called segments.
Segmentation gives the user’s view of the process which paging does not
provide.
Types of Segmentation in Operating Systems
● Virtual Memory Segmentation: Each process is divided into a number of
segments, but the segmentation is not done all at once. This
segmentation may or may not take place at the run time of the program.
● Simple Segmentation: Each process is divided into a number of
segments, all of which are loaded into memory at run time, though not
necessarily contiguously.
There is no simple relationship between logical addresses and physical addresses
in segmentation. A table stores the information about all such segments and is
called Segment Table.
4)What is Virtual Memory?
Virtual memory is a memory management technique used by operating systems
to give the appearance of a large, continuous block of memory to applications,
even if the physical memory (RAM) is limited. It allows larger applications to
run on systems with less RAM.
● The main objective of virtual memory is to support multiprogramming,
The main advantage that virtual memory provides is, a running process
does not need to be entirely in memory.
● Programs can be larger than the available physical memory. Virtual
Memory provides an abstraction of main memory, eliminating concerns
about storage limitations.
Program 1:
PROBLEM Assume that a system has a 32-bit virtual address with a 4-KB page size. Write a
STATEMENT : C program that is passed a virtual address (in decimal) on the command line and
have it output the page number and offset for the given address. As an example,
your program would run as follows: ./a.out 19986 Your program would output:
The address 19986 contains: page number = 4 offset = 3602 Writing this
program will require using the appropriate data type to store 32 bits. We
encourage you to use unsigned data types as well.
PROGRAM: #include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <virtual_address>\n", argv[0]);
return 1;
}
unsigned int address = (unsigned int) strtoul(argv[1], NULL,10);
const unsigned int page_size = 4096;
const unsigned int offset_bits = 12;
unsigned int page_number = address >> offset_bits;
unsigned int offset = address & (page_size - 1);
printf("The address %u contains:\n", address);
printf("page number = %u\n", page_number);
printf("offset = %u\n", offset);
return 0;
}
RESULT:
CONCLUSION: Through this experiment, I learned how to extract the page number and offset
from a 32-bit virtual address using bitwise operations in C. By assuming a 4 KB
page size, I understood how to divide the address space and isolate specific bits
to compute the page number (address >> 12) and offset (address & 0xFFF). This
helped me understand the practical implementation of paging in operating
systems and how memory is logically organized into pages and frames.