Operating Systems Notes-Unit 4
Operating Systems Notes-Unit 4
Chapter – I
Memory Management:
The term Memory can be defined as a collection of data in a specific format. It is used to store
instructions and processed data. The memory comprises a large array or group of words or bytes, each with
its own location.
The primary motive of a computer system is to execute programs. These programs, along with the
information they access, should be in the main memory during execution. The CPU fetches instructions from
memory according to the value of the program counter.
To achieve a degree of multiprogramming and proper utilization of memory, memory management is
important. Many memory management methods exist, reflecting various approaches, and the effectiveness of
each algorithm depends on the situation.
The main memory is central to the operation 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 lost its data when a power interruption occurs.
In a multiprogramming computer, the operating system resides in a part of memory and t he 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.
Why Memory Management is required:
Logical Address space: An address generated by the CPU is known as “Logical Address”. It is also
known as a Virtual address. Logical address space can be defined as the size of the process. A logical address
can be changed.
Physical Address space: An address seen by the memory unit (i.e the one loaded into the memory
address register of the memory) is commonly known as a “Physical Address”. A Physical address is also
known as a Real address. The set of all physical addresses corresponding to these logical addresses is known
as Physical address space.
A physical address is computed by MMU. The run-time mapping from virtual to physical addresses is
done by a hardware device Memory Management Unit(MMU). The physical address always remains
constant.
To load a process into the main memory is done by a loader. There are two different types of loading :
o Static loading:- loading the entire program into a fixed address. It requires more memory space.
o Dynamic loading:- The entire program and all data of a process must be in physical memory for the
process to execute. So, the size of a process is limited to the size of physical memory. To gain proper
memory utilization, dynamic loading is used.
In dynamic loading, a routine is not loaded until it is called. All routines are residing on disk in
a relocatable load format. One of the advantages of dynamic loading is that unused routine is never
loaded. This loading is useful when a large amount of code is needed to handle it efficiently.
To perform a linking task a linker is used. A linker is a program that takes one or more object files
generated by a compiler and combines them into a single executable file.
Static linking: In static linking, the linker combines all necessary program modules into a single
executable program. So there is no runtime dependency.
Some operating systems support only static linking, in which system language libraries are treated like
any other object module.
Dynamic linking: The basic concept of dynamic linking is similar to dynamic loading. In dynamic
linking, “Stub” is included for each appropriate library routine reference.
A stub is a small piece of code. When the stub is executed, it checks whether the needed routine is
already in memory or not. If not available then the program loads the routine into memory.
Swapping
Swapping is a memory management scheme in which any process can be temporarily swapped from
main memory to secondary memory so that the main memory can be made available for other processes.
It is used to improve main memory utilization. In secondary memory, the place where the swapped-out
process is stored is called swap space.
The purpose of the swapping in operating system is to access the data present in the hard disk and bring
it to RAM so that the application programs can use it. The thing to remember is that swapping is used only
when data is not present in RAM.
Although the process of swapping affects the performance of the system, it helps to run larger and more
than one process. This is the reason why swapping is also referred to as memory compaction.
The concept of swapping has divided into two more concepts: Swap-in and Swap-out.
o Swap-out is a method of removing a process from RAM and adding it to the hard disk.
o Swap-in is a method of removing a program from a hard disk and putting it back into the main memory
or RAM.
When a process is executed it must have resided in memory. Swapping is a process of swap a process
temporarily into a secondary memory from the main memory, which is fast as compared to secondary
memory.
A swapping allows more processes to be run and can be fit into memory at one time. The main part of
swapping is transferred time and the total time directly proportional to the amount of memory swapped.
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.
In this memory allocation, all the available memory space remains together in one place which implies
that the freely available memory partitions are not spread over here and there across the whole memory space.
Contiguous Memory allocation is achieved just by dividing the memory into the fixed-sized partition.
The memory can be divided either in the fixed-sized partition or in the variable-sized partition in
order to allocate contiguous space to user processes.
Fixed-size Partition Scheme
This technique is also known as Static partitioning. In this scheme, the system divides the memory into
fixed-size partitions. The partitions may or may not be the same size. The size of each partition is fixed as
indicated by the name of the technique and it cannot be changed.
In this partition scheme, each partition may contain exactly one process. There is a problem that this
technique will limit the degree of multiprogramming because the number of partitions will basically decide the
number of processes.
Whenever any process terminates then the partition becomes available for another process.
Example
Let's take an example of fixed size partitioning scheme, we will divide a memory size of 15 KB into
fixed-size partitions:
It is important to note that these partitions are allocated to the processes as they arrive and the partition
that is allocated to the arrived process basically depends on the algorithm followed.
If there is some wastage inside the partition then it is termed Internal Fragmentation.
As partition size varies according to the need of the process so in this partition scheme there is
no internal fragmentation.
Segmentation
In Operating Systems, 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 is stored in
one (or many) of the segments.
Till now, we were using Paging as our main memory management technique. Paging is more close to
the Operating system rather than the User. It divides all the processes into the form of pages regardless of the
fact that a process can have some relative parts of functions which need to be loaded in the same page.
Operating system doesn't care about the User's view of the process. It may divide the same function into
different pages and those pages may or may not be loaded at the same time into the memory. It decreases the
efficiency of the system.
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.
For Example:
Suppose a 16 bit address is used with 4 bits for the segment number and 12 bits for the segment offset so
the maximum segment size is 4096 and the 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 above figure shows how address translation is done in case of segmentation.
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compared to the page table in paging.
Paging
Paging is a storage mechanism that allows OS to retrieve processes from the secondary storage into the
main memory in the form of pages.
In the Paging method, the main memory is divided into small fixed-size blocks of physical memory,
which is called frames.
The size of a frame should be kept the same as that of a page to have maximum utilization of the main
memory and to avoid external fragmentation. Paging is used for faster access to data, and it is a logical concept.
Example of Paging in OS
For example, if the main memory size is 16 KB and Frame size is 1 KB. Here, the main memory will be
divided into the collection of 16 frames of 1 KB each.
There are 4 separate processes in the system that is A1, A2, A3, and A4 of 4 KB each. Here, all the
processes are divided into pages of 1 KB each so that operating system can store one page in one frame.
At the beginning of the process, all the frames remain empty so that all the pages of the processes will
get stored in a contiguous way.
In this example you can see that A2 and A4 are moved to the waiting state after some time. Therefore,
eight frames become empty, and so other pages can be loaded in that empty blocks.
The process A5 of size 8 pages (8 KB) are waiting in the ready queue.
In this example, you can see that there are eight non-contiguous frames which is available in the
memory, and paging offers the flexibility of storing the process at the different places.
Advantages of Paging
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as
though it were part of the main memory.
The addresses a program may use to reference memory are distinguished from the addresses the
memory system uses to identify physical storage sites, and program-generated addresses are translated
automatically to the corresponding machine addresses.
The size of virtual storage is limited by the addressing scheme of the computer system and the amount
of secondary memory is available not by the actual number of the main storage locations.
It is a technique that is implemented using both hardware and software. It maps memory addresses
used by a program, called virtual addresses, into physical addresses in computer memory.
All memory references within a process are logical addresses that are dynamically translated into
physical addresses at run time. This means that a process can be swapped in and out of the main
memory such that it occupies different places in the main memory at different times during the course
of execution.
A process may be broken into a number of pieces and these pieces need not be continuously located in
the main memory during execution. The combination of dynamic run-time address translation and use
of page or segment table permits this.
A computer can address more memory than the amount physically installed on the system. This extra
memory is actually called virtual memory and it is a section of a hard disk that's set up to emulate the
computer's RAM.
The main visible advantage of this scheme is that programs can be larger than physical memory. Virtual
memory serves two purposes. First, it allows us to extend the use of physical memory by using disk. Second, it
allows us to have memory protection, because each virtual address is translated to a physical address.
Following are the situations, when entire program is not required to be loaded fully in main memory.
User written error handling routines are used only when an error occurred in the data or computation.
Certain options and features of a program may be used rarely.
Many tables are assigned a fixed amount of address space even though only a small amount of the table
is actually used.
The ability to execute a program that is only partially in memory would counter many benefits.
Less number of I/O would be needed to load or swap each user program into memory.
A program would no longer be constrained by the amount of physical memory that is available.
Each user program could take less physical memory, more programs could be run the same time, with a
corresponding increase in CPU utilization and throughput.
Modern microprocessors intended for general-purpose use, a memory management unit, or MMU, is
built into the hardware. The MMU's job is to translate virtual addresses into physical addresses.
A basic example is given below −
Virtual memory is commonly implemented by demand paging. It can also be implemented in a
segmentation system. Demand segmentation can also be used to provide virtual memory.
Demand paging
A demand paging system is quite similar to a paging system with swapping where processes reside in
secondary memory and pages are loaded only on demand, not in advance.
When a context switch occurs, the operating system does not copy any of the old program‟s pages out to
the disk or any of the new program‟s pages into the main memory Instead, it just begins executing the new
program after loading the first page and fetches that program‟s pages as they are referenced.
While executing a program, if the program references a page which is not available in the main memory
because it was swapped out a little ago, the processor treats this invalid memory reference as a page fault and
transfers control from the program to the operating system to demand the page back into the memory.
Advantages
Copy-on-Write (CoW)
Copy-on-Write (CoW) is mainly a resource management technique that allows the parent and child
process to share the same pages of the memory initially. If any process either parent or child modifies the shared
page, only then the page is copied.
The CoW is basically a technique of efficiently copying the data resources in the computer system. In
this case, if a unit of data is copied but is not modified then "copy" can mainly exist as a reference to the
original data.
But when the copied data is modified, then at that time its copy is created(where new bytes are actually
written )as suggested from the name of the technique.
The main use of this technique is in the implementation of the fork system call in which it shares the
virtual memory/pages of the Operating system.
Recall in the UNIX(OS), the fork() system call is used to create a duplicate process of the parent
process which is known as the child process.
The CoW technique is used by several Operating systems like Linux, Solaris, and Windows XP.
The CoW technique is an efficient process creation technique as only the pages that are modified are
copied.
Free pages in this technique are allocated from a pool of zeroed-out pages.
The main intention behind the CoW technique is that whenever a parent process creates a child process
both parent and child process initially will share the same pages in the memory.
These shared pages between parent and child process will be marked as copy-on-write which means that
if the parent or child process will attempt to modify the shared pages then a copy of these pages will be
created and the modifications will be done only on the copy of pages by that process and it will not
affect other processes.
Now its time to take a look at the basic example of this technique:
Let us take an example where Process A creates a new process that is Process B, initially both these
processes will share the same pages of the memory.
Page replacement
Page replacement algorithms are the techniques using which an Operating System decides which
memory pages to swap out, write to disk when a page of memory needs to be allocated.
Paging happens whenever a page fault occurs and a free page cannot be used for allocation purpose
accounting to reason that pages are not available or the number of free pages is lower than required pages.
When the page that was selected for replacement and was paged out, is referenced again, it has to read
in from disk, and this requires for I/O completion.
This process determines the quality of the page replacement algorithm: the lesser the time waiting for
page-ins, the better is the algorithm.
A page replacement algorithm looks at the limited information about accessing the pages provided by
hardware, and tries to select which pages should be replaced to minimize the total number of page misses,
while balancing it with the costs of primary storage and processor time of the algorithm itself.
There are many different page replacement algorithms. We evaluate an algorithm by running it on a
particular string of memory reference and computing the number of page faults,
Reference String
The string of memory references is called reference string. Reference strings are generated artificially or
by tracing a given system and recording the address of each memory reference.
The latter choice produces a large number of data, where we note two things.
For a given page size, we need to consider only the page number, not the entire address.
If we have a reference to a page p, then any immediately following references to page p will never
cause a page fault. Page p will be in memory after the first reference; the immediately following
references will not fault.
For example, consider the following sequence of addresses − 123,215,600,1234,76,96
If page size is 100, then the reference string is 1,2,6,12,0,0
First In First Out (FIFO) algorithm
This algorithm is based on the argument that the page with the smallest count was probably just brought
in and has yet to be used.
Allocation of Frames
The main memory of the operating system is divided into various frames. The process is stored in these
frames, and once the process is saved as a frame, the CPU may run it.
As a result, the operating system must set aside enough frames for each process. As a result, the
operating system uses various algorithms in order to assign the frame.
Demand paging is used to implement virtual memory, an essential operating system feature. It requires
the development of a page replacement mechanism and a frame allocation system.
If you have multiple processes, the frame allocation techniques are utilized to define how many frames
to allot to each one. A number of factors constrain the strategies for allocating frames:
1. You cannot assign more frames than the total number of frames available.
2. A specific number of frames should be assigned to each process. This limitation is due to two factors.
The first is that when the number of frames assigned drops, the page fault ratio grows, decreasing the
process's execution performance. Second, there should be sufficient frames to hold all the multiple pages
that any instruction may reference.
There are mainly five ways of frame allocation algorithms in the OS. These are as follows:
In equal frame allocation, the processes are assigned equally among the processes in the OS. For
example, if the system has 30 frames and 7 processes, each process will get 4 frames.
The 2 frames that are not assigned to any system process may be used as a free-frame buffer pool in the
system.
Proportional Frame Allocation
The proportional frame allocation technique assigns frames based on the size needed for execution and
the total number of frames in memory.
The allocated frames for a process pi of size si are ai = (si/S)*m, in which S represents the total of all
process sizes, and m represents the number of frames in the system.
Priority frame allocation assigns frames based on the number of frame allocations and the processes.
Suppose a process has a high priority and requires more frames that many frames will be allocated to it.
Following that, lesser priority processes are allocated.
When a process requires a page that isn't currently in memory, it may put it in and select a frame from
the all frames sets, even if another process is already utilizing that frame. In other words, one process may take
a frame from another.
When a process requires a page that isn't already in memory, it can bring it in and assign it a frame from
its set of allocated frames.
Thrashing
Thrashing is a condition or a situation when the system is spending a major portion of its time in
servicing the page faults, but the actual processing done is very negligible.
A locality is a set of pages that are actively used together. The locality model states that as a process
executes, it moves from one locality to another. A program is generally composed of several different
localities which may overlap.
For example when a function is called, it defines a new locality where memory references are made to
the instructions of the function call, it‟s local and global variables, etc. Similarly, when the function is exited,
the process leaves this locality.
Techniques to handle:
This model is based on the above-stated concept of the Locality Model. The basic principle states that
if we allocate enough frames to a process to accommodate its current locality, it will only fault whenever it
moves to some new locality. But if the allocated frames are lesser than the size of the current locality, the
process is bound to thrash.
According to this model, based on a parameter A, the working set is defined as the set of pages in the
most recent „A‟ page references. Hence, all the actively used pages would always end up being a part of the
working set.
The accuracy of the working set is dependent on the value of parameter A. If A is too large, then
working sets may overlap. On the other hand, for smaller values of A, the locality might not be covered
entirely.
If D is the total demand for frames and is the working set size for a process i, Now, if „m‟ is the
number of frames available in the memory, there are 2 possibilities:
(i) D>m i.e. total demand exceeds the number of frames, then thrashing will occur as some processes
would not get enough frames.
(ii) D<=m, then there would be no thrashing.
If the page fault rate falls below the lower limit, frames can be removed from the process. Similarly, if
the page fault rate exceeds the upper limit, more number of frames can be allocated to the process.
In other words, the graphical state of the system should be kept limited to the rectangular region
formed in the given diagram.
Here too, if the page fault rate is high with no free frames, then some of the processes can be
suspended and frames allocated to them can be reallocated to other processes. The suspended processes can
then be restarted later.