0% found this document useful (0 votes)
50 views21 pages

Unit 4

Uploaded by

Tanisha Jain
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)
50 views21 pages

Unit 4

Uploaded by

Tanisha Jain
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

Unit-4

Memory Management

Operating Systems (BMC-203) Prepared by- [Link] Pandey


Lecture-1

Operating Systems (BMC-203) Prepared by- [Link] Pandey


In the early days of computing, before the development of modern operating systems, the execution of programs
was done directly on the hardware.
• This was made possible by two fundamental components: Bare Machine and Resident Monitor.
• These components are although outdated in today's computing environment but once they were important for
memory management and task execution.
Basic Bare Machine:-
• A Bare Machine is a computer system that operates directly with its hardware.
• It executes programs in machine language without the need for an operating system.
• Before the development of operating system, the only drawback was that it accepted instructions in only machine language
due to which only people with sufficient knowledge about Computer field are able to operate a computer. Hence, after the
development of the operating system, Bare machine is considered as inefficient.
Working of Bare Machine-
• The processor executes machine language instructions provided by the user. There is no abstraction between the user and the
machine.
• Hence, the system is highly dependent on the knowledge of machine code. Each instruction is processed by the hardware
without the help of an operating system which results in a basic, raw computing environment.
Advantages of Bare Machine-
1. Direct Control: Users can directly control the hardware without any interference from an operating system which can be
beneficial in low-level tasks or specialized applications.
2. Efficiency in Simple Tasks: For basic, single-task operations, Bare Machines can be faster than using an operating system
as there is no overhead.
3. Low Resource Requirement: Bare Machines do not require resources or memory needed by an operating system which
makes them useful in environments with limited resources.
Disadvantages of Bare Machine-
2. Lack of User-Friendliness: Bare Machines only accept machine language. Users must have a understanding of low-level
programming.
[Link] Multitasking: Unlike systems with operating systems, Bare Machines cannot handle multiple tasks simultaneously
which makes them inefficient for most general-purpose use cases.
[Link] Scope: Bare Machines are inefficient for complex applications as they lack the abstraction and multitasking features
provided by modern operating systems.

Resident Monitor:-
• Resident Monitor is a code that runs on Bare Machines. The resident monitor works
like an operating system that controls the instructions and performs all necessary
functions.
• It also works like job sequencer because it also sequences the job and sends them to the
processor.
• After scheduling the job, Resident monitors loads the programs one by one into the
main memory according to their sequences.
• One most important factor about the resident monitor is that when the program
execution occurred, there is no gap between the program execution and the processing
is going to be faster.
• The Resident monitors are divided into 4 parts as:
Control Language Interpreter, Loader, Device Drivers, Interrupt Processing

Operating Systems (BMC-203) Prepared by- [Link] Pandey


These are explained as following below:-
1. Control Language Interpreter: The first part of the Resident monitor is control language interpreter which is used to read
and carry out the instruction from one level to the next level.
2. Loader: The second part of the Resident monitor which is the main part of the Resident Monitor is Loader which Loads all
the necessary system and application programs into the main memory. It also sequences the job and sends them to the processor.
3. Device Driver: The third part of the Resident monitor is Device Driver which is used to manage the connecting input-output
devices to the system. So basically, it is the interface between the user and the system. It works as an interface between the
request and response. Request which user made, Device driver responds that the system produces to fulfill these requests.
4. Interrupt Processing: The fourth part as the name suggests, it processes the all occurred interrupt to the system.

•Working of Resident Monitor:-


•It works like job sequencer as it sequences job and sends them to the processor.
•After scheduling the job Resident monitors loads the programs one by one into the main memory according to their sequences.
•It controls the execution of instructions and ensures that programs are executed without any interruption in between.
•The Interrupt Processing feature allows the system to handle signals or requests from the hardware during execution, ensuring
smooth operations.

Advantages of Resident Monitor- Disadvantages of Resident Monitor-


[Link] Sequencing: [Link] Functionality:
[Link] Execution: [Link] Multitasking:
[Link] OS Overhead: [Link] Scalable:
[Link] System Control: [Link] Handling:

Operating Systems (BMC-203) Prepared by- [Link] Pandey


• Memory Management:-
➢ In operating systems, Memory Management is the function responsible for allocating and managing a
computer’s main memory.
➢ The memory management function keeps track of the status of each memory location, either allocated or free to
ensure effective and efficient use of Primary Memory.
Below are Memory Management Techniques:-
•Contiguous
•Non-Contiguous
In the Contiguous Technique, the executing process must be loaded entirely in the main memory.
The contiguous Technique can be divided into:
•Fixed (static) partitioning
•Variable (dynamic) partitioning
Multi-programming with Fixed (static) Partitioning :-
• Multi-programming with fixed partitioning is a contiguous memory management technique in which the main memory is
divided into fixed sized partitions.
• Whenever we have to allocate a process memory then a free partition that is big enough to hold the process is found. Then
the memory is allocated to the process.
• If there is no free space available then the process waits in the queue to be allocated memory. It is one of the oldest memory
management technique which is easy to implement.
• Multiprogramming with fixed partitions, also known as static partitioning.

Operating Systems (BMC-203) Prepared by- [Link] Pandey


Advantages:-
➢ Simplicity: It is straightforward to implement because the partitions are static and do not change.
➢ Predictability: The operating system can ensure a minimum amount of memory for each process.
➢ Security: Processes are isolated in their own partitions, which can prevent them from interfering with
each other’s memory space.
Disadvantages:-
➢ Internal fragmentation: If a process’s memory requirements are smaller than the partition size, the
remaining memory within the partition goes unused.
➢ Limitation on process size: A process cannot be larger than the largest partition, which imposes a
limitation on the size of processes that can be loaded into memory.
➢ Degree of multiprogramming is fixed: The number of processes that can run concurrently is limited by the number of
partitions, and the system may not be able to accommodate as many processes as it could with variable partitioning.
Multi-programming with variable (dynamic) partitioning:- Multi-programming with variable partitioning is a
contiguous memory management technique in which the main memory is not divided into partitions and the process is
allocated a chunk of free memory that is big enough for it to fit. The space which is left is considered as the free space which
can be further used by other processes. It also provides the concept of compaction. In compaction the spaces that are free and
the spaces which not allocated to the process are combined and single large memory space is made.
Advantages: -
➢ No internal fragmentation: Partitions are exactly the size needed for the process, so no space within a partition goes unused.
➢ No limit on the degree of multiprogramming: More processes can be accommodated in memory at once, as there is no
wasted space.
➢ No limitation on the size of the process: Unlike fixed partitioning, where a process cannot be larger than the largest partition,
variable partitioning allows any size of process as long as there is enough free memory.
Operating Systems (BMC-203) Prepared by- [Link] Pandey
Disadvantages:
➢ Difficult implementation: Allocating memory at run-time is more complex than pre-
allocated fixed partitions.
➢ External fragmentation: Although internal fragmentation is avoided, external
fragmentation can occur when there are small blocks of free memory scattered throughout the
system that cannot be used effectively.

Operating Systems (BMC-203)


Prepared by- [Link] Pandey
Best Fit, First Fit, Worst Fit:- Storage placement strategies are used in memory management (especially in dynamic
memory allocation) to decide where a new process or data block should be placed in memory.
These strategies impact how efficiently memory is utilized, how fragmented memory becomes, and how fast allocation
occurs. Here's a discussion of Best Fit, First Fit, and Worst Fit, with suitable examples:
(i) Best Fit Definition:
The Best Fit strategy allocates the smallest available block that is large enough to accommodate the requested memory. It
tries to minimize leftover space (internal fragmentation).
Advantages-
•Reduces wasted space within allocated memory blocks.
•Tries to preserve large blocks for future requests.
Disadvantages-
•Slower allocation time (must search the whole list).
•Can lead to many small unusable memory fragments (external fragmentation).
Example:
Assume memory blocks:
•Block A: 100 KB
•Block B: 500 KB
•Block C: 200 KB
•Block D: 300 KB
Request: Allocate 210 KB
Best Fit Choice: Block D (300 KB), Because 300 KB is the smallest block that is ≥ 210 KB.

Operating Systems (BMC-203) Prepared by- [Link] Pandey


(ii) First Fit Definition:- (iii) Worst Fit Definition:-
The First Fit strategy allocates memory to the first available The Worst Fit strategy allocates memory to the largest available
block that is large enough to accommodate the requested block. The idea is to leave the biggest leftover space, hoping it will
size. be useful for future large requests.
Advantages: Advantages:
•Faster allocation (doesn’t search entire list). •Tries to avoid small unusable fragments.
•Simple to implement. •Might result in larger blocks being available for future use.
Disadvantages: Disadvantages:
•Can leave small unusable fragments at the start. •Often leads to poor utilization.
•Might cause fragmentation at the beginning of memory. •Still prone to fragmentation.
Example: Example:
Same memory blocks: Same memory blocks:
•Block A: 100 KB •Block A: 100 KB
•Block B: 500 KB •Block B: 500 KB
•Block C: 200 KB •Block C: 200 KB
•Block D: 300 KB •Block D: 300 KB
Request: Allocate 210 KB Request: Allocate 210 KB
First Fit Choice: Block B (500 KB), Because it's the first block Worst Fit Choice: Block B (500 KB), Because it is the largest block
(in order) that is ≥ 210 KB. and can accommodate the request.
Strategy Search Time Fragmentation Suitability
Good when space utilization is a
Best Fit- Slow External
priority
First Fit- Fast Moderate Good for speed and simplicity
Operating Systems (BMC-203)
Worst Fit- Slow Often poorPrepared by- [Link] Pandey Rarely used.
Example- Five memory partitions of 100, 500, 200, 300 & 600 (all in KB) are in order, how would the ‘first-fit, best-fit & worst-
fit’ shall place processes of size 212, 417, 112, & 426 ((all in KB). Deduce the most efficient memory management technique.
Sol. AKTU: 2022-23
Memory Partitions (in KB) Process Sizes (in KB)
•100 KB •P1: 212 KB
•500 KB •P2: 417 KB
•200 KB •P3: 112 KB
•300 KB •P4: 426 KB
•600 KB
1. First-Fit Allocation:- 2. Best-Fit Allocation:-
•P1 (212 KB): Allocated to the first partition that fits, •P1 (212 KB): Allocated to the smallest partition that fits, which
which is 500 KB. is 300 KB.
•P2 (417 KB): Allocated to the first available partition •P2 (417 KB): Allocated to the smallest partition that fits, which
that fits, which is 600 KB. is 500 KB.
•P3 (112 KB): Allocated to the first available partition •P3 (112 KB): Allocated to the smallest partition that fits, which
that fits, which is 200 KB. is 200 KB.
•P4 (426 KB): Cannot be allocated as no remaining •P4 (426 KB): Allocated to the smallest partition that fits, which
partition is large enough. is 600 KB.
Resulting Partitions: Resulting Partitions:
•100 KB: Unused •100 KB: Unused
•500 KB: 288 KB free •500 KB: 83 KB free
•200 KB: 88 KB free •200 KB: 88 KB free
•300 KB: Unused •300 KB: 88 KB free
•600 KB: 183 KB free •600 KB: 174 KB free
3. Worst-Fit Allocation:-
•P1 (212 KB): Allocated to the largest partition that fits, which is 600 KB.
•P2 (417 KB): Allocated to the largest partition that fits, which is 500 KB.
•P3 (112 KB): Allocated to the largest partition that fits, which is 300 KB.
•P4 (426 KB): Cannot be allocated as no remaining partition is large enough.
Resulting Partitions:
•100 KB: Unused
•500 KB: 83 KB free
•200 KB: Unused
•300 KB: 188 KB free
•600 KB: 388 KB free

Algorithm Allocated Processes Unallocated Processes Remaining Free Space (KB)


First-Fit P1, P2, P3 P4 288, 88, 183
Best-Fit P1, P2, P3, P4 None 83, 88, 88, 174
Worst-Fit P1, P2, P3 P4 83, 188, 388

Conclusion:
Best-Fit is the most efficient memory management technique among the three, as it minimizes fragmentation and
utilizes memory more effectively.

Operating Systems (BMC-203) Prepared by- [Link] Pandey


Lecture-2

Operating Systems (BMC-203) Prepared by- [Link] Pandey


• Protection Scheme:-
❑ Memory protection is a way to control memory access rights on a computer, and is a part of most modern instruction set
architectures and operating systems.
❑ The main purpose of memory protection is to prevent a process from accessing memory that has not been allocated to it.
The primary goal of safeguarding memory is to avert an application from accessing RAM without permission.
❑ Whenever an approach attempts to use memory that it does not have permission to enter, the computer's operating system
will stop and end the process. This hinders the program from obtaining memory that it should not.

Different Scheme of Memory Protection-


Segmentation:-Memory is segmented into sections, every single one which can have a separate set of access rights. An OS
kernel segment, for instance, might be read-only, whereas a user data segment could have been designated as read-write.

Paged Virtual Memory:-Memory is divided into pages in paged virtual memory, and each page can be saved to its own place
in physical memory. In order to maintain track of where pages are kept, the OS uses a page table. This gives the operating
system the ability to move pages to various parts of physical memory, where they can be secured against unauthorized access.

Protection keys:- Each RAM page has a set of bits called encryption keys. Accessibility to the page can be controlled using
these bits. A protection key could be utilized, for instance, to specify whether or not a document will be read, written to, or
operated.

Operating Systems (BMC-203)


Prepared by- [Link] Pandey
• Paging:-
➢ In Operating Systems, Paging is a storage mechanism used
to retrieve processes from the secondary storage into the Secondary
main memory in the form of pages. Memory
➢ The main idea behind the paging is to divide each process
in the form of pages. The main memory will also be divided
in the form of frames.
➢ Pages of the process are brought into the main memory
only when they are required otherwise, they reside in the
secondary storage.
➢ Different operating system defines different frame sizes.
The sizes of each frame must be equal.
➢ Considering the fact that the pages are mapped to the
frames in Paging, page size needs to be as same as frame
size.
Example
• Let us consider the main memory size 16 Kb and Frame size is
1 KB therefore the main memory will be divided into the
collection of 16 frames of 1 KB each.
• There are 4 processes in the system that is P1, P2, P3 and P4
of 4 KB each.
• Each process is divided into pages of 1 KB each so that one
page can be stored in one frame.
• Initially, all the frames are empty therefore pages of the
processes will get stored in the contiguous way.

Frames, pages and the mapping between the two is shown in


the image below→

• Now let us consider that, suppose P2 and P4 are moved to


waiting state after some time.
• Now, 8 frames become empty and therefore other pages can be
loaded in that empty place.
• The process P5 of size 8 KB (8 pages) is waiting inside the
ready queue.

Operating Systems (BMC-203) Prepared by- [Link] Pandey


• Given the fact that, we have 8 non-contiguous frames
available in the memory and paging provides the flexibility
of storing the process at the different places.
• Therefore, we can load the pages of process P5 in the place
of P2 and P4.
• Figure is given in slide-

Memory Management Unit:-


• The purpose of Memory Management Unit (MMU) is to
convert the logical address into the physical address.
• The logical address is the address generated by the CPU for
every page while the physical address is the actual address of
the frame where each page will be stored.

When a page is to be accessed by the CPU by using the logical


address, the operating system needs to obtain the physical address
to access that page physically.
The logical address has two parts.
1. Page Number
2. Offset
Segmentation:-
❑ Segmentation is a memory management technique in which the memory is divided into the variable size parts. Each part is
known as a segment which can be allocated to a process.
❑ The details about each segment are stored in a table called a segment table.
❑ Segment table contains mainly two information about segment:
1. Base Address: It is the base address of the segment
2. Limit: It is the length of the segment.
• It is better to have segmentation which divides the process into the segments.
• Each segment contains the same type of functions such as the main function can be included in one segment and the library
functions can be included in the other segment.
• The segment and offset are combined to form a full memory address in secondary memory.
Translation of logical address into physical address by segment table-
CPU generates a logical address which contains two
parts:
1. Segment Number
2. Segment Offset (Segment limit)

Operating Systems (BMC-203)


Prepared by- [Link] Pandey
For Example: -
Suppose in16-bit address, 4 bits are used for the segment number and
12 bits for the segment offset, So, the maximum segment size is 4096.
Maximum number of segments that can be refereed is 16.
• When a program is loaded into memory, the segmentation system
tries to locate space that is large enough to hold the first segment of
the process. Space information is obtained from the free list
maintained by memory manager.
• Then it tries to locate space for other segments. Once adequate
space is located for all the segments, it loads them into their
respective areas. The operating system also generates a segment
map table for each program.
• With the help of segment map tables and hardware assistance, the
operating system can easily translate a logical address into physical
address on execution of a program.
• The Segment number is mapped to the segment table. The limit of
the respective segment is compared with the offset. If the offset is
less than the limit then the address is valid otherwise it throws an
error as the address is invalid.
• In the case of valid addresses, the base address of the segment is
added to the offset to get the physical address of the actual word in
the main memory.
The given figure shows how address translation is done in case of
segmentation.→
Differences between Segmentation & Paging-

Operating Systems (BMC-203)


Prepared by- [Link] Pandey
Virtual Memory
+
Cache Memory

Operating Systems (BMC-203)


Prepared by- [Link] Pandey

You might also like