Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Unit - 4

Operating System Unit – 4

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.

Role of Memory management

Following are the important roles of memory management in a computer system:


• Memory manager is used to keep track of the status of memory locations, whether it is free or
allocated. It addresses primary memory by providing abstractions so that software perceives a large
memory is allocated to it.
• Memory manager permits computers with a small amount of main memory to execute programs
larger than the size or amount of available memory. It does this by moving information back and
forth between primary memory and secondary memory by using the concept of swapping.
• The memory manager is responsible for protecting the memory allocated to each process from being
corrupted by another process. If this is not ensured, then the system may exhibit unpredictable
behavior.
• Memory managers should enable sharing of memory space between processes. Thus, two programs
can reside at the same memory location although at different times.

1
Unit - 4

Logical and Physical Address

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.

Mapping virtual-address to physical-addresses

Differences Between Logical and Physical Address

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:

Parameter LOGICAL ADDRESS PHYSICAL ADDRESS


Basic generated by CPU location in a memory unit
Address Space Logical Address Space is set of all Physical Address is set of all physical
logical addresses generated by CPU in addresses mapped to the corresponding
reference to a program. logical addresses.
Visibility User can view the logical address of a User can never view physical address of
program. program.
Generation generated by the CPU Computed by MMU
Access The user can use the logical address The user can indirectly access physical
to access the physical address. address but not directly.
Editable Logical address can be change. Physical address will not change.
Also called virtual address. real address.

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

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.
• 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.

Translation of Two dimensional Logical Address to dimensional Physical Address.

Address generated by the CPU is divided into:


• 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 –
• 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.

Virtual Memory Works

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

Advantages of Virtual Memory


• The degree of Multiprogramming will be increased.
• User can run large application with less real RAM.
• There is no need to buy more memory RAMs.

Disadvantages of Virtual Memory


• The system becomes slower since swapping takes time.
• It takes more time in switching between applications.
• The user will have the lesser hard disk space for its use.

Page Replacement Algorithms

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

First In First Out (FIFO):

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

Following are some of the important features of Linux Operating System.


• Portable − Portability means software can works on different types of hardware in same way. Linux
kernel and application programs supports their installation on any kind of hardware platform.
• Open Source − Linux source code is freely available and it is community based development project.
Multiple teams work in collaboration to enhance the capability of Linux operating system and it is
continuously evolving.
• Multi-User − Linux is a multiuser system means multiple users can access system resources like
memory/ ram/ application programs at same time.
• Multiprogramming − Linux is a multiprogramming system means multiple applications can run at
same time.
• Hierarchical File System − Linux provides a standard file structure in which system files/ user files are
arranged.
• Shell − Linux provides a special interpreter program which can be used to execute commands of the
operating system. It can be used to do various types of operations, call application programs. etc.
• Security − Linux provides user security using authentication features like password protection/
controlled access to specific files/ encryption of data.

Components of Linux System

Linux Operating System has primarily three components


• Kernel − Kernel is the core part of Linux. It is responsible for all major activities of this operating
system. It consists of various modules and it interacts directly with the underlying hardware. Kernel
provides the required abstraction to hide low level hardware details to system or application
programs.
• System Library − System libraries are special functions or programs using which application programs
or system utilities accesses Kernel's features. These libraries implement most of the functionalities
of the operating system and do not requires kernel module's code access rights.
• System Utility − System Utility programs are responsible to do specialized, individual level tasks.

10
Unit - 4

Kernel Mode vs User Mode

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

The following illustration shows the architecture of a Linux system −

The architecture of a Linux System consists of the following layers −


• Hardware layer − Hardware consists of all peripheral devices (RAM/ HDD/ CPU etc).
• Kernel − It is the core component of Operating System, interacts directly with hardware, provides
low level services to upper layer components.
• Shell − An interface to kernel, hiding complexity of kernel's functions from users. The shell takes
commands from the user and executes kernel's functions.
• Utilities − Utility programs that provide the user most of the functionalities of an operating systems.

Linux Directory Structure (Linux File Hierarchy Structure)

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

Some other directories in the Linux system:


• /boot- It contains all the boot-related information files and folders such as conf, grub, etc.
• /dev – It is the location of the device files such as dev/sda1, dev/sda2, etc.
• /lib – It contains kernel modules and a shared library.
• /lost+found – It is used to find recovered bits of corrupted files.
• /media – It contains subdirectories where removal media devices inserted.
• /mnt – It contains temporary mount directories for mounting the file system.
• /proc – It is a virtual and pseudo-file system to contains info about the running processes with a
specific process ID or PID.
• /run – It stores volatile runtime data.
• /sbin – binary executable programs for an administrator.
• /srv – It contains server-specific and server-related files.
• /sys – It is a virtual filesystem for modern Linux distributions to store and allows modification of the
devices connected to the system.

Exploring directories and their usability:

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.

Linux Kernel File:


• /boot/vmlinux – The Linux kernel file.

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.

User Related Files:


• /usr/bin – It contains most of the executable files.
• /usr/bin/X11 – Symbolic link of /usr/bin.
• /usr/include – It contains standard include files used by C program.
• /usr/share – It contains architecture independent shareable text files.
• /usr/lib – It contains object files and libraries.
• /usr/sbin – It contains commands for Super User, for System Administration.

Version Information File:


• /version – It displays the Linux version information.

13
Unit - 4

System Configuration Files:


• /etc/bashrc – It is used by bash shell that contains system defaults and aliases.
• /etc/crontab – A shell script to run specified commands on a predefined time interval.
• /etc/exports – It contains information on the file system available on the network.
• /etc/fstab – Information of the Disk Drive and their mount point.
• /etc/group – It is a text file to define Information of Security Group.
• /etc/grub.conf – It is the grub bootloader configuration file.
• /etc/init.d – Service startup Script.
• /etc/lilo.conf – It contains lilo bootloader configuration file.
• /etc/hosts – Information of IP and corresponding hostnames.
• /etc/hosts.allow – It contains a list of hosts allowed accessing services on the local machine.
• /etc/host.deny – List of hosts denied to access services on the local machine.
• /etc/inittab – INIT process and their interaction at the various run level.
• /etc/issue – Allows editing the pre-login message.
• /etc/modules.conf – It contains the configuration files for the system modules.
• /etc/motd – It contains the message of the day.
• /etc/mtab – Currently mounted blocks information.
• /etc/passwd – It contains username, password of the system, users in a shadow file.
• /etc/printcap – It contains printer Information.
• /etc/profile – Bash shell defaults.
• /etc/profile.d – It contains other scripts like application scripts, executed after login.
• /etc/rc.d – It avoids script duplication.
• /etc/rc.d/init.d – Run Level Initialisation Script.
• /etc/resolv.conf – DNS being used by System.
• /etc/security – It contains the name of terminals where root login is possible.
• /etc/skel – Script that initiates new user home directory.
• /etc/termcap – An ASCII file that defines the behavior of different types of the terminal.
• /etc/X11 – Directory tree contains all the conf files for the X-window System.

Virtual and Pseudo Process Related Files:


• /proc/cpuinfo – CPU Information
• /proc/filesystems – It keeps the useful info about the processes that are running currently.
• /proc/interrupts – it keeps the information about the number of interrupts per IRQ.
• /proc/ioports – Contains all the Input and Output addresses used by devices on the server.
• /proc/meminfo – It reports the memory usage information.
• /proc/modules – Currently using kernel module.
• /proc/mount – Mounted File-system Information.
• /proc/stat – It displays the detailed statistics of the current system.
• /proc/swaps – It contains swap file information.

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.

To Learn about steps of installing (Linux OS) in Computer (Click here)

15

You might also like