Os Unit 4 Notes
Os Unit 4 Notes
Memory Management
Q. Explain Memory Management: Memory Management Requirement?
Memory management keeps track of the status of each memory location, whether it is allocated or
free.
It allocates the memory dynamically to the programs at their request and frees it for reuse when it
is no longer needed.
Memory management meant to satisfy some requirements that we should keep in mind.
These Requirements of memory management are:
1. Relocation –
The available memory is generally shared among a number of processes in a
multiprogramming system, so it is not possible to know in advance which other programs will be
resident in main memory at the time of execution of this program.
Swapping the active processes in and out of the main memory enables the operating system to have
a larger pool of ready-to-execute process.
When a program gets swapped out to a disk memory, then it is not always possible that when it is
swapped back into main memory then it occupies the previous memory location, since the location
may still be occupied by another process.
We may need to relocate the process to a different area of memory.
Thus, there is a possibility that program may be moved in main memory due to swapping.
The figure depicts a process image. The process image is occupying a continuous region of main
memory.
The operating system will need to know many things including the location of process control
information, the execution stack, and the code entry.
Within a program, there are memory references in various instructions, and these are called logical
addresses.
After loading of the program into main memory, the processor and the operating system must be able
to translate logical addresses into physical addresses.
1
Branch instructions contain the address of the next instruction to be executed. Data reference
instructions contain the address of byte or word of data referenced.
The figure depicts a process image.
The process image is occupying a continuous region of main memory.
The operating system will need to know many things including the location of process control
information, the execution stack, and the code entry.
Within a program, there are memory references in various instructions, and these are called logical
addresses.
After loading of the program into main memory, the processor and the operating system must be able
to translate logical addresses into physical addresses.
Branch instructions contain the address of the next instruction to be executed. Data reference
instructions contain the address of byte or word of data referenced.
2. Protection –
There is always a danger when we have multiple programs at the same time as one program may
write to the address space of another program.
So, every process must be protected against unwanted interference when other process tries to write
in a process whether accidental or incidental.
Between relocation and protection requirement a trade-off occurs as the satisfaction of relocation
requirement increases the difficulty of satisfying the protection requirement.
Prediction of the location of a program in main memory is not possible, that’s why it is impossible to
check the absolute address at compile time to assure protection.
Most of the programming language allows the dynamic calculation of address at run time.
The memory protection requirement must be satisfied by the processor rather than
the operating system because the operating system can hardly control a process when it occupies the
processor.
Thus, it is possible to check the validity of memory references.
3. Sharing –
A protection mechanism must have to allow several processes to access the same portion of main
memory.
Allowing each processes access to the same copy of the program rather than have their own separate
copy has an advantage.
For example, multiple processes may use the same system file and it is natural to load one copy of
the file in main memory and let it shared by those processes.
It is the task of Memory management to allow controlled access to the shared areas of memory
without compromising the protection.
Mechanisms are used to support relocation supported sharing capabilities.
4. Logical Organization –
Main memory is organized as linear or it can be a one-dimensional address space which consists of
a sequence of bytes or words.
Most of the programs can be organized into modules, some of those are unmodifiable (read-only,
execute only) and some of those contain data that can be modified.
To effectively deal with a user program, the operating system and computer hardware must support
a basic module to provide the required protection and sharing.
2
It has the following advantages:
Modules are written and compiled independently and all the references from one module to another
module are resolved by `the system at run time.
Different modules are provided with different degrees of protection.
There are mechanisms by which modules can be shared among processes.
Sharing can be provided on a module level that lets the user specify the sharing that is desired.
5. Physical organization –
The structure of computer memory has two levels referred to as main memory and secondary
memory.
Main memory is relatively very fast and costly as compared to the secondary memory. Main
memory is volatile.
Thus, secondary memory is provided for storage of data on a long-term basis while the main
memory holds currently used programs.
The major system concern between main memory and secondary memory is the flow of information
and it is impractical for programmers to understand this for two reasons:
The programmer may engage in a practice known as overlaying when the main memory
available for a program and its data may be insufficient.
It allows different modules to be assigned to the same region of memory. One disadvantage is that it
is time-consuming for the programmer.
In a multiprogramming environment, the programmer does not know how much space will be
available at the time of coding and where that space will be located inside the memory.
In operating systems, Memory Management is the function responsible for allocating and managing
a computer’s main memory.
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.
There are two Memory Management Techniques: Contiguous, and Non-Contiguous.
In Contiguous Technique, executing process must be loaded entirely in the main memory.
Contiguous Technique can be divided into:
Fixed (or static) partitioning
Variable (or dynamic) partitioning
Fixed Partitioning:
This is the oldest and simplest technique used to put more than one process in the main memory.
In this partitioning, the number of partitions (non-overlapping) in RAM is fixed but the size of each
partition may or may not be the same.
As it is a contiguous allocation, hence no spanning is allowed. Here partitions are made before
execution or during system configure.
3
As illustrated in above figure, first process is only consuming 1MB out of 4MB in the main
memory.
Hence, Internal Fragmentation in first block is (4-1) = 3MB.
Sum of Internal Fragmentation in every block = (4-1)+(8-7)+(8-7)+(16-14)= 3+1+1+2 = 7MB.
Suppose process P5 of size 7MB comes. But this process cannot be accommodated in spite of
available free space because of contiguous allocation (as spanning is not allowed). Hence, 7MB
becomes part of External Fragmentation.
There are some advantages and disadvantages of fixed partitioning.
2. External Fragmentation:
The total unused space (as stated above) of various partitions cannot be used to load the
processes even though there is space available but not in the contiguous form (as spanning is not
allowed).
There are some advantages and disadvantages of variable partitioning over fixed partitioning as
given below.
Advantages of Variable Partitioning –
1. No Internal Fragmentation:
In variable Partitioning, space in main memory is allocated strictly according to the need of
process.
Hence there is no case of internal fragmentation.
There will be no unused space left in the partition.
2. No restriction on Degree of Multiprogramming:
More number of processes can be accommodated due to absence of internal fragmentation.
5
A process can be loaded until the memory is empty.
3. No Limitation on the size of the process:
In Fixed partitioning, the process with the size greater than the size of the largest partition could
not be loaded and process cannot be divided as it is invalid in contiguous allocation technique.
Here, In variable partitioning, the process size can’t be restricted since the partition size is
decided according to the process size.
Buddy allocation system is an algorithm in which a larger memory block is divided into small
parts to satisfy the request.
This algorithm is used to give best fit. The two smaller parts of block are of equal size and called
as buddies.
The buddy memory allocation technique is a memory allocation algorithm that divides memory
into partitions to try to satisfy a memory request as suitably as possible.
This system makes use of splitting memory into halves to try to give a best fit.
Static partition schemes suffer from the limitation of having the fixed number of active processes
and the usage of space may also not be optimal.
The buddy system is a memory allocation and management algorithm that manages memory in
power of two increments.
Assume the memory size is 2U, suppose a size of S is required.
If 2U-1<S<=2U: Allocate the whole block
Else: Recursively divide the block equally and test the condition at each time, when it satisfies,
allocate the block and get out the loop.
System also keep the record of all the unallocated blocks each and can merge these different size
blocks to make one big chunk.
Advantage –
Easy to implement a buddy system
Allocates block of correct size
It is easy to merge adjacent holes
6
Fast to allocate memory and de-allocating memory
Disadvantage –
It requires all allocation unit to be powers of two
It leads to internal fragmentation
Example –
Consider a system having buddy system with physical address space 128 KB.
Calculate the size of partition for 18 KB process.
So, size of partition for 18 KB process = 32 KB. It divides by 2, till possible to get minimum block
to fit 18 KB.
Relocation is the process of assigning load addresses for position-dependent code and data of
a program and adjusting the code and data to reflect the assigned addresses.
Relocation problem. Having multiple partitions presents a problem:
This means that references to particular memory addresses within the program won't be the
same every time the program runs.
For example, the program might call a particular function that begins 1024 bytes into the
program.
Memory management keeps track of the status of each memory location, whether it is
allocated or free.
It allocates the memory dynamically to the programs at their request and frees it for reuse
when it is no longer needed.
Memory management meant to satisfy some requirements that we should keep in mind.
These Requirements of memory management are:
Relocation – The available memory is generally shared among a number of processes in a
multiprogramming system, so it is not possible to know in advance which other programs will
be resident in main memory at the time of execution of this program.
Swapping the active processes in and out of the main memory enables the operating system to
have a larger pool of ready-to-execute process.
When a program gets swapped out to a disk memory, then it is not always possible that when it is
swapped back into main memory then it occupies the previous memory location, since the location
may still be occupied by another process.
7
We may need to relocate the process to a different area of memory.
Thus, there is a possibility that program may be moved in main memory due to swapping.
The figure depicts a process image.
The process image is occupying a continuous region of main memory.
The operating system will need to know many things including the location of process control
information, the execution stack, and the code entry.
Within a program, there are memory references in various instructions and these are called logical
addresses.
After loading of the program into main memory, the processor and the operating system must be
able to translate logical addresses into physical addresses.
Branch instructions contain the address of the next instruction to be executed. Data reference
instructions contain the address of byte or word of data referenced.
8
The size of the last part may be less than the page size.
The pages of process are stored in the frames of main memory depending upon
their availability.
Example
Consider a process is divided into 4 pages P0, P1, P2 and P3.
Depending upon the availability, these pages may be stored in the main memory
frames in a non-contiguous fashion as shown
Following steps are followed to translate logical address into physical address-
Step-01:
Page Number specifies the specific page of the process from which CPU wants
to read the data.
Page Offset specifies the specific word on the page that CPU wants to read.
9
Step-02:
For the page number generated by the CPU,
Page Table provides the corresponding frame number (base address of the
frame) where that page is stored in the main memory.
Step-03:
The frame number combined with the page offset forms the required physical
address.
Framenumber specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page .
Diagram-
The following diagram illustrates the above steps of translating logical address into
physical address-
Advantages-
The advantages of paging are-
It allows to store parts of a single process in a non-contiguous fashion.
It solves the problem of external fragmentation.
10
Disadvantages-
The disadvantages of paging are-
It suffers from internal fragmentation.
There is an overhead of maintaining a page table for each process.
The time taken to fetch the instruction increases since now two memory accesses are required.
Virtual memory segmentation – Each process is divided into a number of segments, not all of
which are resident at any one point in time.
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.
11
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.
Segment Table – It maps two-dimensional Logical address into one-dimensional Physical
address. It’s each table entry has:
Base Address: It contains the starting physical address where the segments reside in memory.
Limit: It specifies the length of the segment.
Segment number (s): Number of bits required to represent the segment.
Segment offset (d): Number of bits required to represent the size of the segment.
Advantages of Segmentation –
1. No Internal fragmentation.
2. Segment Table consumes less space in comparison to Page table in paging.
3. As a complete module is loaded all at once, segmentation improves CPU utilization.
4 The user’s perception of physical memory is quite similar to segmentation.
5. Users can divide user programmes into modules via segmentation.
6. These modules are nothing more than the separate processes’ codes.
7. The user specifies the segment size, whereas in paging, the hardware determines the page size.
8. Segmentation is a method that can be used to segregate data from security operations.
Disadvantage of Segmentation –
1. As processes are loaded and removed from the memory, the free memory space is broken into
little pieces, causing External fragmentation.
2. Overhead is associated with keeping a segment table for each activity.
3. Due to the need for two memory accesses, one for the segment table and the other for main
memory, access time to retrieve the instruction increases.
With virtual memory based on paging or segmentation, that job is left to the operating system and
the hardware.
As far as the programmer is concerned, they are dealing with a huge memory associated with disk
storage.
The operating system automatically loads pieces into main memory as required.
Memory references are dynamically translated into physical addresses at run time.
A process may be swapped in and out of main memory such that it occupies different regions.
A process may be broken up into pieces that do not need to be located contiguously in main
memory.
All pieces of a process do not need to be loaded in main memory during execution.
Memory references are dynamically translated into physical addresses at run time.
A process may be swapped in and out of main memory such that it occupies different regions.
The operating system (OS) manages all of the software and hardware on the computer.
It performs basic tasks such as file, memory and process management, handling input and output,
and controlling peripheral devices such as disk drives and printers.
12
System software is the software that manages the resources and allows a user to interact with
the system.
It controls a computer's internal working, mostly through an operating system.
It also manages peripherals, including printers, storage devices, and monitors.
The data and programs that make up the computer's operating system are known
as system software.
Printers, hardware drivers, function libraries, system services, system preferences, and other
configuration files are included in system files.
System software programs examples are file management tools, system utilities, assemblers,
debuggers, and compilers.
When you installed the operating system on the computer, the system software is installed.
You may also update the software by executing programs like "Windows Update" for Windows
and "Software Update" for Mac OS X.
Advantages
Disadvantages
There are various disadvantages of System Software. Some of them are as follows:
System software developers must have full knowledge of low-level languages and machine
activities.
It does not meet all of the user's requirements.
System software development and updates take longer.
If you choose a licensed and reputable organization's system software, it will be more
expensive.
13