Professional Documents
Culture Documents
Unit-4 (Os)
Unit-4 (Os)
Memory Management
Memory is the important part of the computer that is used to store the data. Its management is critical to
the computer system because the amount of main memory available in a computer system is very limited.
At any time, many processes are competing for it. Moreover, to increase performance, several processes are
executed simultaneously. For this, we must keep several processes in the main memory, so it is even more
important to manage them effectively.
1
Unit - 4
Logical Address is generated by CPU while a program is running. The logical address is virtual address as it
does not exist physically, therefore, it is also known as Virtual Address. This address is used as a reference
to access the physical memory location by CPU. The term Logical Address Space is used for the set of all
logical addresses generated by a program’s perspective.
The hardware device called Memory-Management Unit is used for mapping logical address to its
corresponding physical address.
Physical Address identifies a physical location of required data in a memory. The user never directly deals
with the physical address but can access by its corresponding logical address. The user program generates
the logical address and thinks that the program is running in this logical address but the program needs
physical memory for its execution, therefore, the logical address must be mapped to the physical address
by MMU before they are used. The term Physical Address Space is used for all physical addresses
corresponding to the logical addresses in a Logical address space.
1. The basic difference between Logical and physical address is that Logical address is generated by
CPU in perspective of a program whereas the physical address is a location that exists in the
memory unit.
2. Logical Address Space is the set of all logical addresses generated by CPU for a program whereas
the set of all physical address mapped to corresponding logical addresses is called Physical Address
Space.
3. The logical address does not exist physically in the memory whereas physical address is a location in
the memory that can be accessed physically.
4. Identical logical addresses are generated by Compile-time and Load time address binding methods
whereas they differs from each other in run-time address binding method. Please refer this for
details.
5. The logical address is generated by the CPU while the program is running whereas the physical
address is computed by the Memory Management Unit (MMU).
2
Unit - 4
Comparison Chart:
Paging
In Operating Systems, Paging is a storage mechanism used to retrieve processes from the secondary storage
into the main memory in the form of pages.
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.
One page of the process is to be stored in one of the frames of the memory. The pages can be stored at the
different locations of the memory but the priority is always to find the contiguous frames or holes.
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.
3
Unit - 4
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.
Let us consider that, 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.
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.
4
Unit - 4
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.
• Page Number
• Offset
Memory management unit of OS needs to convert the page number to the frame number.
Example
Considering the above image, let's say that the CPU demands 10th word of 4th page of process P3. Since the
page number 4 of process P1 gets stored at frame number 9 therefore the 10th word of 9th frame will be
returned as the physical address.
Segmentation
A process is divided into Segments. The chunks that a program is divided into which are not necessarily all
of the same sizes are called segments. Segmentation gives the user’s view of the process which paging
does not give. Here the user’s view is mapped to physical memory. There are types of segmentation:
• 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.
5
Unit - 4
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.
Advantages of Segmentation –
• No Internal fragmentation.
• Segment Table consumes less space in comparison to Page table in paging.
• As a complete module is loaded all at once, segmentation improves CPU utilization.
• The user’s perception of physical memory is quite similar to segmentation. Users can divide user
programmes into modules via segmentation. These modules are nothing more than the separate
processes’ codes.
• The user specifies the segment size, whereas in paging, the hardware determines the page size.
• Segmentation is a method that can be used to segregate data from security operations.
6
Unit - 4
Disadvantage of Segmentation –
• As processes are loaded and removed from the memory, the free memory space is broken into little
pieces, causing External fragmentation.
• Overhead is associated with keeping a segment table for each activity.
• 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.
Virtual Memory
Virtual Memory is a storage scheme that provides user an illusion of having a very big main memory. This is
done by treating a part of secondary memory as the main memory.
In this scheme, User can load the bigger size processes than the available main memory by having the illusion
that the memory is available to load the process.
Instead of loading one big process in the main memory, the Operating System loads the different parts of
more than one process in the main memory.
By doing this, the degree of multiprogramming will be increased and therefore, the CPU utilization will also
be increased.
In modern word, virtual memory has become quite common these days. In this scheme, whenever some
pages needs to be loaded in the main memory for the execution and the memory is not available for those
many pages, then in that case, instead of stopping the pages from entering in the main memory, the OS
search for the RAM area that are least used in the recent times or that are not referenced and copy that into
the secondary memory to make the space for the new pages in the main memory.
Since all this procedure happens automatically, therefore it makes the computer feel like it is having the
unlimited RAM.
7
Unit - 4
In an operating system that uses paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when a new page comes in.
Page Fault: A page fault happens when a running program accesses a memory page that is mapped into
the virtual address space but not loaded in physical memory. Since actual physical memory is much smaller
than virtual memory, page faults happen. In case of a page fault, Operating System might have to replace
one of the existing pages with the newly needed page. Different page replacement algorithms suggest
different ways to decide which page to replace. The target for all algorithms is to reduce the number of
page faults.
8
Unit - 4
This is the simplest page replacement algorithm. In this algorithm, the operating system keeps track of all
pages in the memory in a queue, the oldest page is in the front of the queue. When a page needs to be
replaced page in the front of the queue is selected for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames.Find the number of page
faults.
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is not available in memory so
it replaces the oldest page slot i.e 1. —>1 Page Fault. 6 comes, it is also not available in memory so it
replaces the oldest page slot i.e 3 —>1 Page Fault. Finally, when 3 come it is not available so it replaces 0 1
page fault.
Belady’s anomaly proves that it is possible to have more page faults when increasing the number of page
frames while using the First in First Out (FIFO) page replacement algorithm. For example, if we consider
reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if we increase
slots to 4, we get 10-page faults.
Linux
Linux is one of popular version of UNIX operating System. It is open source as its source code is freely
available. It is free to use. Linux was designed considering UNIX compatibility. Its functionality list is quite
similar to that of UNIX.
9
Unit - 4
Basic Features
10
Unit - 4
Kernel component code executes in a special privileged mode called kernel mode with full access to all
resources of the computer. This code represents a single process, executes in single address space and do
not require any context switch and hence is very efficient and fast. Kernel runs each processes and provides
system services to processes, provides protected access to hardware to processes.
Support code which is not required to run in kernel mode is in System Library. User programs and other
system programs works in User Mode which has no access to system hardware and kernel code. User
programs/ utilities use System libraries to access Kernel functions to get system's low level tasks.
Architecture
In Linux/Unix operating system everything is a file even directories are files, files are files, and devices like
mouse, keyboard, printer, etc are also files. Here we are going to see the Directory Structure in Linux.
11
Unit - 4
Types of files
1. General Files – It is also called ordinary files. It may be an image, video, program, or simple text
files. These types of files can be in ASCII or Binary format. It is the most commonly used file in the
Linux system.
2. Directory Files – These types of files are a warehouse for other file types. It may be a directory file
within a directory (subdirectory).
3. Device Files – In a Windows-like operating system, devices like CD-ROM, and hard drives are
represented as drive letters like F: G: H whereas in the Linux system device are represented as files.
As for example, /dev/sda1, /dev/sda2 and so on.
We know that Windows-like operating system, files are stored in different folders on different data drives
like C: D: E: whereas in the Linux/Unix operating system files are stored in a tree-like structure starting with
the root directory as shown in the below diagram.
The base of the Linux/Unix file system hierarchy begins at the root and everything starts with the root
directory.
These are the common top-level directories associated with the root directory:
• /bin – binary or executable programs.
• /etc – system configuration files.
• /home – home directory. It is the default current directory.
• /opt – optional or third-party software.
• /tmp – temporary space, typically cleared on reboot.
• /usr – User related programs.
• /var – log files.
12
Unit - 4
We know that Linux is a very complex system that requires an efficient way to start, stop, maintain and
reboot a system, unlike Windows operating system. In the Linux system some well-defined configuration
files, binaries, man pages information files available for every process.
Device Files:
• /dev/hda – Device file for the first IDE HDD.
• /dev/hdc – A pseudo-device that output garbage output is redirected to /dev/null.
13
Unit - 4
Log Files:
• /var/log/lastlog – It stores user last login info.
• /var/log/messages – It has all the global system messages.
• /var/log/wtmp – It keeps a history of login and logout information.
14
Unit - 4
To check the Linux directories open the terminal and execute sudo -s followed by system password to give
root privilege. Then after changing the current home directory to the root directory and check the list of all
available directories in the base directory as shown below.
15