0% found this document useful (0 votes)
454 views159 pages

OS Book 2025

The document provides an overview of operating systems, detailing their role as intermediaries between hardware and software, and outlining their main layers, types, and structures. It discusses various operating system structures such as simple, monolithic, micro-kernel, and layered structures, along with their advantages and disadvantages. Additionally, it describes different types of operating systems, including multiprocessor, multitasking, and client/server network OS, highlighting their functionalities and use cases.
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)
454 views159 pages

OS Book 2025

The document provides an overview of operating systems, detailing their role as intermediaries between hardware and software, and outlining their main layers, types, and structures. It discusses various operating system structures such as simple, monolithic, micro-kernel, and layered structures, along with their advantages and disadvantages. Additionally, it describes different types of operating systems, including multiprocessor, multitasking, and client/server network OS, highlighting their functionalities and use cases.
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
You are on page 1/ 159

Operating System

CHAPTER 1
BASICS CONCEPT

INTRODUCTION
The operating system is a program that can play a middle role between
hardware and software. An operating system (OS) exploits the hardware resources
of one or more processors to provide a set of services to system users. An operating
system serves as an interface between the program and various computer hardware
or software components. The operating system is made to control all of the
computer's resources and activities. It is an entirely integrated collection of
specialized applications that manage all of the computer's functions.

Main Layers in an Operating System: The software that acts as an


interface between various computer parts is referred to as layers in an operating
system. A clear benefit of layering is evident in an operating system. Each layer
may be designed independently and interacted with as needed. The following five-
layer model is often used in an Operating System:
• Kernel: It links a computer's hardware with application software. As a result, it
controls how applications in the RAM access memory. Additionally, it decides
when each program will execute and allots processing time and memory to each
application.
• Memory Management: It is in charge of restarting the computer's physical
memory between processes and managing programs that need more memory
than is physically accessible.

Page 1 of 159
Operating System

• Input/Output: This layer manages all physical interactions with other devices,
including the keyboard, printer, display, and disc drive. The I/O layer receives a
request if a higher layer needs access to a device.
• File Management System: It also goes by the name "file system." It is in
charge of planning and overseeing the data storage on long-term storage devices
including hard drives, and floppy disc drives.
• User Interface: It is referred to as the area where human and machine
interaction takes place. There are two different types of user interfaces: the icon-
based Graphical User Interface (GUI), which is used in Windows and Apple Mac
OS, and the text-based Command Line Interface (CLI), which is used in MS-DOS
and LINUX.

Different Types of Operating Systems


• Microsoft Windows: It is a form of an operating system that comes in 32 and
64-bit variants of Microsoft Windows. Microsoft was the one that created it. It
offers a Graphical User Interface (GUI), the ability to manage virtual memory,
multitasking features, and compatibility with a wide range of peripheral devices.
• UNIX: The most capable and well-liked operating system for multiple users and
tasks is Unix. It is a group of applications that serve as the user's interface with
the computer. Dennis Ritchie later contributed to Ken Thompson's original UNIX
code. Unix systems are built around a core kernel that manages the system and
the other processes. The following are the major features of UNIX:
• A hierarchy of files
• Independence from devices
• Multi-tasking
• Multi-user functionality
• Tools and utilities for creating tools
• The portability
• Integrated Networking
• Linux: One of the most widely used variations of Unix OS is the LINUX
Operating System. It is an open-source operating system that is freely available
online and whose source code is editable by anybody who uses it. Its
functionality list resembles Unix in many ways.

STRUCTURE OF THE OPERATING SYSTEM


The operating system structure refers to how the various components of an
operating system are organized and interconnected. There are several different
approaches to operating system structure, each with its advantages and
disadvantages.
An operating system has a complex structure, so we need a well-defined
structure to assist us in applying it to our unique requirements. Just as we break
down a big problem into smaller, easier-to-solve subproblems, designing an

Page 2 of 159
Operating System

operating system in parts is a simpler approach to do it. Each section is an


Operating System component. The approach of interconnecting and integrating
multiple operating system components into the kernel can be described as an
operating system structure. As mentioned below, various sorts of structures are
used to implement operating systems.
1. Simple Structure
2. Monolithic Structure
3. Layered Approach Structure
4. Micro-kernel Structure

Simple Structure: It is the simplest Operating System Structure and is not


well defined; It can only be used for small and limited systems. In this structure,
the interfaces and levels of functionality are well separated; hence programs can
access I/O routines which can cause unauthorized access to I/O routines. This
structure is implemented in the MS-DOS operating system: The MS-DOS operating
System is made up of various layers, each with its own set of functions. These
layers are the Application Program, System Program, MS-DOS device drivers, and
ROM BIOS device drivers.

Advantages of Simple Structure


• It is easy to develop because of the limited number of interfaces and layers.
• Offers good performance due to lesser layers between hardware and
applications.
• Minimal overhead, suitable for resource-constrained environments.
Disadvantages of Simple Structure
• If one user program fails, the entire operating system crashes.
• Limited functionality.
• Abstraction or data hiding is not present as layers are connected and
communicate.
• Layers can access the processes going in the Operating System, which can
lead to data modification and can cause the Operating System to crash.

Monolithic Structure: The Monolithic operating System in which


the kernel acts as a manager by managing all things like file management, memory

Page 3 of 159
Operating System

management, device management, and operational processes of the Operating


System.
The kernel is the heart of a computer operating system (OS). Kernel delivers
basic services to all other elements of the System. It serves as the primary interface
between the Operating System and the hardware.
In monolithic systems, kernels can directly access all the resources of the operating
system like physical hardware, exp Keyboard, Mouse, etc. The monolithic kernel is
another name for the monolithic operating system. Batch processing and time-
sharing maximize the usability of a processor by multiprogramming. The
monolithic kernel functions as a virtual machine by working on top of the
Operating System and controlling all hardware components. This is an outdated
operating system that was used in banks to accomplish minor activities such as
batch processing and time-sharing, which enables many people at various
terminals to access the Operating System. A Diagram of the Monolithic structure is
shown below:

Advantages of Monolithic structure:


• It is simple to design and implement because all operations are managed
by kernel only, and layering is not needed.
• As services such as memory management, file management, process scheduling,
etc., are implemented in the same address space, the execution of the
monolithic kernel is relatively fast as compared to normal systems. Using the
same address saves time for address allocation for new processes and makes it
faster.
• Simple design and implementation.

Disadvantages of Monolithic structure:


• If any service in the monolithic kernel fails, the entire System fails because, in
address space, the services are connected and affect each other.
• Lack of modularity makes maintenance and extensions difficult.
• It is not flexible, and to introduce a new service

Micro-kernel Structure: Micro-kernel structure designs the Operating


System by removing all non-essential components of the kernel. These non-

Page 4 of 159
Operating System

essential components of kernels are implemented as systems and user programs.


Hence these implemented systems are called Micro-Kernels. Each Micro-Kernel is
made independently and is isolated from other Micro-Kernels. So, this makes the
system more secure and reliable. If any Micro-Kernel fails, then the remaining
operating System remains untouched and works fine.

Advantages of Micro-kernel structure:


• It allows the operating system to be portable between platforms.
• Enhanced system stability and security.
• As each Micro-Kernel is isolated, it is safe and trustworthy.
• Because Micro-Kernels are smaller, they can be successfully tested.
• If any component or Micro-Kernel fails, the remaining operating System is
unaffected and continues to function normally.
Disadvantages of Micro-kernel structure:
• Increased inter-module communication reduces system performance.
• System is complex to construct.
• Complexity in managing user-space components.

Layered Structure: In this type of structure, OS is divided into layers or


levels. The hardware is on the bottom layer (layer 0), while the user interface is on
the top layer (layer N). These layers are arranged in a hierarchical way in which the
top-level layers use the functionalities of their lower-level levels. In this approach,
the functionalities of each layer are isolated, and abstraction is also available. In a
layered structure, debugging is easier as it is a hierarchical model, so all lower-level
layered is debugged, and then the upper layer is checked. So, all the lower layers
are already checked, and the current layer is to be checked only.
The following are some of the key characteristics of a layered operating
system structure:
• Each layer is responsible for a specific set of tasks. This makes it easier to
understand, develop, and maintain the operating system.
• Layers are typically arranged in a hierarchy. This means that each layer can only
use the services provided by the layers below it.

Page 5 of 159
Operating System

• Layers are independent of each other. This means that a change to one layer
should not affect the other layers.
Below is the Image illustrating the Layered structure in OS:

Advantages of Layered Structure


• A layered structure is highly modular, meaning that each layer is
responsible for a specific set of tasks. This makes it easier to understand,
develop, and maintain the operating system.
• Each layer has its functionalities, so work tasks are isolated, and
abstraction is present up to some level.
• Debugging is easier as lower layers are debugged, and then upper layers are
checked.
Disadvantages of Layered Structure
• In Layered Structure, layering causes degradation in performance.
• It takes careful planning to construct the layers since higher layers only
utilize the functions of lower layers.
• There can be some performance overhead associated with the
communication between layers. This is because each layer must pass data to
the layer above it.

TYPES OF OPERATING SYSTEM


The following are the different types of operating systems:

Multiprocessor OS: A multiprocessor operating system is an operating system


that uses multiple processors to improve performance. This operating system is
commonly found on computers with more than one CPU. Multiprocessor systems
improve system performance by allowing the execution of tasks on multiple
processors simultaneously. Overall reduces the time it takes to complete specific
tasks.

Page 6 of 159
Operating System

Advantages
• It allows the system to run multiple programs simultaneously.
• Beneficial for tasks that need to use all of the processor’s resources, such as
games, scientific calculations, and financial simulations.
Disadvantages: They require additional hardware, such as processors and
memory, making a system more expensive.

Multi-programming OS: The operating system that can run multiple


processes on a single processor is called a multiprogramming operating system.
Different programs want to get executed. So, these programs are kept in the ready
queue. And are assigned to the CPU one by one. If one process gets blocked then
other processes from the ready queue are assigned to the CPU. The aim of this is
optimal resource utilization and more CPU utilization. In the below figure, different
processes are there in RAM (main memory). Some processes are waiting for the
CPU, and process 2(which was previously executing) is now doing I/O operations.
So CPU shifted to execute process 1.

Distributed OS: A distributed operating system is an operating system that is


designed to operate on a network of computers. Distributed systems are usually
used to distribute software applications and data. Distributed systems are also
used to manage the resources of multiple computers. Users could be at different
sites. Multiple computers are connected via a single communication channel. Every
system has its processor and memory. Resources like disk, computer, CPU,
network interface, nodes, etc., are shared among different computers at different
locations. It increases data availability in the entire system.
Advantages
• It is more reliable as a failure of one system will not impact the other
computers or the overall system.
• All computers work independently.
• Resources are shared, so there is less cost overall.
• The system works at a higher speed as resources are shared
• The host system has less load.
• Computers can be easily added to the system.
Disadvantages
• Costly setup.
• If the server fails, then the whole system will fail.

Page 7 of 159
Operating System

• Complex software is used for such a system

Multitasking OS: Multi-tasking operating systems are designed to enable


multiple applications to run simultaneously. Multi-tasking operating systems allow
multiple users to work on the same document or application simultaneously.
For example, a user running antivirus software, searching the internet, and
playing a song simultaneously. Then the user is using a multitasking OS.

Time-sharing OS: A time-sharing operating system is an application that


provides a shared user interface with multiple users logged in simultaneously. It
allows multiple users to access the same resources, such as files and applications,
as long as they are logged in simultaneously. This operating system type is most
commonly used in businesses, especially those that involve many simultaneous
users. Time-sharing operating systems enable users to finish their jobs on a system
at once. The time-sharing OS is the latest advancement in the computer science
world; it is being accepted worldwide, also at an increasing rate.

Client/Server Network OS: Client/server network operating systems are


those networks that contain two types of nodes: servers and clients. The servers
host the applications or services for users while clients use these applications. In a
client/server system, both the server and client computers must have certain
software installed to connect securely over a network connection.
Client-server networks are a type of computer network in which two or more
computer systems are linked through a telecommunications network. Clients are
the computers that use the network to access services provided by the server.
Servers are the computers that provide the services to the network. Client/server
networks are commonly used in business and government applications.

Page 8 of 159
Operating System

Advantages
• Allows companies to scale their computing resources to handle increased
demand without having to buy new hardware.
• Client-server systems can be quickly reconfigured to meet the changing
needs of an organization.
• They are also more reliable and easier to maintain than dedicated server
systems.
• Lower operating cost.
• More reliable and easier to maintain than dedicated server systems
Disadvantages
• These OS need more sophisticated management and networking
technologies, longer startup times, and increased vulnerability to attack.
• Less secure than dedicated server systems.
• More challenging to scale than dedicated server systems.

Batch OS: There are different users, and each user prepares their work in a
standalone device, such as punch cards in batch operating systems and sends
them to a computer operator. The various systems split and distribute similar tasks
in batches to facilitate computing and faster responses. A single operator takes
similar jobs with similar needs and requirements and then groups them into
various batches. Similar kinds of jobs that share similar needs and requirements.
These types of operating systems are not used nowadays.

Page 9 of 159
Operating System

Advantages
• The overall time the system takes to execute all the programs will be
reduced.
• Less time to execute all programs.
• These operating systems are shared between multiple users.
• Suitable for small-scale businesses.
• It can work in offline mode also.
• It can give specific time to the computer, and when a computer is idle can
process the jobs.
Disadvantages
• Sometimes, manual interventions are required between two batches.
• The CPU utilization is low because the time taken in loading and unloading
batches is very high compared to execution time.
• Sometimes, jobs enter into an infinite loop due to some mistake.
• Meanwhile, if one job takes too much time, other jobs must wait.
Thus, the diverse types of operating systems (OS) serve as the backbone of
computer technology, each catering to different requirements and user
preferences.

OPERATING SYSTEM FUNCTIONS


The operating system (OS) is a crucial component of the computing
environment, serving as an intermediary between users and computer hardware.
Let's examine the functions of the OS in detail below.
File Management
Device Management
Process Management
Memory Management
Job Accounting

File Management: An operating system’s (OS) primary function is to manage


files and folders. Operating systems are responsible for managing the files on a
computer. This includes creating, opening, closing, and deleting files. The operating
system is also responsible for organizing the files on the disk.
The OS also handles file permissions, which dictate what actions a user can
take on a particular file or folder. For example, you may be able to read a file but
not edit or delete it. This prevents unauthorized users from accessing or tampering
with your files.
Tasks of Operating System
• Keeps track of the location and status of files.
• Allocating and deallocating resources.
• Decide which resource to assign to which file.

Page 10 of 159
Operating System

OS helps in:
• Creating a file: The operating system provides a graphical user interface or
command-line interface that allows users to create new files. In a graphical
user interface-
• You can right-click on a folder or desktop and select “New”
• Choose the type of file you want to create, such as a text file or a Microsoft
Word document. Alternatively, you can use a command-line interface and
type commands to create files.
• Editing a file: Once a file has been created, you can use various tools, such as
a word processor and applications, provided by the operating system, to edit it.
• Updating a file: The operating system provides the facility to edit the file and
also tracks changes made to the file and updates the file metadata accordingly.
• Deleting a file: The operating system allows you to delete the file you no longer
need. The OS moves the file to the recycle bin or trash folder, which can be
restored if necessary, or permanently deletes it from the storage device.

Device Management: Operating systems provide essential functions for


managing devices connected to a computer. These functions include allocating
memory, processing input and output requests, and managing storage devices.
These devices could be a keyboard, mouse, printer, or any other device you may
have connected.
An operating system will provide options for managing each device's
behaviour. For example, you can set up your keyboard to type in a specific
language or make the mouse move only one screen at a time. An operating system
can also install software and updates for your devices and manage their security
settings.
The operating system does the following tasks:
• Allocating and deallocating devices to different processes.
• Keeps records of all the devices attached to the computer.
• Decide which device to be allocated to which process and for how much
time.

Process Management: The operating system is responsible for managing the


processes on your computer. This includes starting and stopping programs,
allocating resources, and managing memory usage. The operating system ensures
that the programs running on your computer should be compatible. It’s also
responsible for enforcing program security, which helps to keep your computer safe
from potential attacks.
How do Operating systems manage all processes? Each process, called a
quantum, is given a certain amount of time to execute. Once a process has used its
quantum, the operating system interrupts it, providing another process with a
turn. This ensures that each process gets a fair share of the CPU time.
The operating system manages processes by doing the following tasks:
• Allocating and deallocating the resources.

Page 11 of 159
Operating System

• Allocates resources such that the system doesn’t run out of resources.
• Offering mechanisms for process synchronization.
• Helps in process communication(inter-communication).

Memory Management: One of the most critical functions of an operating


system is memory management. This is the process of keeping track of all the
different applications and processes running on your computer and all the data
they’re using.
This is especially important on computers with limited memory, as it
ensures that no application or process takes up too much space and slows down
the computer. The operating system can move data around and delete files to free
up space.
Operating systems perform the following tasks:
• Allocating/deallocating memory to store programs.
• Deciding the amount of memory that should be allocated to the program.
• Memory distribution while multiprocessing.
• Update the status in case memory is freed
• Keeps record of how much memory is used and how much is unused.
When a computer starts, the operating system loads itself into memory and
manages all the other running programs. It checks how much memory is used and
how much is available and makes sure that executing programs do not interfere
with each other.

Job Accounting: An operating system’s (OS) job accounting feature is a


powerful tool for tracking how your computer’s resources are being used. This
information can help you pinpoint and troubleshoot any performance issues and
identify unauthorized software installations.
Operating systems track which users and processes use how many
resources. This information can be used for various purposes, including monitoring
system usage, billing users for their resource use, and providing system
administrators with information about which users and processes are causing
problems.
The operating system does the following tasks:
• Keeps record of all the activities taking place on the system.
• Keeps record of information regarding resources, memory, errors, resources,
etc.
• Responsible for Program swapping (in and out) in memory
• Keeps track of memory usage and accordingly assigns memory
• Opening and closing and writing to peripheral devices.
• Creating a file system for organizing files and directories.

Page 12 of 159
Operating System

CHARACTERISTICS OF MODERN OS
An operating system is a fundamental piece of software that manages
computer hardware resources and provides services for running applications.
Modern operating systems have evolved significantly over the years and have a
plethora of features that enhance their functionality, security, and usability.
The characteristics of modern operating systems that make them reliable,
efficient, and user-friendly are given below.
Object-Oriented Design: An object-oriented operating system (OOOS) is an
operating system that is designed and built using the principles of object-oriented
programming (OOP).
In an OOOS, the operating system is viewed as a collection of objects, each
representing a different aspect of the system. For example, there may be objects for
processes, files, devices, and users. These objects encapsulate their respective data
and behaviour and interact with each other through well-defined interfaces.
Multitasking and Multithreading: Multitasking is the ability of an operating
system to run multiple programs or processes at the same time, allowing users to
switch between them seamlessly.
In a multitasking environment, the operating system allocates CPU time to
each program or process in small time slices, allowing each program to run for a
short period before switching to the next program. This gives the illusion of
multiple programs running simultaneously, even though only one program is
running at any given moment.
Multithreading, on the other hand, is the ability of a program to perform
multiple tasks or subtasks simultaneously within a single process. In a
multithreaded program, each thread can execute a separate set of instructions
simultaneously, allowing the program to perform multiple tasks concurrently. This
can improve program performance and responsiveness, particularly for applications
that require heavy processing or input/output operations.
The combination of multitasking and multithreading allows modern
operating systems to efficiently manage system resources and run multiple
programs or processes simultaneously. This enables users to perform multiple
tasks at once and allows for better utilization of system resources such as CPU
time and memory.
Symmetric Multiprocessing: Symmetric multiprocessing (SMP) is a type of
multiprocessing architecture in which two or more identical processors are
connected to a single shared main memory and run a single operating system. In
an SMP system, each processor can access any memory area and perform any
system task, such as running applications or managing input/output operations.
SMP systems are commonly used in servers, high-performance computing
clusters, and desktop computers with multiple processors. In these systems, the
workload can be divided among the available processors, allowing for improved
performance and faster execution of tasks.

Page 13 of 159
Operating System

Distributed Operating System: A distributed operating system (DOS) is an


operating system that runs on multiple independent computers and coordinates
their activities as a single system.
Unlike traditional operating systems, which are designed to run on a single
computer, a DOS is designed to support distributed computing, where multiple
computers work together to achieve a common goal.
DOS is typically used in environments where a large number of computers
need to work together to perform complex tasks. Examples include scientific
simulations, weather forecasting, and large-scale data processing. In a DOS, the
different computers that make up the system can communicate with each other
and share resources such as memory, storage, and processing power.
Traditional Unix System: The rapid growth of Unix is due to many factors, i.e.,
its portability to a wide range of machines, adaptability, simplicity, a wide range of
tasks it can perform, its multi-user and multitasking nature, and suitability for
networking.
Data security: Modern operating systems use security measures like encryption,
file permissions, and user authentication to protect the system and user data. They
also receive regular security updates and patches to address vulnerabilities.
Error handling: Modern operating systems check for errors in hardware,
software, and data. They display error messages and may even suggest solutions to
problems.
Multitasking: Modern operating systems can handle multiple applications or
processes running at the same time.
File management: Modern operating systems can create new files, place them in
specific locations, and help users find and share files.
Resource management: Modern operating systems manage a computer's
resources.

MICROKERNEL ARCHITECTURE
A microkernel is a type of operating system kernel that is designed to provide
only the most basic services required for an operating system to function, such as
memory management and process scheduling. Other services, such as device
drivers and file systems, are implemented as user-level processes that
communicate with the microkernel via message passing (IPC i.e. inter-process
communication). This design allows the operating system to be more modular and
flexible than traditional monolithic kernels, which implement all operating system
services in kernel space. Microkernel Example: MINX (mini-UNIX), QNX, etc.
Salient Features
1. Modularity: Microkernels are designed with a modular structure, separating
the core functionalities into small, independent components which is easier to
add remove or update without affecting the entire system.

Page 14 of 159
Operating System

2. Kernel Minimalism: By keeping the minimal, the trusted computing base


(TCB) is reduced, enhancing security and reliability.
3. Inter-Process Communication (IPC): Microkernels heavily rely on IPC
mechanisms, such as message passing, for communication between user-space
servers and microkernel
4. Scalability: Microkernels can be more scalable than monolithic kernels,
allowing for easier adaptation to different system requirements without
sacrificing performance.

The minimum functionalities included in the microkernel are:


• Memory management mechanisms like address spaces are included in the
microkernel. This also contains memory protection features.
• Processor scheduling mechanisms are also necessary in the microkernel.
This contains process and thread schedulers.
• Interprocess communication is important as it is needed to manage the
servers that run their own address spaces.

Performance of a Microkernel System: Providing services in a microkernel


system is much more expensive than in a normal monolithic system. The service is
obtained by sending an interprocess communication message to the server and
getting one in return. This means a context switch or a function call if the drivers
are implemented as processes or procedures respectively.
So, performance can be complicated in microkernel systems and may lead to
some problems. However, this issue is reduced in the modern microkernel systems
created such as L4 microkernel systems.

PROCESS MANAGEMENT
A process is a program in execution. For example, when we write a program
in C or C++ and compile it, the compiler creates binary code. The original code and
binary code are both programs. When we run the binary code, it becomes a
process. A process is an ‘active’ entity instead of a program, which is considered a
‘passive’ entity. A single program can create many processes when run multiple
times; for example, when we open a .exe or binary file multiple times, multiple
instances begin (multiple processes are created).

Page 15 of 159
Operating System

Process management is a key part of an operating system. It controls how


processes are carried out, and controls how your computer runs by handling the
active processes. This includes stopping processes, setting which processes should
get more attention, and many more. You can manage processes on your computer
too.
The OS is responsible for managing the start, stop, and scheduling of
processes, which are programs running on the system. The operating system uses
several methods to prevent deadlocks, facilitate inter-process communication, and
synchronize processes. Efficient resource allocation, conflict-free process execution,
and optimal system performance are all guaranteed by competent process
management. This essential component of an operating system enables the
execution of numerous applications at once, enhancing system utilization and
responsiveness.

Process Attributes/Characteristics: Let us see the attributes of a


process at a glance.
• Process ID: a unique identifier assigned by the operating system to each
process.
• Process State: there are a few possible states a process goes through during
execution.
• CPU registers: stores the details of the process when it is swapped in and out of
the CPU, just like the program counter.
• I/O status information: shows information like the device to which a process is
allotted and details of open files.
• CPU scheduling information: processes are scheduled and executed based on
priority.

• Accounting & Business information: information about the amount of CPU


used and time utilities like a job or process number, real-time utilized, etc.
• Memory management information: information about the value of base
registers and limit registers, segment tables, and pages.

Characteristics of a Process: A process has the following characteristics:

Page 16 of 159
Operating System

• Process State: A process can be in several states, some of them are ready,
suspend wait, and running.
• Process Control Block: The PCB is a data structure that contains information
related to a process. These blocks are stored in the process table.
• Resources: Processes request various types of resources such as files,
input/output devices, and network connections. The OS manages the allocation
of these resources.
• Priority: Each process has a scheduling priority. Higher-priority processes are
given preferential treatment and they receive more CPU time compared to lower-
priority processes.
• Execution Context: Each process has its execution context, which includes the
address of the next instruction to be executed, stack pointer, and register
values.

PROCESS STATES
These are the states in which a process might go during its execution. Let us
know more about them:

• New: a new process is created when a certain program is called up from


secondary memory and loaded to RAM.
• Ready: a process is said to be in a ready state if it is already loaded in the
primary memory/RAM for its execution.
• Running: here, the process is already executing.
• Paused (Waiting): at this stage, the process is believed to be waiting for CPU
or resource allocation.
• Blocked: at this stage, the process is waiting for some I/O operation to get
completed.
• Terminated: this specifies the time at which the process gets terminated by
the OS.

Page 17 of 159
Operating System

• Suspended: this state shows that the process is ready but it has not been
placed in the ready queue for execution.

OPERATIONS ON PROCESS
In an operating system, processes represent the execution of individual tasks
or programs. Process operations involve the creation, scheduling, execution, and
termination of processes. The OS allocates necessary resources, such as CPU time,
memory, and I/O devices, to ensure the seamless execution of processes. Process
operations in OS encompass vital aspects of process lifecycle management,
optimizing resource allocation, and facilitating concurrent and responsive
computing environments.

Process Operations: Process operations in an operating system involve several


key steps that manage the lifecycle of processes. The operations on process in OS
ensure efficient utilization of system resources, multitasking, and a responsive
computing environment. The primary process operations in OS include:
1. Process Creation
2. Process Scheduling
3. Context Switching
4. Process Execution
5. Inter-Process Communication (IPC)
6. Process Termination
7. Process Synchronization
8. Process State Management
9. Process Priority Management
10. Process Accounting and Monitoring

Creating: Process creation is a fundamental operation within an operating


system that involves the creation and initialization of a new process. The process
operation in OS is crucial for enabling multitasking, resource allocation, and
concurrent execution of tasks. The process creation operation in OS typically
follows a series of steps which are as follows:
1. Request: The process of creation begins with a request from a user or a system
component, such as an application or the operating system itself, to start a new
process.
2. Allocating Resources: The operating system allocates necessary resources for
the new process, including memory space, a unique process identifier (PID), a
process control block (PCB), and other essential data structures.
3. Loading Program Code: The program code and data associated with the
process are loaded into the allocated memory space.
4. Setting Up Execution Environment: The OS sets up the initial execution
environment for the process.

Page 18 of 159
Operating System

5. Initialization: Any initializations required for the process are performed at this
stage. This might involve initializing variables, setting default values, and
preparing the process for execution.
6. Process State: After the necessary setup, the new process is typically in a
"ready" or "waiting" state, indicating that it is prepared for execution but hasn't
started running yet.

Dispatching/Scheduling: It is a crucial operation within an operating


system that involves the selection of the next process to execute on the central
processing unit (CPU). This operation is a key component of process management
and is essential for efficient multitasking and resource allocation. The dispatching
operation encompasses the following key steps:
1. Process Selection: The dispatching operation selects a process from the pool
of ready-to-execute processes. The selection criteria may include factors such
as process priority, execution history, and the scheduling algorithm employed
by the OS.
2. Context Switching: Before executing the selected process, the operating
system performs a context switch. This involves saving the state of the
currently running process, including the program counter, CPU registers, and
other relevant information, into the process control block (PCB).
3. Loading New Process: Once the context switch is complete, the OS loads the
saved state of the selected process from its PCB. This includes restoring the
program counter and other CPU registers to the values they had when the
process was last pre-empted or voluntarily yielded to the CPU.
4. Execution: The CPU begins executing the instructions of the selected process.
The process advances through its program logic, utilizing system resources
such as memory, I/O devices, and external data.
5. Timer Interrupts and Pre-emption: During process execution, timer
interrupts are set at regular intervals. When a timer interrupt occurs, the
currently running process is pre-empted, and the CPU returns control to the
operating system.
6. Scheduling Algorithms: The dispatching operation relies on scheduling
algorithms that determine the order and duration of process execution.
7. Resource Allocation: The dispatching operation is responsible for allocating
CPU time to processes based on the scheduling algorithm and their priority.
This ensures that high-priority or time-sensitive tasks receive appropriate
attention.

Blocking: In an operating system, a blocking operation refers to a situation


where a process in OS is temporarily suspended or "blocked" from executing
further instructions until a specific event or condition occurs. This event typically
involves waiting for a particular resource or condition to become available before
the process can proceed. Blocking operations are common in scenarios where
processes need to interact with external resources, such as input/output (I/O)
devices, files, or other processes.

Page 19 of 159
Operating System

When a process operation in OS initiates a blocking operation, it enters a


state known as "blocked" or "waiting." The operating system removes the process
from the CPU's execution queue and places it in a waiting queue associated with
the resource it is waiting for. The process remains in this state until the resource
becomes available or the condition is satisfied.
Blocking operations is crucial for efficient resource management and
coordination among processes. They prevent processes from monopolizing system
resources while waiting for external events, enabling the operating system to
schedule other processes for execution. Common examples of blocking operations
include:

1. I/O Operations: When a process requests data from an I/O device (such as
reading data from a disk or receiving input from a keyboard), it may be blocked
until the requested data is ready.
2. Synchronization: Processes often wait for synchronization primitives like
semaphores or mutexes to achieve mutual exclusion or coordinate their
activities.
3. Inter-Process Communication: Processes waiting for messages or data from
other processes through mechanisms like message queues or pipes may enter a
blocked state.
4. Resource Allocation: Processes requesting system resources, such as memory
or network connections, may be blocked until the resources are allocated.

Pre-emption: Pre-emption in an operating system refers to the act of


temporarily interrupting the execution of a currently running process to allocate
the CPU to another process. This interruption is typically triggered by a higher-
priority process becoming available for execution or by the expiration of a time slice
assigned to the currently running process in a time-sharing environment. Key
aspects of pre-emption include:
1. Priority-Based Preemption: Processes with higher priorities are given
preference in execution. When a higher-priority process becomes available, the
OS may pre-empt the currently running process to allow the higher-priority
process to execute.
2. Time Sharing: In a time-sharing or multitasking environment, processes are
allocated small time slices (quantum) of CPU time. When a time slice expires,

Page 20 of 159
Operating System

the currently running process is pre-empted, and the OS selects the next
process to run.
3. Interrupt-Driven Preemption: Hardware or software interrupts can trigger
pre-emption. For example, an interrupt generated by a hardware device or a
system call request from a process may cause the OS to pre-empt the current
process and handle the interrupt.
4. Fairness and Responsiveness: Preemption ensures that no process is unfairly
blocked from accessing CPU time. It guarantees that even low-priority
processes get a chance to execute, preventing starvation.
5. Real-Time Systems: Preemption is crucial in real-time systems, where tasks
have strict timing requirements. If a higher-priority real-time task becomes
ready to run, the OS must pre-empt lower-priority tasks to ensure timely
execution.

Termination of Process: Termination of a process operation in an


operating system refers to the orderly and controlled cessation of a running
process's execution. Process termination occurs when a process has completed its
intended task, when it is no longer needed, or when an error or exception occurs.
This operation involves several steps to ensure proper cleanup and resource
reclamation:
1. Exit Status: When a process terminates, it typically returns an exit status or
code that indicates the outcome of its execution. This status provides
information about whether the process was completed successfully or
encountered an error.
2. Resource Deallocation: The OS releases the resources allocated to the process,
including memory, file handles, open sockets, and other system resources. This
prevents resource leaks and ensures efficient utilization of system components.
3. File Cleanup: If the process has opened files or created temporary files, the OS
ensures that these files are properly closed and removed, preventing data
corruption and freeing up storage space.
4. Parent Process Notification: In most cases, the parent process (the process
that created the terminating process) needs to be informed of the termination
and the exit status.
5. Process Control Block Update: The OS updates the process control block (PCB)
of the terminated process, marking it as "terminated" and removing it from the
list of active processes.
6. Reclamation of System Resources: The OS updates its data structures and
internal tables to reflect the availability of system resources that were used by
the terminated process.

CONCURRENT PROCESS
Concurrency in operating systems refers to the ability of an OS to manage
and execute multiple tasks or processes simultaneously. It allows multiple tasks to

Page 21 of 159
Operating System

overlap in execution, giving the appearance of parallelism even on single-core


processors. Concurrency is achieved through various techniques such as
multitasking, multithreading, and multiprocessing.
Multitasking involves the execution of multiple tasks by rapidly switching between
them. Each task gets a time slot, and the OS switches between them so quickly
that it seems as if they are running simultaneously.
Multithreading takes advantage of modern processors with multiple cores. It
allows different threads of a process to run on separate cores, enabling true
parallelism within a single process.
Multiprocessing goes a step further by distributing multiple processes across
multiple physical processors or cores, achieving parallel execution at a higher level.

Why Allow Concurrent Execution? The need for concurrent execution


arises from the desire to utilize computer resources efficiently. Here are some key
reasons why concurrent execution is essential:
• Resource Utilization: Concurrency ensures that the CPU, memory, and other
resources are used optimally. Without concurrency, a CPU might remain idle
while waiting for I/O operations to complete, leading to inefficient resource
utilization.
• Responsiveness: Concurrent systems are more responsive. Users can interact
with multiple applications simultaneously, and the OS can switch between them
quickly, providing a smoother user experience.
• Throughput: Concurrency increases the overall throughput of the system.
Multiple tasks can progress simultaneously, allowing more work to be done in a
given time frame.
• Real-Time Processing: Certain applications, such as multimedia playback and
gaming, require real-time processing. Concurrency ensures that these
applications can run without interruptions, delivering a seamless experience.

Principles of Concurrency in Operating Systems: To effectively


implement concurrency, OS designers adhere to several key principles:
• Process Isolation: Each process should have its own memory space and
resources to prevent interference between processes. This isolation is critical to
maintain system stability.
• Synchronization: Concurrency introduces the possibility of data races and
conflicts. Synchronization mechanisms like locks, semaphores, and mutexes are
used to coordinate access to shared resources and ensure data consistency.
• Deadlock Avoidance: OSs implement algorithms to detect and avoid deadlock
situations where processes are stuck waiting for resources indefinitely.
Deadlocks can halt the entire system.
• Fairness: The OS should allocate CPU time fairly among processes to prevent
any single process from monopolizing system resources.

Problems in Concurrency: While concurrency offers numerous benefits, it


also introduces a range of challenges and problems:

Page 22 of 159
Operating System

• Race Conditions: They occur when multiple threads or processes access shared
resources simultaneously without proper synchronization. In the absence of
synchronization mechanisms, race conditions can lead to unpredictable
behaviour and data corruption. This can result in data inconsistencies,
application crashes, or even security vulnerabilities if sensitive data is involved.
• Deadlocks: A deadlock arises when two or more processes or threads become
unable to progress as they are mutually waiting for resources that are currently
held by each other. This situation can bring the entire system to a standstill,
causing disruptions and frustration for users.
• Priority Inversion: Priority inversion occurs when a lower-priority task
temporarily holds a resource that a higher-priority task needs. This can lead to
delays in the execution of high-priority tasks, reducing system efficiency and
responsiveness.
• Resource Starvation: Resource starvation occurs when some processes are
unable to obtain the resources they need, leading to poor performance and
responsiveness for those processes. This can happen if the OS does not manage
resource allocation effectively or if certain processes monopolize resources.

Advantages of Concurrency: Concurrency in operating systems offers


several distinct advantages:
• Improved Performance: Concurrency significantly enhances system
performance by effectively utilizing available resources. With multiple tasks
running concurrently, the CPU, memory, and I/O devices are continuously
engaged, reducing idle time and maximizing overall throughput.
• Responsiveness: Concurrency ensures that users enjoy fast response times,
even when juggling multiple applications. The ability of the operating system to
swiftly switch between tasks gives the impression of seamless multitasking and
enhances the user experience.
• Scalability: Concurrency allows systems to scale horizontally by adding more
processors or cores, making it suitable for both single-core and multi-core
environments.
• Fault Tolerance: Concurrency contributes to fault tolerance, a critical aspect of
system reliability. In multiprocessor systems, if one processor encounters a
failure, the remaining processors can continue processing tasks. This
redundancy minimizes downtime and ensures uninterrupted system operation.

Limitations of Concurrency: Despite its advantages, concurrency has its


limitations:
• Complexity: Debugging and testing concurrent code is often more challenging
than sequential code. The potential for hard-to-reproduce bugs necessitates
careful design and thorough testing.
• Overhead: Synchronization mechanisms introduce overhead, which can slow
down the execution of individual tasks, especially in scenarios where
synchronization is excessive.
• Race Conditions: Dealing with race conditions requires careful consideration
during the design and rigorous testing to prevent data corruption and erratic
behaviour.

Page 23 of 159
Operating System

• Resource Management: Balancing resource usage to prevent both resource


starvation and excessive contention is a critical task. Careful resource
management is vital to maintain system stability.

PROCESS THREADS
In computers, a single process might have multiple functionalities running
parallelly where each functionality can be considered as a thread. Each thread has
its own set of registers and stack space. There can be multiple threads in a single
process having the same or different functionality. Threads in operating systems
are also termed lightweight processes.
Thread is a sequential flow of tasks within a process. Threads in an
operating system can be of the same or different types. Threads are used to
increase the performance of the applications.
Each thread has its own program counter, stack, and set of registers.
However, the threads of a single process might share the same code and
data/file. Threads are also termed lightweight processes as they share common
resources.
E.g.: While playing a movie on a device the audio and video are controlled by
different threads in the background.

Components of Thread: A thread has the following three components:


1. Program Counter
2. Register Set
3. Stack space

Types of Thread
User Level Thread: User-level threads are implemented and managed by the
user and the kernel is not aware of it. User-level threads are implemented using
user-level libraries and the OS does not recognize these threads. User-level threads
are faster to create and manage compared to kernel-level threads. If one user-level

Page 24 of 159
Operating System

thread performs a blocking operation then the entire process gets blocked.
E.g.: POSIX threads, Java threads, etc.
User Level Thread is a type of thread that is not created using system calls.
The kernel has no work in the management of user-level threads. User-level
threads can be easily implemented by the user. In case when user-level threads are
single-handed processes, kernel-level thread manages them. Let’s look at the
advantages and disadvantages of User-Level Thread.
Advantages of User-Level Threads
• Implementation of the User-Level Thread is easier than Kernel Level Thread.
• Context Switch Time is less in User Level Thread.
• User-Level Thread is more efficient than Kernel-Level Thread.
• Because of the presence of only Program Counter, Register Set, and Stack
Space, it has a simple representation.
Disadvantages of User-Level Threads
• There is a lack of coordination between Thread and Kernel.
• In case of a page fault, the whole process can be blocked.

Kernel-level Thread: Kernel-level threads are implemented and managed by


the OS. Kernel level threads are implemented using system calls and Kernel level
threads are recognized by the OS. Kernel-level threads are slower to create and
manage compared to user-level threads. Context switching in a kernel-level thread
is slower. Even if one kernel-level thread performs a blocking operation, it does not
affect other threads. E.g.: Window Solaris.

The above diagram shows the functioning of user-level threads in user space
and kernel-level threads in kernel space.
A kernel-level thread is a type of thread that can recognize the Operating
system easily. Kernel Level Threads has its thread table which it keeps track of the
system. The operating System Kernel helps in managing threads. Kernel Threads
have somehow longer context switching time. Kernel helps in the management of
threads.

Page 25 of 159
Operating System

Advantages of Kernel-Level Threads


• It has up-to-date information on all threads.
• Applications that block frequency are to be handled by the Kernel-Level Threads.
• Whenever any process requires more time to process, Kernel-Level Thread
provides more time to it.
Disadvantages of Kernel-Level threads
• Kernel-Level Thread is slower than User-Level Thread.
• Implementation of this type of thread is a little more complex than a user-level
thread.

Advantages of Threading
• Threads improve the overall performance of a program.
• Threads increase the responsiveness of the program
• Context Switching time in threads is faster.
• Threads share the same memory and resources within a process.
• Communication is faster in threads.
• Threads provide concurrency within a process.
• Enhanced throughput of the system.
• Since different threads can run parallelly, threading enables the utilization
of the multiprocessor architecture to a greater extent and increases
efficiency.

Life Cycle of Thread

1. Creation: The first stage in the lifecycle of a thread is its creation. In most
programming languages and environments, threads are created by
instantiating a thread object or invoking a thread creation function. During
creation, you specify the code or function that the thread will execute.
2. Ready/Runnable: After a thread is created, it enters the "ready" or
"runnable" state. In this state, the thread is ready to run, but the operating
system scheduler has not yet selected it to execute on the CPU. Threads in the
ready state are typically waiting for the scheduler to allocate CPU time to them.

Page 26 of 159
Operating System

3. Running: When the scheduler selects a thread from the pool of ready threads
and allocates CPU time to it, the thread enters the "running" state. In this
state, the thread's code is being executed on the CPU. A running thread will
continue to execute until it either voluntarily yields the CPU (e.g., through sleep
or wait operations) or is pre-empted by a higher-priority thread.
4. Blocked/Waiting: Threads can enter the "blocked" or "waiting" state when
they are waiting for some event to occur, such as I/O operations,
synchronization primitives (e.g., locks or semaphores), or signals from other
threads. When a thread is blocked, it is not eligible to run until the event it is
waiting for occurs.
5. Termination: Threads can terminate either voluntarily or involuntarily.
Voluntary termination occurs when a thread completes its execution or
explicitly calls a termination function. Involuntary termination can happen due
to errors (e.g., segmentation faults) or signals received from the operating
system.
6. Dead: Once a thread has terminated, it enters the "dead" state. In this state,
the thread's resources (such as memory and handles) are deallocated, and it no
longer exists as an active entity in the system. Dead threads cannot be
restarted or resumed.

MULTITHREADING
The term multithreading means a process and a thread. Process means a
program that is being executed. Processes are further divided into independent
units also known as threads, also known as collections of threads. It is a process
that is small and lightweight residing inside a process.

Multithreading divides the task of application into separate individual


threads. The same process or tasks in multithreading can be done by several
threads or it can be said that more than one thread is used for performing the
tasks in multithreading.
It has a segment that divides the code into a small set of lightweight tasks
and gives less load to CPU memory.

Page 27 of 159
Operating System

For example, client 1, client 2, and client 3 in the above example are
accessing the web server without having to wait for other tasks to be completed.
The threads in this are divided into user-level and kernel-level threads. The
user-level thread is used for handling an independent form of the kernel-level
thread without any support from the kernel. On the other hand, the kernel-level
threads are directly managed by the operating system.
Examples of Multithreading Operating Systems: Multithreading is widely
used by applications. Some of the applications are processing transactions
like online bank transfers, recharge, etc.
For instance, in the banking system, many users perform day-to-day
activities using bank servers like transfers, payments, deposits, `opening a new
account, etc. All these activities are performed instantly without having to wait for
another user to finish.
In this, all the activities get executed simultaneously as and when they arise.
This is where multithreading comes into the picture, wherein several threads
perform different activities without interfering with others.

Advantages:
• Multithreading allows the CPU to execute multiple tasks simultaneously, which
can boost performance.
• Multithreading reduces the amount of time that is spent waiting for a task to
finish.
• Multithreading can help to improve the scalability of a program.
• Interactive applications may allow a program to continue running even if part of
it is blocked or is performing a lengthy operation, thereby increasing
responsiveness.

Disadvantages of multi-threading
• Multithreading can be complex and challenging to implement.
• Multithreading can increase the complexity of a program.
• Multithreading can be error-prone.
• Programmers must carefully design their code to utilize multithreading
capabilities without introducing unwanted delays or fragmentation into their
programs’ execution.

Process Vs. Thread: Process simply means any program in execution while
the thread is a segment of a process. The main differences between process and
thread are mentioned below:
Process Thread
Processes use more resources and
Threads share resources and hence
hence they are termed as heavyweight
they are termed lightweight processes.
processes.
Creation and termination times of Creation and termination times of
processes are slower. threads are faster compared to

Page 28 of 159
Operating System

processes.
Processes have their code and Threads share code and data/files
data/file. within a process.
Communication between processes is Communication between threads is
slower. faster.
Context Switching in processes is
Context switching in threads is faster.
slower.
Threads, on the other hand, are
Processes are independent of each
interdependent. (i.e. they can read,
other.
write or change another thread’s data)
E.g.: Opening two tabs in the same
E.g.: Opening two different browsers.
browser.

MICROKERNELS
A kernel is the most important part of an operating system. It manages the
resources of the system and also acts as an interface between hardware and the
computer application. A microkernel is one type of kernel. It manages all system
resources.
Microkernel in an operating system is one of the kernel’s classifications. In
the microkernel, the user service, as well as the kernel service, are all implemented
on different kernel spaces.
In the user address space, all the user services are kept while in the kernel
address space, all the kernel services are available. Thus, by doing this, the size of
the kernel as well as the size of an operating system is reduced.
It is used for communicating between applications or client programs and
services that are running in the address space of the user are established by
message passing, thereby reducing the speed of the microkernel. It also provides
minimal service of memory management and process.
Since the user service and kernel service are isolated from each other, the
operating system remains unaffected if any user service is failed as it does not
affect the kernel service. It can be extended if any new service is to be added as
they are added to the user space and no modification is needed in the kernel space.
It is secure, portable, and reliable.

Architecture of Microkernel OS: A kernel is the core part of an operating


system which means that all the important services are handled by it. Because of
this, in the architecture of the microkernel, the important services reside within the
kernel and the rest of the services reside within the system’s application program.
The important services for which the microkernel is responsible are:
• Inter-process communication
• Scheduling of CPU
• Memory management

Page 29 of 159
Operating System

Inter-process communication: It refers to how processes interact with each other.


A process has many threads. Threads of any process can interact with each other
in kernel space. Messages are sent and received using ports across threads.

Scheduling of CPU: It refers to which process will be executed next. All the
processes reside in a queue and are executed one at a time. There are levels of
priority in every process and the process that has the highest process is executed
first. It helps in the optimization and utilization of the CPU to the maximum by
utilizing the resources efficiently. It minimizes the waiting time, response, and
turnaround times.
Memory management: It is the process of allocating space in memory for
processes. Virtual memory is also created if the process has a size bigger than that
of the actual memory by which the memory is divided into portions and stored. All
the processes wait in memory before CPU execution.

Components of Microkernel Operating System: A microkernel contains


only the basic functions of the system. A component in the microkernel is included
if and only if putting the component outside disrupts the operation of the system.
For non-essential components, user mode should be used. Following is some of the
functionalities of components of the microkernel:
• Processors, as well as thread schedulers, are included. The scheduling
algorithm of the processor is also required in the microkernel.
• Address space and other memory management services are incorporated into
the microkernel along with its security.
• For managing servers that execute their own address space, Inter-process
communication is used.

Example of Microkernel Operating System


Here are a few examples of microkernel operating systems:
• Helen OS
• Minix
• Horizon
• The L4 microkernel family

Page 30 of 159
Operating System

• Zircon

Advantages of Microkernel Operating System


• These are secure since a few parts are added, the ones that might change
the functionality of the system.
• Microkernels are modular, which means, various modules in them can be
swapped, modified, and reloaded without affecting the kernel.
• Better performance since the architecture is compact and isolated.
• It is scalable and hence more systems can be introduced without disturbing
each other.
• It adds new features without even recompiling.
• The interface of the microkernel helps in enforcing the modular structure.

Disadvantages of Microkernel Operating System


• Context switch or function call is needed while implementing drivers as
procedures.
• Providing services is more costly in microkernel systems as compared to
traditional monolithic systems.
• The performance might be indifferent and can cause some issues.

CPU SCHEDULING
CPU scheduling is a process that allows one process to use the CPU while
the execution of another process is on hold (in a waiting state) due to the
unavailability of any resource like I/O etc, thereby making full use of the CPU. CPU
scheduling aims to make the system efficient, fast, and fair.
Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried out by
the short-term scheduler (or CPU scheduler). The scheduler selects from among the
processes in memory that are ready to execute and allocates the CPU to one of
them. There is essential 4 conditions under which CPU scheduling decisions are
taken:
1. If a process is making the switch between the running state to the waiting state
(could be for an I/O request, or invocation of wait () for terminating one of its
child processes)
2. If a process is making the switch from the running state to the ready state (on
the occurrence of an interrupt, for example)
3. If a process is making the switch between waiting and ready state (e.g. when
its I/O request completes)
4. If a process terminates upon completion of execution.
So, in the case of conditions 1 and 4, the CPU does not have a choice of
scheduling, if a process exists in the ready queue the CPU's response to this would
be to select it for execution. In cases 2 and 3, the CPU has a choice of selecting a
particular process for executing next. There are mainly two types of CPU
scheduling:

Page 31 of 159
Operating System

Non-Preemptive Scheduling: In the case of non-pre-emptive scheduling, new


processes are executed only after the current process has completed its execution.
The process holds the resources of the CPU (CPU time) till its state changes to
terminated or is pushed to the process waiting state. If a process is currently being
executed by the CPU, it is not interrupted till it is completed.
Once the process has completed its execution, the processer picks the next
process from the ready queue (the queue in which all processes that are ready for
execution are stored).

For Example: In the image above, we can see that all the processes were executed
in the order in which they appeared, and none of the processes were interrupted by
another, making this a non-preemptive, FCFS (First Come, First Served) CPU
scheduling algorithm. P2 was the first process to arrive (arrived at time = 0) and
was hence executed first. Let's ignore the third column for a moment, we'll get to
that soon. Process P3 arrived next (at time = 1) and was executed after the previous
process - P2 was done executing, and so on.
Some examples of non-preemptive scheduling algorithms are - Shortest Job
First (SJF, non-preemptive), and Priority scheduling (non-preemptive).

Preemptive Scheduling: Preemptive scheduling takes into consideration the


fact that some processes could have a higher priority and hence must be executed
before the processes that have a lower priority.
In preemptive scheduling, the CPU resource is allocated to a process for only
a limited period and then those resources are taken back and assigned to another
process (the next in execution). If the process has yet to complete its execution, it is
placed back in the ready state, where it will remain till it gets a chance to execute
once again.
So, when we take a look at the conditions under which CPU
scheduling decisions are taken based on which CPU provides its resources to
processes, we can see that there isn't a choice in making a decision when it comes
to conditions 1 and 4. If we have a process in the ready queue, we must select it for
execution.

Page 32 of 159
Operating System

However, we do have a choice in conditions 2 and 3. If we opt to choose


scheduling only if a process terminates (condition 4) or if the current process
execution is waiting for I/O (condition 1) then we can say that our scheduling
is non-preemptive, however, if we make scheduling decisions in other conditions as
well, we can say that our scheduling process is preemptive.

SCHEDULERS
A scheduler is a software that helps schedule the processes in an operating
system. It helps to keep all computer resources busy and allows multiple users to
share system resources effectively. Let’s go through different schedulers in an
operating system.

1. Long-term schedulers: The processes that are created are in the NEW state.
The programs are admitted to the RAM for execution. So, before execution, the
processes are put in the READY queue. So, do they get into the ready queue
themselves? Here comes the role of long-term schedulers (LTS). It is also called
a job scheduler. These schedulers select processes from secondary memory and
put them into the ready queue. LTS runs less frequently. The main aim of LTS
is to maintain the degree of multiprogramming. Multiprogramming means
executing multiple programs by a single processor. But not all processes
simultaneously. It means if one process is not executing for some reason, then
another process will get a chance to get executed. An optimal level of
multiprogramming means
The average rate of process = the average departure rate of processes getting
executed and leaving the system.

2. Short-term schedulers: It is also called a CPU scheduler. When the


processes are in the ready queue, they are prepared to get executed. So the
short-term schedulers select one process from the ready queue, put it in the
running queue, and allocate a processor (CPU) to it. They are also known as the
dispatcher who decides which process will be executed next. They are faster
than long-term schedulers. The performance of the system depends on the
choice of Short-term schedulers. If it selects the processes having high burst
time, then, in that case, other processes in the waiting queue will keep on
waiting in the ready queue. This situation is called starvation.

Page 33 of 159
Operating System

3. Medium-term schedulers: When the process is assigned CPU and the


program execution starts, the program execution is sometimes suspended. The
reason could be an I/O request or some high-priority process. In this case,
suspended processes cannot make any progress towards completion. So the
process has to be removed from the memory and make space for other
processes. The suspended process is moved back to the secondary storage. For
example, suppose process 1 was executing, but it got suspended for some
reason, so process 1 is swapped out, and process 2 is swapped in. This means
swapping is taking place here. For doing swapping, we have a medium-term
scheduler.

SCHEDULING METHODOLOGY
In different environments, different scheduling methodologies are needed.
This situation arises because different application areas (and different kinds of
operating systems) have different goals. In other words, what the scheduler should
optimize for is not the same in all systems. Three environments are
1. Batch.
2. Interactive.
3. Real time.
Batch systems are still in widespread use in the business world for doing
payroll, inventory, accounts receivable, accounts payable, interest calculation (at
banks), claims processing (at insurance companies), and other periodic tasks. In
batch systems, there are no users impatiently waiting at their terminals for a quick
response to a short request. Consequently, non-preemptive algorithms, or
preemptive algorithms with long periods for each process, are often acceptable.
This approach reduces process switches and thus improves performance. The
batch algorithms are fairly general and often applicable to other situations as well,
which makes them worth studying, even for people not involved in corporate
mainframe computing.
In an environment with interactive users, preemption is essential to keep
one process from hogging the CPU and denying service to the others. Even if no
process intentionally ran forever, one process might shut out all the others
indefinitely due to a program bug. Preemption is needed to prevent this behaviour.
Servers also fall into this category, since they normally serve multiple (remote)
users, all of whom are in a big hurry.
In systems with real-time constraints, preemption is, oddly enough,
sometimes not needed because the processes know that they may not run for long
periods and usually do their work and block quickly. The difference with interactive

Page 34 of 159
Operating System

systems is that real-time systems run only programs that are intended to further
the application at hand. Interactive systems are general purpose and may run
arbitrary programs that are not cooperative or even malicious.

CPU SCHEDULING ALGORITHMS


There are different processes, and every process wants to get executed, but
all cannot be executed simultaneously. So that is why we need Scheduling. CPU
does this Scheduling. That’s why we call it CPU scheduling. CPU Scheduling
Algorithm is an essential part of any operating system. Various algorithms can be
used, each with advantages and disadvantages.
The CPU scheduling algorithm is used to schedule process execution by
determining which process should be removed from execution and which process
should be executed. In the end, the main goal is to engage the CPU all the time,
which means the CPU should not be idle.
Types of Scheduling Algorithms
• Preemptive process- These processes are based on the process priority. If a low-
priority process is executing and the higher-priority process enters, then the low-
priority process is preempted (stop running) and executes the high-priority
process first.
• Non-preemptive process- In these algorithms, if any process is assigned the
CPU, it will execute entirely or be released in case of context switching or
termination. There is no process priority in this.

Important CPU Scheduling Terminologies: Let's now discuss some


important terminologies that are relevant to CPU scheduling.
1. Arrival time: Arrival time (AT) is the time at which a process arrives at
the ready queue.
2. Burst Time: As you may have seen, the third column is 'burst time', it is the
time required by the CPU to complete the execution of a process, or the amount
of time required for the execution of a process. It is also sometimes called
the execution time or running time.
3. Completion Time: As the name suggests, completion time is the time when a
process completes its execution. It is not to be confused with burst time.
4. Turn-Around Time: Also written as TAT, turn-around time is simply the
difference between completion time and arrival time (Completion time - arrival
time).
5. Waiting Time: A process's Waiting time (WT) is the difference between
turnaround time and burst time (TAT - BT), i.e., the amount of time a process
waits to get CPU resources in the ready queue.
6. Response Time: The response time (RT) of a process is the time after which
any process gets CPU resources allocated after entering the ready queue.

Page 35 of 159
Operating System

FCFS
The full form of FCFS Scheduling is First Come First Serve Scheduling.
FCFS Scheduling algorithm automatically executes the queued processes
and requests in the order of their arrival. It allocates the job that first arrived in the
queue to the CPU, then allocates the second one, and so on. FCFS is the simplest
and easiest CPU scheduling algorithm, managed with a FIFO queue. FIFO stands
for First In First Out. The FCFS scheduling algorithm places the arriving
processes/jobs at the very end of the queue. So, the processes that request the
CPU first get the allocation from the CPU first. As any process enters the FIFO
queue, its Process Control Block (PCB) gets linked with the queue’s tail. As the CPU
becomes free, the process at the very beginning gets assigned to it. Even if the CPU
starts working on a longer job, many shorter ones have to wait after it. The FCFS
scheduling algorithm works in most of the batches of operating systems.
Examples of FCFS scheduling: Buying a movie ticket at the counter. This
algorithm serves people in the queue order. The first person in line buys a ticket,
then the next person, and so on. This process will continue until the last person in
line has purchased a ticket. This method mimics the CPU process.

Advantages of FCFS:
• Simplicity. Orders are fulfilled in order, simplifying scheduling and processing.
Orders are simply performed in chronological sequence.
• User friendly. Order scheduling and code writing are straightforward for team
members. Easy scheduling saves time and labour. It’s a foolproof technique that
also reduces errors.
• Easy to implement. FCFS's simplicity makes it easier to integrate into existing
systems. FCFS order scheduling can be deployed quickly and inexpensively into
any scheduling system your company uses. FCFS can be used soon after its
implementation.

Limitation of FCFS:
• Long waiting time. FCFS processes orders in order since it is non-preemptive.
This means a business order can start processing once the previous order has
been completed. A CPU-allocated process will never release it until it finishes. If
the initial order has a long burst time, orders following it must wait for
fulfilment, regardless of their burst times.
• Lower device usage. Simple FCFS is inefficient. Longer wait periods accompany
this. If the CPU is busy processing a long order, all other orders lie idle, causing
a backup. FCFS is particularly wasteful because the CPU can only process one
order at a time.
• CPU over I/O. FCFS emphasizes CPU over I/O. The algorithm is more CPU-
friendly than I/O-friendly. This may dissuade I/O system users.
Example:
Process Burst Time
P1 24

Page 36 of 159
Operating System

P2 3
P3 3
If the processes arrive in the order P1, P2, and P3, and are served in FCFS
order, we get the result shown in the following Gantt chart:

P1 P2 P3
0 24 27 30
The waiting time is 0 milliseconds for process P1, 24 milliseconds for process
P2, and 27 milliseconds for process P3. Thus, the average waiting time is (0 + 24 +
27)/3 = 17 milliseconds. If the processes arrive in the order P2, P3, and P1,
however, the results will be as shown in the following Gantt chart:

P2 P3 P1
0 3 6 30
The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds. This
reduction is substantial. Thus, the average waiting time under an FCFS policy is
generally not minimal and may vary substantially if the process's CPU burst times
vary greatly.

SJF
Shortest Job First (SJF) algorithm is also known as Shortest Job Next
(SJN) or Shortest Process Next (SPN). It is a CPU processes scheduling algorithm
that sorts and executes the process with the smallest execution time first, and then
the subsequent processes with the increased execution time. Both preemptive and
non-preemptive scheduling strategies are possible in the SJF scheduling algorithm.
In SJF, there is a significant amount of reduction in the average waiting time for
other processes that are waiting to be executed.
However, it can be quite challenging to estimate the burst time required for a
process, making it difficult to apply this technique to the operating system
scheduling process.

Page 37 of 159
Operating System

The burst time for a process can only be approximated or predicted. Our
approximations must be correct to get the most out of the SJF algorithm.
Numerous methods can be used to predict a process's CPU burst time.
There are two types of SJF methods:
• Non-Preemptive SJF
• Preemptive SJF
In non-preemptive scheduling, once the CPU cycle is allocated to the
process, the process holds it till it reaches a waiting state or is terminated.
In Preemptive SJF Scheduling, jobs are put into the ready queue as they
come. A process with the shortest burst time begins execution. If a process with
even a shorter burst time arrives, the current process is removed or preempted
from execution, and the shorter job is allocated a CPU cycle.

Advantages of SJF: These are some of the Advantages of the SJF algorithm:
• Shortest Job First (SJF) has a shorter average waiting time as compared to the
First Come First Serve (FCFS) algorithm.
• SJF can be applied to long-term scheduling.
• SJF is ideal for jobs that run in batches and whose run times are known.
• SJF is probably the best concerning the average turnaround time of a process.

Disadvantages of SJF: These are some of the Disadvantages of the SJF


algorithm:
• The Shortest Job First algorithm may result in a starvation problem with
extremely long turnaround times.
• In SJF, job burst time must be predetermined, although it might be difficult to
predict it.
• As we are unable to estimate the duration of the upcoming CPU process burst
time, we cannot utilize SJF for short-term CPU scheduling.

RR
The Round-robin scheduling algorithm is a kind of preemptive first-come,
first-served CPU Scheduling algorithm in which each process in the ready state
gets the CPU for a fixed time cyclically (turn by turn). It is the oldest scheduling
algorithm and is mainly used for multitasking.
The round-robin scheduling algorithm is one of the CPU scheduling
algorithms in which every process gets a fixed amount of time to execute.
In this algorithm, every process gets executed cyclically. This means that
processes that have their burst time remaining after the expiration of the time
quantum are sent back to the ready state and wait for their next turn to complete
the execution until it terminates. This processing is done in FIFO order, suggesting
that processes are executed on a first-come, first-served basis.

Working of Round Robin Algorithm:


1. All the processes are added to the ready queue.

Page 38 of 159
Operating System

2. At first, the burst time of every process is compared to the time quantum of the
CPU.
3. If the burst time of the process is less than or equal to the time quantum in the
round-robin scheduling algorithm, the process is executed to its burst time.
4. If the burst time of the process is greater than the time quantum, the process is
executed up to the time quantum (TQ).
5. When the time quantum expires, it checks if the process is executed completely
or not.
6. On completion, the process terminates. Otherwise, it goes back again to
the ready state.

Advantages
1. This round-robin algorithm offers starvation-free execution of processes.
2. Each process gets equal priority and fair allocation of CPU.
3. Round Robin scheduling algorithm enables the Context switching method to
save the states of preempted processes.
4. It is easily implementable on the system because round-robin scheduling in
OS doesn’t depend upon burst time.

Disadvantages
1. The waiting and response times are higher due to the short time slot.
2. Lower time quantum results in higher context switching.
3. We cannot set any special priority for the processes.

Example: Consider the following set of processes that arrive at time 0, with the
length of the CPU burst given in milliseconds:

Process Burst Time

P1 24

P2 3

P3 3

If we use a time quantum of 4 milliseconds, then process P 1 gets the first 4


milliseconds. Since it requires another 20 milliseconds, it is preempted after the
first-time quantum, and the CPU is given to the next process in the queue, process
P2. Since process P2 does not need 4 milliseconds, it quits before its time quantum
expires. The CPU is then given to the next process, process P 3. Once each process
has received 1 time quantum, the CPU is returned to process P1 for an additional
time quantum. The resulting RR schedule is

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

The average waiting time is 17/3 = 5.66 milliseconds. In the RR scheduling


algorithm, no process is allocated the CPU for more than 1 time quantum in a row
(unless it is the only runnable process). If a process's CPU burst exceeds 1 time

Page 39 of 159
Operating System

quantum, that process is pre-empted and is put back in the ready queue. The RR
scheduling algorithm is thus pre-emptive.

PRIORITY SCHEDULING
Priority scheduling in OS is the scheduling algorithm that schedules
processes according to the priority assigned to each process. Higher-priority
processes are executed before lower-priority processes.
In priority scheduling in OS, processes are executed based on their priority.
The jobs/processes with higher priority are executed first. Naturally, you might
want to know how the priority of processes is decided. Priority of processes depends
on some factors such as:
• Time limit
• Memory requirements of the process
• Ratio of average I/O to average CPU burst time
There can be more factors based on which the priority of a process/job is
determined. This priority is assigned to the processes by the scheduler.
These priorities of processes are represented as simple integers in a fixed range
such as 0 to 7, or maybe 0 to 4095. These numbers depend on the type of system.

Types of Priority Scheduling: There are two types of priority scheduling


algorithms in OS:
Non-Preemptive Scheduling: In this type of scheduling, if during the execution
of a process, another process with a higher priority arrives for execution, even then
the currently executing process will not be disturbed. The newly arrived high-
priority process will be put in next for execution since it has higher priority than
the processes that are waiting for execution. All the other processes will remain in
the waiting queue to be processed. Once the execution of the current process is
done, the high-priority process will be given the CPU for execution.
Preemptive Scheduling: `Preemptive Scheduling as opposed to non-preemptive
scheduling will preempt (stop and store the currently executing process) the
currently running process if a higher priority process enters the waiting state for
execution and will execute the higher priority process first and then resume
executing the previous process.

Advantages:
• High-priority processes do not have to wait for their chance to be executed due
to the current running process.
• We can define the relative importance/priority of processes.
• The applications in which the requirements of time and resources fluctuate are
useful.

Disadvantages:
• Since we only execute high-priority processes, this can lead to starvation of the
processes that have a low priority. Starvation is the phenomenon in which a

Page 40 of 159
Operating System

process gets infinitely postponed because the resources that are required by the
process are never allocated to it, since other processes are executed before it.
You can research more about starvation on Google.
• If the system eventually crashes, all of the processes that have low priority will
get lost since they are stored in the RAM.
Example: As an example, consider the following set of processes, assumed to have
arrived at time 0, in the order P1, P2, • •, P5, with the length of the CPU burst given
in milliseconds:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Using priority scheduling, we would schedule these processes according to
the following Gantt chart:

P2 P5 P1 P3 P4
0 1 6 16 18 19
The average waiting time is 8.2 milliseconds.
Priorities can be defined either internally or externally. Internally defined
priorities use some measurable quantity or quantities to compute the priority of a
process. For example, time limits, memory requirements, the number of open files,
and the ratio of average I/O burst to average CPU burst have been used in
computing priorities. External priorities are set by criteria outside the operating
system, such as the importance of the process, the type and amount of funds being
paid for computer use, the department sponsoring the work, and other, often
political, factors.

SRTF SCHEDULING ALGORITHM


The Pre-emptive version of Shortest Job First (SJF) scheduling is known as
Shortest Remaining Time First (SRTF). With the SRTF algorithm's help, the process
with the smallest amount of time remaining until completion is selected first to
execute. So basically, in SRTF, the processes are scheduled according to the
shortest remaining time.
However, the SRTF algorithm involves more overheads than the Shortest job
first (SJF)scheduling, because SRTF OS is frequently required to monitor the CPU
time of the jobs in the READY queue and perform context switching.
In the SRTF scheduling algorithm, the execution of any process can be
stopped after a certain amount of time. On arrival of every process, the short-term
scheduler schedules those processes from the list of available processes & running
processes that have the least remaining burst time.

Page 41 of 159
Operating System

After all the processes are available in the ready queue, No-preemption will be done
and the algorithm will work the same as SJF scheduling. In the Process Control
Block, the context of the process is saved, when the process is removed from the
execution, and when the next process is scheduled. The PCB is accessed on the
next execution of this process.
Advantages of SRTF: The main advantage of the SRTF algorithm is that it
makes the processing of the jobs faster than the SJF algorithm, mentioned its
overhead charges are not counted.
Disadvantages of SRTF: In SRTF, the context switching is done a lot more times
than in SJN due to more consumption of the CPU's valuable time for processing.
The consumed time of the CPU then adds up to its processing time which then
diminishes the advantage of fast processing of this algorithm.
Example

At the 0th unit of the CPU, there is only one process which is P1, so P1 gets
executed for the 1-time unit. At the 1st unit of the CPU, Process P2 arrives. Now,
the P1 needs 6 more units to be executed, and the P2 needs only 3 units. So, P2 is
executed first by pre-empting P1. At the 3rd unit of time, the process P3 arrives,
and the burst time of P3 is 4 units which is more than the completion time of P2
which is 1 unit, so P2 continues its execution. Now after the completion of P2, the
burst time of P3 is 4 units which means it needs only 4 units for completion while
P1 needs 6 units for completion. So, this algorithm picks P3 above P1 due to the
reason that the completion time of P3 is less than that of P1. P3 gets completed at
time unit 8, there are no new processes arrived. So again, P1 is sent for execution,
and it gets completed at the 14th unit.
The arrival Time and Burst time for three processes P1, P2, and P3 are given
in the above diagram. Let us calculate Turnaround time, completion time, and
waiting time.

FRAGMENTATION
Fragmentation is an unwanted issue that occurs in an operating system in
which a process is unloaded and loaded from memory, causing the free memory
space to become fragmented. As a result, the process cannot be assigned to the
memory blocks due to their small size.

Page 42 of 159
Operating System

In operating systems, fragmentation is a phenomenon that impacts storage


space efficiency, impeding both capacity and performance. Fragmentation usually
arises when blocks of storage space are scattered, leading to potential wastage.

Causes of Fragmentation: At its core, fragmentation is caused by the


dynamic allocation and deallocation of memory during a program's execution.
External fragmentation occurs when a process is removed from memory, leaving a
'hole' of unused memory. If the hole is too small or poorly located to accommodate
subsequent processes, fragmentation occurs.
Internal fragmentation, on the other hand, results from allocating memory in
fixed block sizes. If a process does not fully utilize its allocated block, the remaining
memory is wasted. This is essentially space reserved for a specific process but left
unused, creating inefficiency within the system.

Types of Fragmentation: Fragmentation is a condition in which memory is


allocated but not used efficiently. Below are the most common fragmentations:

Internal Fragmentation: Contrastingly, internal fragmentation occurs when


memory blocks allocated to processes exceed what they initially requested. The
leftover space within a block, which remains unused, gives rise to internal
fragmentation.
For Example,

Internal fragmentation is a classic case of over-allocation, where system


resources are wasted within allocated blocks. While attempts to prevent under-
allocation and the associated performance issues are well-intentioned,
overcompensation can lead to its own set of problems. Consider this simple
representation of internal fragmentation:

Process Memory Requested Memory Allocated

P1 10 units 15 units

P2 20 units 25 units

P3 30 units 35 units

Here, each process is allocated more memory than it requested, leading to


wasted space and internal fragmentation.

External Fragmentation: External fragmentation arises when free memory


blocks in a system become separated and non-contiguous. This typically happens

Page 43 of 159
Operating System

when memory blocks, once allocated, are freed up, leading to 'holes' of unused
memory spread across the system.

The issue is that these 'holes' or blocks may not be large enough to satisfy
subsequent allocation requests, despite collectively having sufficient space.
Consequently, the system is unable to use this memory effectively, leading to
wasted resources and decreased efficiency. Consider this simple representation of
external fragmentation:

Memory Blocks State

Block 1 Used

Block 2 Free

Block 3 Used

Block 4 Free

Block 5 Used

Here, although there is free memory (Blocks 2 and 4), it is not contiguous,
resulting in external fragmentation.

REVIEW QUESTIONS
1. What is an Operating System? Explain the different functions of the operating
system.
2. Explain the following terms: (1) Process (2) Creation and termination
operation of process.
3. What is multithreading? Explain with a suitable example.
4. What is scheduling? Explain the SJP Shortest-Job First algorithm.
5. What is a process thread? Explain.
6. What is a thread? Explain the concept of multithreading with a suitable
example.
7. Explain schedulers and types of schedulers in detail.
8. Describe the Round Robin CPU scheduling algorithm.
9. What is Micro Kernel? Explain its architecture and benefits.
10. Explain the structure of the Operating System.

Page 44 of 159
Operating System

11. What is Process? Explain different process states.


12. Explain: (i) Concurrent Process (ii) Multithreading.
13. Explain the FCFS (First Come First Served) CPU Scheduling algorithm with an
example.
14. What are the differences between process and threads? Explain process states
along with a diagram.
15. What are Threads? Explain its Life Cycle.
16. Explain FCFS, SRTF, and Round Robin CPU scheduling algorithms with proper
examples.
17. Explain Internal and External Fragmentation.
18. Explain: (i) User level thread (ii) Kernel level thread
19. Explain the SRTF CPU scheduling algorithm with an example.
20. List and explain the characteristics of modern operating systems.
21. Draw and explain the life cycle of the thread
22. What is microkernel? Explain its architecture and benefits.
23. Write short notes on (i) Multiprogramming and (ii) The sharing system.

Page 45 of 159
Operating System

CHAPTER 2
PERFORMANCE AND DEADLOCK

PERFORMANCE COMPARISON
The performance of an operating system is dependent on a variety of factors,
such as the hardware specifications of the computer, the design and
implementation of the operating system, the type and number of applications
running on it, and the workload and environment. These elements can have
varying degrees of impact on the performance of an operating system, depending on
the situation and goal. For instance, the CPU, memory, disk, and network can
influence the kernel, scheduler, file system, security features, user interface,
background processes, and network services. Moreover, user input, system load,
and external events can also affect its performance.
There are many scheduling algorithms, each with its parameters. As a
result, selecting an algorithm can be difficult. The first problem is defining the
criteria to be used in selecting an algorithm. Criteria are often defined in terms of
CPU utilization, response time, or throughput. To select an algorithm, we must first
define the relative importance of these measures. Our criteria may include several
measures, such as:
• Maximizing CPU utilization under the constraint that the maximum response
time is 1 second
• Maximizing throughput such that turnaround time is (on average) linearly
proportional to total execution time
Once the selection criteria have been defined, we want to evaluate the
algorithms under consideration. Following are the various evaluation methods we
can use.

DETERMINISTIC MODELLING
In a deterministic model, when one starts running the model with the same
initial condition every time, the result or the outcome is the same. Moreover, a
deterministic model does not involve randomness; it works accordingly. In the case
of the deterministic model when some work starts at a particular time that is at the
same pace every time, then the output of the model always depends on the initial
conditions. For a well-defined linear model, the unique output is produced from a
unique input, and in the case of a non-linear model, multiple outputs are
produced. This model can be described in different stages of temporal variations
viz. time-independent, time-dependent, and dynamic. A deterministic system
assumes an exact relationship between variables. As a result, this relationship
between variables enables one to predict and notice how variables affect the other.

Page 46 of 159
Operating System

This method takes a particular predetermined workload and defines the


performance of each algorithm for that workload. For example, assume that we
have the workload shown below. All five processes arrive at time 0, in the order
given, with the length of the CPU burst given in milliseconds:
Process Burst Time
P1 10
P2 29
P3 3
P4 7
P5 12
Consider the FCFS, SJF, and RR (quantum = 10 milliseconds) scheduling
algorithms for this set of processes. Which algorithm would give the minimum
average waiting time?
For the FCFS algorithm, we would execute the processes as

P1 P2 P3 P4 P5
0 10 39 42 49 61
The waiting time is 0 milliseconds for process P1, 10 milliseconds for process
P2, 39 milliseconds for process P3, 42 milliseconds for process P4, and 49
milliseconds for process P5. Thus, the average waiting time is (0 + 10 + 39 + 42 +
49)/5 = 28 milliseconds.
With non-preemptive SJF scheduling, we execute the processes as

P3 P4 P1 P5 P2
0 3 10 20 32 61
The waiting time is 10 milliseconds for process P1, 32 milliseconds for
process P2, 0 milliseconds for process P3, 3 milliseconds for process P 4, and 20
milliseconds for process P5. Thus, the average waiting time is (10 + 32 + 0 + 3 +
20)/5 = 13 milliseconds.
With the RR algorithm, we execute the processes as

P1 P2 P3 P4 P5 P2 P5 P2
0 10 20 23 30 40 50 52 61
The waiting time is 0 milliseconds for process P1, 10 milliseconds for process
P2, 20 milliseconds for process P3, 23 milliseconds for process P 4, and 40
milliseconds for process P5. Thus, the average waiting time is (0 + 32 + 20 + 23 +
40)/5 = 23 milliseconds.
We see that the average waiting time obtained with the SJF policy is less
than half that obtained with FCFS scheduling; the RR algorithm gives us an
intermediate value.
Deterministic modelling is simple and fast. It gives us exact numbers,
allowing us to compare the algorithms. However, it requires exact numbers for
input. The main uses of deterministic modelling are in describing scheduling
algorithms and providing examples. In cases where we are running the same
program over and over again and can measure the program's processing

Page 47 of 159
Operating System

requirements exactly, we may be able to use deterministic modelling to select a


scheduling algorithm. Furthermore, over a set of examples, deterministic modelling
may indicate trends that can then be analysed and proved separately. For example,
it can be shown that, for the environment described (all processes and their times
available at time 0), the SJF policy will always result in a minimum waiting time.

QUEUING ANALYSIS
On many systems, the processes that are run vary from day to day, so there
is no static set of processes (or times) to use for deterministic modelling. These
distributions can be measured and then approximated or simply estimated. The
result is a mathematical formula describing the probability of a particular CPU
burst. Commonly, this distribution is exponential and is described by its mean.
Similarly, we can describe the distribution of times when processes arrive in the
system (the arrival-time distribution). From these two distributions, it is possible to
compute the average throughput, utilization, waiting time, and so on for most
algorithms.
The computer system is described as a network of servers. Each server has a
queue of waiting processes. The CPU is a server with its ready queue, as is the I/O
system with its device queues. Knowing arrival rates and service rates, we can
compute utilization, average queue length, average wait time, and so on. This area
of study is called queueing network analysis.
As an example, let n be the average queue length (excluding the process
being serviced), let W be the average waiting time in the queue, and let X be the
average arrival rate for new processes in the queue (such as three processes per
second). We expect that when a process waits,  x W new processes will arrive in
the queue. If the system is in a steady state, then the number of processes leaving
the queue must be equal to the number of processes that arrive. Thus,
n=xW
This equation, known as Little's formula, is particularly useful because it is valid
for any scheduling algorithm and arrival distribution.
We can use Little's formula to compute one of the three variables if we know
the other two. For example, if we know that 7 processes arrive every second (on
average) and that there are normally 14 processes in the queue, then we can
compute the average waiting time per process as 2 seconds.
Queueing analysis can be useful in comparing scheduling algorithms, but it
also has limitations. At the moment, the classes of algorithms and distributions
that can be handled are fairly limited. The mathematics of complicated algorithms
and distributions can be difficult to work with. Thus, arrival and service
distributions are often defined in mathematically tractable but unrealistic ways. It
is also generally necessary to make several independent assumptions, which may
not be accurate. As a result of these difficulties, queueing models are often only
approximations of real systems, and the accuracy of the computed results may be
questionable.

Page 48 of 159
Operating System

SIMULATORS
The OS Simulator is designed to support two main aspects of a computer
system’s resource management: process management and memory management.
Once a compiled code is loaded into the CPU Simulator’s memory, its image is also
available to the OS Simulator. It is then possible to create multiple instances of the
program images as separate processes. The OS Simulator displays the running
processes, the ready processes, and the waiting processes. Each process is
assigned a separate Process Control Block (PCB) that contains information on the
process state. This information is displayed in a separate window. The main
memory display demonstrates the dynamic nature of page allocations according to
the currently selected placement policy. The OS maintains a separate page table for
each process which can also be displayed. The simulator demonstrates how data
memory is relocated and the page tables are maintained as the pages are moved in
and out of the main memory illustrating virtual memory activity.
The process scheduler includes various selectable scheduling policies that
include priority-based, pre-emptive, and round-robin scheduling with variable time
quanta. The OS can carry out context-switching which can be visually enhanced by
slowing down or suspending the progress at some key stage to enable the students
to study the states of CPU registers, stack, cache, pipeline, and PCB contents.
The simulator incorporates an input-output console device, incorporating a
virtual keyboard, and is used to display text and accept input data.
The OS simulator supports dynamic library simulation which is supported
by the appropriate language constructs in the teaching language. The benefits of
sharing code between multiple processes are visually demonstrated. There is also a
facility to link static libraries demonstrating the differences between the two types
of libraries and their benefits and drawbacks.
The simulator allows manual allocation and de-allocation of resources to
processes. This facility is used to create and demonstrate
process deadlocks associated with resources and enables experimentation with
deadlock prevention, detection, and resolution.

STARVATION
Starvation is a problem of resource management where in the OS, the
process does not have resources because it is being used by other processes. This
problem occurs mainly in a priority-based scheduling algorithm in which the
requests with high priority get processed first and the least priority process takes
time to get processed.

Page 49 of 159
Operating System

It is a problem when the low-priority process gets jammed for a long


duration of time because of high-priority requests being executed. A stream of high-
priority requests stops the low-priority process from obtaining the processor or
resources. Starvation happens usually when the process is delayed for an infinite
period of duration. Resources needed for the Operating system to handle requests
of the process:
• Memory
• CPU time
• Disk space
• Bandwidth of network
• I/O access to disk or network

What Causes Starvation in OS? Here are a few reasons why starvation in
OS occurs:
• In starvation, a process with low priority might wait forever if the process with
higher priority uses a processor constantly.
• Because the low-priority processes are not interacting with resources the
deadlock does not occur but there are chances of starvation as the low-priority
processes are kept in a wait state.
• Hence starvation is precisely a fail-safe method, that is it prevents deadlock
temporarily but it affects the system in general.
• The important cause of starvation might be that there are not enough resources
to provide for every resource.
• If a process selection is random then there can be a possibility that a process
might have to wait for a long duration.
• Starvation can also occur when a resource is never provided to a process for
execution due to faulty allocation of resources.

Various Methods to Handle Starvation in OS: Here are the following


ways in which the starvation situation in OS can be handled:
• The allocation of resources by CPU should be taken care of by a freelance
manager to ensure that there is an even distribution of resources.
• Random choice of process method should be avoided due to which starvation
occurs.
• The aging criteria of processes should be taken into consideration while
resource allocation to avoid starvation.
• Scheduling algorithm with a priority queue can also be used to handle
starvation.

Page 50 of 159
Operating System

• If the random technique is to be used then use it with a priority queue to


handle starvation.
• A multilevel feedback queue can also be used to avoid starvation in the
operating system.

Example of Starvation:

In the given example, the P2 process has the highest priority, and
process P1 has the lowest priority. In the diagram, it can be seen that there is n
number of processes ready for their execution. So, in the CPU, the process with the
highest priority will come in which is P2, and the process P1 will keep waiting for
its turn because all other processes are at a high priority concerning P1. This
situation in which the process is waiting is called starvation.

DEADLOCK
All the processes in a system require some resources such as a central
processing unit (CPU), file storage, input/output devices, etc to execute it. Once the
execution is finished, the process releases the resource it was holding. However,
when many processes run on a system, they also compete for the resources they
require for execution. This may cause a deadlock situation.
A deadlock is a situation in which more than one process is blocked
because it is holding a resource and also requires some resource that is acquired
by some other process. Therefore, none of the processes gets executed.
Example Of Deadlock

Page 51 of 159
Operating System

In the above figure, there are two processes and two resources. Process 1
holds "Resource 1" and needs "Resource 2" while Process 2 holds "Resource 2" and
requires "Resource 1". This creates a situation of deadlock because none of the two
processes can be executed. Since the resources are non-shareable, they can only be
used by one process at a time (Mutual Exclusion). Each process is holding a
resource and waiting for the other process the release the resource it requires.
None of the two processes releases their resources before their execution and this
creates a circular wait. Therefore, all four conditions are satisfied.

Necessary Conditions for Deadlock: The four necessary conditions for a


deadlock to arise are as follows.
• Mutual Exclusion: Only one process can use a resource at any given time i.e.
the resources are non-sharable.
• Hold and wait: A process is holding at least one resource at a time and is
waiting to acquire other resources held by some other process.
• No preemption: The resource can be released by a process voluntarily i.e. after
execution of the process.
• Circular Wait: A set of processes are circularly waiting for each other. For
example, let's say there are a set of processes {P0 P0, P1 P1, P2 P2, P3 P3} such
that P0P0 depends on P1P1, P1P1 depends on P2P2, P2P2 depends on P3P3
and P3P3 depends on P0P0. This creates a circular relation between all these
processes and they have to wait forever to be executed.

Methods of Handling Deadlocks: The first two methods are used to ensure
the system never enters a deadlock.
Deadlock Prevention: This is done by restraining the ways a request can be
made. Since deadlock occurs when all the above four conditions are met, we try to
prevent any one of them, thus preventing a deadlock.
Deadlock Avoidance: When a process requests a resource, the deadlock
avoidance algorithm examines the resource-allocation state. If allocating that
resource sends the system into an unsafe state, the request is granted.
Therefore, it requires additional information such as how many resources of
each type are required by a process. If the system enters an unsafe state, it must
step back to avoid deadlock.

Page 52 of 159
Operating System

Deadlock Detection and Recovery: We let the system fall into a deadlock and
if it happens, we detect it using a detection algorithm and try to recover. Some
ways of recovery are as follows.
• Aborting all the deadlocked processes.
• Abort one process at a time until the system recovers from the deadlock.
• Resource Preemption: Resources are taken one by one from a process and
assigned to higher priority processes until the deadlock is resolved.
Deadlock Ignorance: In the method, the system assumes that deadlock never
occurs. Since the problem of deadlock situations is not frequent, some systems
simply ignore it. Operating systems such as UNIX and Windows follow this
approach. However, if a deadlock occurs, we can reboot our system, and the
deadlock is resolved automatically.

Difference between Starvation and Deadlocks


Deadlock Starvation

A deadlock is a situation in which more


Starvation is a process in which the
than one process is blocked because it
low-priority processes are postponed
is holding a resource and also requires
indefinitely because the resources are
some resource that is acquired by some
never allocated.
other process.
Resources are blocked by a set of Resources are continuously used by
processes in a circular fashion. high-priority resources.
It is prevented by avoiding anyone
necessary condition required for a
It can be prevented by aging.
deadlock or recovery using a recovery
algorithm.
In starvation, higher-priority processes
In a deadlock, none of the processes get
execute while lower-priority processes
executed.
are postponed.
Deadlock is also called circular wait. Starvation is also called lived lock.

RESOURCE ALLOCATION GRAPH


The Resource Allocation Graph (RAG) is a fundamental concept in the field of
Operating Systems (OS). One of the critical roles of the Resource Allocation
Graph is to identify potential deadlocks.
To describe deadlocks in a more precise way directed graphs are used that
are called system Resource Allocation Graph. This Graph acts as the pictorial
representation of the state of the system. The Resource Allocation graph mainly
consists of a set of vertices V and a set of Edges E. This graph mainly contains
all the information related to the processes that are holding some resources and
also contains the information on the processes that are waiting for some more
resources in the system.

Page 53 of 159
Operating System

Also, this graph contains all the information that is related to all the
instances of the resources which means the information about available resources
and the resources which are being used by the process. In this graph, the circle is
used to represent the process, and the rectangle is used to represent the resource.

Components of Resource Allocation Graph: Given below are the


components of RAG:
1. Vertices
2. Edges

Vertices: There are two kinds of vertices used in the resource allocation graph
and these are:
• Process Vertices
• Resource Vertices
Process Vertices: These vertices are used to represent process vertices. The
circle is used to draw the process vertices and the name of the process is
mentioned inside the circle.
Resource Vertices: These vertices are used to represent resource vertices. The
rectangle is used to draw the resource vertices and we use dots inside the circle to
mention the number of instances of that resource.

In the system, there may exist several instances and according to them,
there are two types of resource vertices and these are single instances and multiple
instances.
Single Instance: In a single instance resource type, there is a single dot inside
the box. The single dot mainly indicates that there is one instance of the resource.
Multiple Instance: In multiple instance resource types, there are multiple dots
inside the box, and these Multiple dots indicate that there are multiple instances of
the resources.

Edges: In the Resource Allocation Graph, Edges are further categorized into two:

Page 54 of 159
Operating System

Assign Edges: Assign Edges are mainly used to represent the allocation of
resources to the process. We can draw assigned edges with the help of an arrow in
which mainly the arrowhead points to the process, and the process mainly tail
points to the instance of the resource.

In the above Figure, the resource is assigned to the process


Request Edges: Request Edge is mainly used to signify the waiting state of the
process. Likewise in the assigned edge, an arrow is used to draw an arrow edge.
But here the arrowhead points to the instance of a resource, and the tail of the
process points to the process.

In the above figure, the process is requesting a resource

Single Instance RAG Example: Suppose there are Four Processes P1, P2,
P3, P4, and two resources R1 and R2, where P1 is holding R1 and P2 is holding R2,
P3 is waiting for R1 and R2 while P4 is waiting for resource R1.

In the above example, there is no circular dependency so there are no


chances for the occurrence of deadlock. Thus, having cycled in a single-instance
resource type must be a sufficient condition for deadlock.

Multiple Instance RAG Example: Suppose there are four processes P1, P2,
P3, P4 and there are two instances of resource R1 and two instances of resource
R2:

Page 55 of 159
Operating System

One instance of R2 is assigned to process P1 and another instance of R2 is


assigned to process P4, Process P1 is waiting for resource R1. One instance of R1 is
assigned to Process P2 while another instance of R2 is assigned to Process P3,
Process P3 is waiting for resource R2.

Advantages of RAG
• It is pretty effective in detecting deadlocks.
• The Banker's Algorithm makes extensive use of it.
• It's a graphical depiction of a system.
• A quick peek at the graph may sometimes tell us if the system is in a deadlock
or not.
• Understanding resource allocation using RAG takes less time.

Disadvantages of RAG
• RAG is beneficial when there are fewer procedures and resources.
• When dealing with a large number of resources or processes, it is preferable to
keep data in a table than an RAG.
• When there are a vast number of resources or processes, the graph becomes
challenging to interpret and very complex.

CONDITIONS FOR DEADLOCK


These four conditions must be met for a deadlock to happen in an operating
system.

1. Mutual Exclusion: In this, two or more processes must compete for the
same resources. There must be some resources that can only be used one
process at a time. This means the resource is non-sharable. This could be a
physical resource like a printer or an abstract concept like a lock on a shared
data structure.
Deadlocks never occur with shared resources like read-only files but with
exclusive access to resources like tape drives and printers.

Page 56 of 159
Operating System

2. Hold and Wait: Hold and wait is when a process is holding a resource and
waiting to acquire another resource that it needs but cannot proceed because
another process is keeping the first resource. Each of these processes must
have a hold on at least one of the resources it’s requesting. If one process
doesn’t have a hold on any of the resources, it can’t wait and will give up
immediately.
When this condition exists, the process must be stopped if it
simultaneously holds one or more resources while waiting for another resource.
In the below diagram, Process 1 currently holding Resource 1 is waiting for
additional Resources 2.

3. No Preemption: When a process completes its task, it can release a


resource voluntarily. A process that holds some resources requests another
that cannot be allocated to it immediately and, in that case, all resources will
become available.
There is a process that is waiting for the list of preempted resources. If the
process can recover its old resources and request a new one, it will be
restarted.
Whenever another resource is available, it is given to the process
requesting it. We release it and give it to the requesting process if it is being
held by another process waiting for another resource.

4. Circular Wait: The circular wait is when two processes wait for each other to
release a resource they are holding, creating a deadlock. There must be a cycle
in the graph below. As you can see, process 1 is holding on to a resource R1
that process 2 in the cycle is waiting for. This is an example of a circular wait.
To better understand let’s understand with another example. For example,
Process A might be holding on to Resource X while waiting for Resource Y,
while Process B is holding on to Resource Y while waiting for Resource Z, and
so on around the cycle.

Page 57 of 159
Operating System

Here,
Process P1 waits for a resource held by process P2.
Process P2 waits for a resource held by process P3.
Process P3 waits for a resource held by process P4.
Process P4 waits for a resource held by process P1.

DEADLOCK PREVENTION
Deadlock prevention is eliminating one of the necessary conditions of
deadlock so that only safe requests are made to OS and the possibility of deadlock
is excluded before making requests. Here OS does not need to do any additional
tasks as it does in deadlock avoidance by running an algorithm on requests
checking for the possibility of deadlock.

Deadlock Prevention Techniques: Deadlock prevention techniques refer


to violating any one of the four necessary conditions. We will see one by one how
we can violate each of them to make safe requests and which is the best approach
to prevent deadlock.

Mutual Exclusion: Some resources are inherently unshakeable, for example,


Printers. For unshareable resources, processes require exclusive control of the
resources. A mutual exclusion means that unshakeable resources cannot be
accessed simultaneously by processes. Shared resources do not
cause deadlock but some resources can't be shared among processes, leading to a
deadlock.
For Example: a read operation on a file can be done simultaneously by multiple
processes, but a write operation cannot. Write operation requires sequential access,
so, some processes have to wait while another process is doing a write operation.
It is not possible to eliminate mutual exclusion, as some resources are
inherently non-shareable. For Example, tape drive, as only one process can access
data from a Tape drive at a time. For other resources like printers, we can use a
technique called Spooling.
Spooling: It stands for Simultaneous Peripheral Operations online. A Printer has
associated memory which can be used as a spooler directory (memory that is used
to store files that are to be printed next).
In spooling, when multiple processes request the printer, their jobs
(instructions of the processes that require printer access) are added to the queue in
the spooler directory. The printer is allocated to jobs on a first come first serve

Page 58 of 159
Operating System

(FCFS) basis. In this way, the process does not have to wait for the printer and it
continues its work after adding its job to the queue. We can understand the
workings of the Spooler directory better with the diagram given below:

Hold and Wait: Hold and wait is a condition in which a process is holding one
resource while simultaneously waiting for another resource that is being held by
another process. The process cannot continue till it gets all the required resources.
In the diagram given below:

• Resource 1 is allocated to Process 2


• Resource 2 is allocated to Process 1
• Resource 3 is allocated to Process 1
• Process 1 is waiting for Resource 1 and holding Resource 2 and Resource 3
• Process 2 is waiting for Resource 2 and holding Resource 1
There are two ways to eliminate hold and wait:
1. By eliminating wait: The process specifies the resources it requires in advance
so that it does not have to wait for allocation after execution starts. For
Example, Process1 declares in advance that it requires both Resource1 and
Resource2
2. By eliminating hold: The process has to release all resources it is currently
holding before making a new request. For Example: Process1 has to release
Resource2 and Resource3 before making request for Resource1

No preemption: Preemption is temporarily interrupting an executing task and


later resuming it. For example, if process P1 is using a resource and a high-priority
process P2 requests for the resource, process P1 is stopped and the resources are
allocated to P2. There are two ways to eliminate this condition by preemption:

Page 59 of 159
Operating System

1. If a process is holding some resources and waiting for other resources, then it
should release all previously held resources and put a new request for the
required resources again. The process can resume once it has all the required
resources.
For example: If a process has resources R1, R2, and R3 and it is waiting for
resource R4, then it has to release R1, R2, and R3 and put a new request for all
resources again.
2. If a process P1 is waiting for some resource, and there is another process P2 that
is holding that resource and is blocked waiting for some other resource. Then
the resource is taken from P2 and allocated to P1. This way process P2 is
preempted and it requests again for its required resources to resume the task.
The above approaches are possible for resources whose states are easily restored
and saved, such as memory and registers.

Circular Wait: In circular wait, two or more processes wait for resources in a
circular order. We can understand this better by the diagram given below:

To eliminate circular wait, we assign a priority to each resource. A process


can only request resources in increasing order of priority.
In the example above, process P3 is requesting resource R1, which has a
number lower than resource R3 which is already allocated to process P3. So, this
request is invalid and cannot be made, as R1 is already allocated to process P1.

DEADLOCK DETECTION
If a system does not employ either a deadlock-prevention or a deadlock
avoidance algorithm, then a deadlock situation may occur. In this environment, the
system must provide:
• An algorithm that examines the state of the system to determine whether a
deadlock has occurred
• An algorithm to recover from the deadlock

Single Instance of Each Resource Type- If all resources have only a single
instance, then we can define a deadlock-detection algorithm that uses a variant of
the resource-allocation graph, called a wait-for graph. We obtain this graph from
the resource-allocation graph by removing the resource nodes and collapsing the
appropriate edges.

Page 60 of 159
Operating System

P5

P5
R1 R3 R4

P1 P2 P3 P1 P2 P3

P4 P4
R2 R5
(a) (b)
Figure: (a) Resource-allocation graph (b) Corresponding wait-for graph
More precisely, an edge from Pi, to Pj, in a wait-for graph implies that
process Pi, is waiting for process Pj, to release a resource that Pi needs. An edge
Pi → P, exists in a wait-for graph if and only if the corresponding resource-
allocation graph contains two edges P → Rj and Rj → Pi for some resource Rj, for
example, in the figure, we present a resource-allocation graph and the
corresponding wait-for graph.
As before, a deadlock exists in the system if and only if the wait-for graph
contains a cycle. To detect deadlocks, the system needs to maintain the wait-for
graph and periodically invoke an algorithm that searches for a cycle in the graph.
An algorithm to detect a cycle in a graph requires an order of n1 operations, where
n is the number of vertices in the graph.

Several Instances of a Resource Type: The wait-for graph scheme does not
apply to a resource-allocation system with multiple instances of each resource type.
We turn now to a deadlock detection algorithm that is applicable to such a system.
The algorithm employs several time-varying data structures that are similar to
those used in the banker's algorithm:
• Available- A number of available resources of each type.
• Allocation- A number of resources of each type currently allocated to each
process.
• Request- A current request of each process.
We consider a system with five processes P1 through P4 and three resource
types A, B, and C. Resource type A has seven instances, resource type B has two
instances, and resource type C has six instances. Suppose that, at time T0, we have
the following resource-allocation state:
Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100

Page 61 of 159
Operating System

P4 002 002
Suppose now that process P2 makes one additional request for an instance of
type C. The Request matrix is modified as follows:
Request
ABC
P0 000
P1 202
P2 001
P3 100
P4 002
We claim that the system is now deadlocked. Although we can reclaim the
resources held by process Po, the number of available resources is not sufficient to
fulfil the requests of the other processes. Thus, a deadlock exists, consisting of
processes P1, P2, P3, and P4.

Detection-Algorithm Usage: The detection algorithm depends on two factors:


1. How often is a deadlock likely to occur?
2. How many processes will be affected by deadlock when it happens?
If deadlocks occur frequently, then the detection algorithm should be
invoked frequently. Resources allocated to deadlocked processes will be idle until
the deadlock can be broken. In addition, the number of processes involved in the
deadlock cycle may grow.
Deadlocks occur only when some process makes a request that cannot be
granted immediately. This request may be the final request that completes a chain
of waiting processes. In the extreme, we can invoke the deadlock detection
algorithm every time a request for allocation cannot be granted immediately. In this
case, we can identify not only the deadlocked set of processes but also the specific
process that "caused" the deadlock. (In reality, each of the deadlocked processes is
a link in the cycle in the resource graph, so all of them, jointly, caused the
deadlock.) If there are many different resource types, one request may create many
cycles in the resource graph, each cycle completed by the most recent request and
"caused" by the one identifiable process.
If the deadlock-detection algorithm is invoked for every resource request,
this will incur a considerable overhead in computation time. A less expensive
alternative is simply to invoke the algorithm at less frequent intervals - for example,
once per hour or whenever CPU utilization drops below 40 percent. (A deadlock
eventually cripples system throughput and causes CPU utilization to drop.) If the
detection algorithm is invoked at arbitrary points in time, there may be many
cycles in the resource graph. In this case, we would generally not be able to tell
which of the many deadlocked processes “caused” the deadlock.

RECOVERY FROM DEADLOCK

Page 62 of 159
Operating System

To eliminate deadlocks by aborting a process, we use one of two methods. In


both methods, the system reclaims all resources allocated to the terminated
processes.
• Abort all deadlocked processes- This method clearly will break the deadlock
cycle but at great expense; the deadlocked processes may have been computed
for a long time, and the results of these partial computations must be discarded
and probably will have to be recomputed later.
• Abort one process at a time until the deadlock cycle is eliminated- This
method incurs considerable overhead, since, after each process is aborted, a
deadlock-detection algorithm must be invoked to determine whether any
processes are still deadlocked.
Aborting a process may not be easy. If the process was updating a file,
terminating it would leave that file in an incorrect state. Similarly, if the process
was printing data on a printer, the system must reset the printer to the correct
state before printing the next job.
If the partial termination method is used, then we must determine which
deadlocked process (or processes) should be terminated. This determination is a
policy decision, similar to CPU-scheduling decisions. We should abort those
processes whose termination will incur the minimum cost. Many factors may affect
which process is chosen, including:
1. What the priority of the process is
2. How long the process has computed and how much longer the process will take
before completing its designated task
3. How many and what type of resources the process has used (for example,
whether the resources are simple to preempt)
4. How many more resources the process needs to complete
5. How many processes will need to be terminated
6. Whether the process is interactive or batch
To eliminate deadlocks using resource preemption, we successively preempt
some resources from processes and give these resources to other processes until
the deadlock cycle is broken.
If preemption is required to deal with deadlocks, then three issues need to
be addressed:
1. Selecting a victim- Which resources and which processes are to be preempted?
As in process termination, we must determine the order of preemption to
minimize cost. Cost factors may include such parameters as the number of
resources a deadlocked process is holding and the amount of time the process
has thus far consumed during its execution.
2. Rollback- If we preempt a resource from a process, what should be done with
that process? It cannot continue with its normal execution; it is missing some
needed resources. We must roll back the process to some safe state and restart
it from that state. Since it is difficult to determine what a safe state is, the
simplest solution is a total rollback: Abort the process and then restart it.
Although it is more effective to roll back the process only as far as necessary to

Page 63 of 159
Operating System

break the deadlock, this method requires the system to keep more information
about the state of all running processes.
3. Starvation- How do we ensure that starvation will not occur? That is, how can
we guarantee that resources will not always be preempted from the same
process?
In a system where victim selection is based primarily on cost factors, the
same process may always pick a victim. As a result, this process never completes
its designated task, a starvation situation that must be dealt with in any practical
system. We must ensure that a process can be picked as a victim only a (small)
finite number of times. The most common solution is to include the number of
rollbacks in the cost factor.

Recovery Strategies/Methods:
• Process Termination: One way to recover from a deadlock is to terminate one
or more of the processes involved in the deadlock. By releasing the resources
held by these terminated processes, the remaining processes may be able to
continue executing. However, this approach should be used cautiously, as
terminating processes could lead to loss of data or incomplete transactions.
• Resource Preemption: Resources can be forcibly taken away from one or
more processes and allocated to the waiting processes. This approach can break
the circular wait condition and allow the system to proceed. However, resource
preemption can be complex and needs careful consideration to avoid disrupting
the execution of processes.
• Process Rollback: In situations where processes have checkpoints or states
saved at various intervals, a process can be rolled back to a previously saved
state. This means that the process will release all the resources acquired after
the saved state, which can then be allocated to other waiting processes.
Rollback, though, can be resource-intensive and may not be feasible for all types
of applications.
• Wait-Die and Wound-Wait Schemes: As mentioned in the Deadlock
Detection in OS section, these schemes can also be used for recovery. Older
processes can preempt resources from younger processes (Wound-Wait), or
younger processes can be terminated if they try to access resources held by older
processes (Wait-Die).
• Kill the Deadlock: In some cases, it might be possible to identify a specific
process that is causing the deadlock and terminate it. This is typically a last
resort option, as it directly terminates a process without any complex recovery
mechanisms.

BANKER’S ALGORITHM
The banker algorithm in the Operating System is used to avoid deadlock and
for resource allocation safely to each process in the system. As the name suggests,
it is mainly used in the banking system to check whether a loan can be sanctioned
to a person or not.

Page 64 of 159
Operating System

The banker's algorithm in OS is a combination of two main algorithms: the


safety algorithm (to check whether the system is in a safe state or not) and the
resource request algorithm (to check how the system behaves when a process
makes a resource request).

Bankers Algorithm in OS: A process in OS can request a resource, can use


the resource, and can also release it. There comes a situation of deadlock in OS in
which a set of processes is blocked because it is holding a resource and also
requires some other resources at the same time that are being acquired by other
processes.
So, to avoid such a situation of deadlock, we have the Bankers algorithm in
the Operating System.
The banker’s algorithm in OS is a deadlock avoidance algorithm and it is
also used to safely allocate the resources to each process within the system.
It was designed by Edsger Dijkstra. As the name suggests, the Bankers
algorithm in OS is mostly used in banking systems to identify whether a loan
should be given to a person or not.
To understand this algorithm in detail, let us discuss a real-world
scenario. Supposing there are 'n' account holders in a particular bank and the total
sum of their money is 'x'.
Now, if a person applies for a loan (let's say for buying a car), then the loan
amount subtracted from the total amount available in the bank gives us the
remaining amount and that should be greater than 'x', then only the loan will be
sanctioned by the bank.
It is done keeping in mind the worst case where all the account holders come
to withdraw their money from the bank at the same time. Thus, the bank always
remains in a safe state.
Bankers’ algorithm in OS works similarly. Each process within the system
must provide all the important necessary details to the operating system like
upcoming processes, requests for the resources, delays, etc.
Based on these details, OS decides whether to execute the process or keep it
in the waiting state to avoid deadlocks in the system. Thus, the Banker algorithm is
sometimes also known as the Deadlock Detection Algorithm.

Banker's Algorithm consists of two algorithms

1. Safety Algorithm: The safety algorithm checks for the safe state of the
system. If the system is in a safe state with any of the resource allocation
permutations, then deadlock can be avoided. The steps in the Safety Algorithm
are:
Step 1: Let there be two vectors Work and Finish of length m and n,
respectively. The work array represents the total available resources of each
type (R0, R1, R2, ...Rm) and the Finish array represents whether the particular
process Pi has finished execution or not. It can have a Boolean value of
true/false.
Work = Available

Page 65 of 159
Operating System

Finish[i] = false, where i = 0,1,2, 3, .... n


Initially, all processes are assumed to be unfinished so for all i Finish[i] = false.
Step 2: Need is also an array of size m for each process. We can iterate over
each process from i=0,1,2,3... to n and find if any process Pi exists for
which Need[i]<Work[i] and Finish[i]=false.
Need[i]<Work[i]
Finish[i] = false, i = 0,1,2,3...n
If no such process exists and finish[i]= false for all processes, then the
system is not in a safe state as there is no such process that can complete its
execution from the available resources. So, the system can go in a deadlock
state. If Finish[i]= true for all processes then proceed to step 4.
Step 3: If any process exists for which Need<Available as in step 2 then
allocate those resources to that particular process and increase the allocated
array for that process by Allocated[i].
Work[i] = Work[i] + Allocated[i]
Finish[i] = true
Once this process is executed then proceed to step-2 and repeat for another
process until all the processes complete their execution and Finish[i]= true for
all i = 0, 1, 2, 3 ... n.
Step 4: If finish[i] = true for all i = 0, 1, 2, 3.. n then the system is considered to
be in a safe state. Number of operations required in the worst case in safety
algorithm = m∗n2

2. Resource request algorithm: The resource request algorithm checks if the


request can be safely granted or not to the process of requesting resources.
If a process Pi request for c instances for resource of type j then it can be
represented as Request [i, j] = c. Steps in Resource request algorithm:
Step 1: If Request [i, j] <= Need [i, j] then it means the process is requesting
resources less than or equal to its need for execution. If Request [i, j] > Need [i,
j] then an error flag can be raised stating that the process is requesting
resources beyond its need. If Request [i, j] <= Need [i, j] then proceed to step 2.
Step 2: If Request [i. j] < Available [i, j] then it means the requested resources
are available in the system and can be allocated to the process. If Request [i. j] <
Available [i, j] then proceed to step-3 else the process has to wait for the
available resource to be greater than equal to the requested resources.
Step 3: Now, if Request [i, j] < Available [i, j] then the resource is allocated to
the process Pi, and the Available [i, j], Allocation [i, j], and Need [i, j] will update
as given below:
Available [i, j] = Available [i, j] – Request [i, j]
Allocation [i, j] = Allocation [i, j] + Request [i, j]
Need [i, j] = Need [i, j] – Request [i, j]
If the above resource allocation is safe then the transaction is completed
and Pi is allocated its resources for execution. But, if the new state of the system is

Page 66 of 159
Operating System

unsafe then the process Pi has to wait for the resources to fulfil its request
demands, and the old allocation state is again restored.

Example of Bankers Algorithm: In this example, we have a process table


with several processes that contains an allocation field (for showing the number of
resources of type: A, B, and C allocated to each process in the table), max field (for
showing the maximum number of resources of type: A, B, and C that can be
allocated to each process) and also, the available field (for showing the currently
available resources of each type in the table).
Processes Allocation Max Available
ABC ABC ABC
P0 112 544 321
P1 212 433
P2 301 913
P3 020 864
P4 112 223
Considering the above processing table, we need to calculate the following two
things: Q.1 Calculate the need matrix. Q.2 Is the system in a safe state?
Ans.1 We can easily calculate the entries of the need matrix using the formula:
(Need)i = (Max)i − (Allocation) i (Need)i =(Max)i − (Allocation)i
Process Need
ABC
P0 432
P1 221
P2 612
P3 844
P4 111
Ans.2 Let us check for the safe sequence:
1. For process P0, Need = (4, 3, 2) and Available = (3, 2, 1) Clearly, the resources
needed are more in number than the available ones. So, now the system will
move to process the next request.
2. For Process P1, Need = (2, 2, 1) and Available = (3, 2, 1) Clearly, the resources
needed are less than equal to the available resources within the system. Hence,
the request of P1 is granted.
Available = Available + Allocation
= (3, 2, 1) + (2, 1, 2) = (5, 3, 3) (New Available)
3. For Process P2, Need = (6, 1, 2) and Available = (5, 3, 3) Clearly, the resources
needed are more in number than the available ones. So, now the system will
move to process the next request.

Page 67 of 159
Operating System

4. For Process P3, Need = (8, 4, 4) and Available = (5, 3, 3) Clearly, the resources
needed are more in number than the available ones. So, now the system will
move to process the next request.
5. For Process P4, Need = (1, 1, 1) and Available = (5, 3, 3) Clearly, the resources
needed are less than equal to the available resources within the system. Hence,
the request for P4 is granted.
Available = Available + Allocation
= (5, 3, 3) + (1, 1, 2) = (6, 4, 5) (New Available)
6. Now again check for Process P2, Need = (6, 1, 2) and Available = (6, 4,
5) Clearly, resources needed are less than equal to the available resources
within the system. Hence, the request of P2 is granted.
Available = Available + Allocation
= (6,4,5) + (3,0,1) = (9,4,6) (NewAvailable)
7. Now again check for Process P3, Need = (8, 4, 4) and Available = (9, 4,
6) Clearly, the resources needed are less than equal to the available resources
within the system. Hence, the request for P3 is granted.
Available = Available + Allocation
= (9,4,6) + (0,2,0) = (9,6,6)
8. Now again check for Process P0, Need = (4, 3, 2), and Available (9, 6, 6) Clearly,
the request for P0 is also granted.
Safe sequence: <P1, P4, P2, P3, P0> <P1, P4, P2, P3, P0>
The system has allocated all the required number of resources to each
process in a particular sequence. Therefore, it is proved that the system is in a safe
state.

Constraints of Banker Algorithm: The only constraint in Banker's algorithm is


that Available should always satisfy at least the process resource need so that the
system doesn't land up in an unsafe state and the algorithm has to roll back to the
original allocated state.

Advantages of Bankers Algorithm:


• Bankers’ algorithm in OS consists of [MAX] array attribute which indicates the
maximum number of resources of each type that a process can hold. Using the
[MAX] array, we can always find the need for resources for a particular
process. [Need] = [MAX] − [Allocated][Need] = [MAX] − [Allocated]
• This algorithm helps in detecting and avoiding deadlock and also, helps in
managing and controlling process requests of each type of resource within the
system.
• Each process should provide information to the operating system about
upcoming resource requests, the number of resources, delays, and how long the
resources will be held by the process before release. This is also one of the main
characteristics of the Bankers algorithm.
• Various types of resources are maintained by the system while using this
algorithm, which can fulfil the needs of at least one process type.

Page 68 of 159
Operating System

• This algorithm also consists of two other advanced algorithms for maximum
resource allocation.

Disadvantages of Bankers Algorithm:


• Bankers’ algorithm in OS doesn't allow a process to change its maximum need of
resources while processing.
• Another disadvantage of this algorithm is that all the processes within the
system must know about the maximum resource needs in advance.
• It requires a fixed number of processes for processing and no additional process
can be started in between.
• This algorithm allows all the resource requests of the processes to be granted in
a fixed finite period, but the maximum period is one year for allocating the
resources.

Difference Between Program and Process


Program Process
The program contains a set of
The process is an instance of an
instructions designed to complete a
executing program.
specific task.
The process is an active entity as it is
A program is a passive entity as it
created during execution and loaded
resides in the secondary memory.
into the main memory.
The process exists for a limited period
Program exists at a single place and
as it gets terminated after the
continues to exist until it is deleted.
completion of the task.
A program is a static entity. The process is a dynamic entity.
The program does not have any The process has a high resource
resource requirement, it only requires requirement, it needs resources like
memory space for storing the CPU, memory address, and I/O
instructions. during its lifetime.
The program does not have any control Process has its control block called
block. Process Control Block.
In addition to program data, a process
The program has two logical also requires additional information
components: code and data. required for the management and
execution.
Many processes may execute a single
program. Their program code may be
The program does not change itself.
the same but program data may be
different. these are never the same.
The process is a sequence of
Program contains instructions
instruction execution.

Page 69 of 159
Operating System

REVIEW QUESTIONS
1. What is deadlock? Explain various conditions arising from deadlock.
2. Write short notes on (1) Resource Allocation Graph and (2) Simulators.
3. What is deadlock detection? Explain recovery of deadlock.
4. Write short notes on (1) Deterministic Modeling (2) Queuing Analysis.
5. Explain starvation.
6. Explain mutual exclusion in detail.
7. What is deadlock? Explain the resource allocation graph.
8. Explain the methods for recovery from deadlock.
9. Explain the Hold and Wait condition in brief.
10. What is deadlock? Explain the conditions for deadlock.
11. What is Deadlock Prevention? How can we prevent a deadlock?
12. Write a note on Multithreading.
13. Explain Banker's algorithm for deadlock avoidance.
14. Differentiate between program and process.
15. Explain circular wait conditions with examples.

Page 70 of 159
Operating System

CHAPTER 3
MEMORY MANAGEMENT

INTRODUCTION
The operating system manages the resources of the computer, controls
application launches, and performs tasks such as data protection and system
administration. The resource that the operating system uses the most is memory.
Memory is a storage area on the computer that contains the instructions and data
that the computer uses to run the applications.
When the applications or the operating system need more memory than is
available on the computer, the system must swap the current contents of the
memory space with the contents of the memory space that is being requested. In
the same way, different situations need different memory management techniques.
Some cases call for the use of paging, while others may require the use of an
on-disk cache. Ultimately, deciding which memory management technique to use is
a matter of optimizing the user interface for the available hardware and software.
Memory management is allocating, freeing, and re-organizing memory in a
computer system to optimize the available memory or to make more memory
available. It keeps track of every memory location (if it is free or occupied).

Memory Allocation Schemes


Contiguous Memory Management Schemes: Contiguous memory allocation
means assigning continuous blocks of memory to the process. The best example
is Array.
Non-contiguous Memory Management Schemes: The program is divided into
blocks (fixed size or variable size) and loaded at different portions of memory. That
means program blocks are not stored adjacent to each other.
Memory management in OS is a technique of controlling and managing the
functionality of Random-access memory (primary memory). It is used for achieving
better concurrency, system performance, and memory utilization. Memory
management moves processes from primary memory to secondary memory and vice
versa. It also keeps track of available memory, memory allocation, and unallocated.

Page 71 of 159
Operating System

Use Memory Management In OS:


• Memory management keeps track of the status of each memory location,
whether it is allocated or free.
• Memory management enables computer systems to run programs that require
more main memory than the amount of free main memory available on the
system. This is achieved by moving data between primary and secondary
memory.
• Memory management addresses the system’s primary memory by providing
abstractions such that the programs running on the system perceive a large
memory is allocated to them.
• It is the job of memory management to protect the memory allocated to all the
processes from being corrupted by other processes. If this is not done, the
computer may exhibit unexpected/faulty behaviour.
• Memory management enables sharing of memory spaces among processes, with
the help of which, multiple programs can reside at the same memory location
(although only one at a time).

LOGICAL VS. PHYSICAL ADDRESS SPACE


Logical and physical addresses are important terms. The logical address is
rendered by the CPU while a program is executed, whereas the physical address is
directed to a location in the memory unit.
Physical addresses are specific to the hardware architecture and memory
layout of a specific computer system. They are not portable between systems or
even architectures. Logical addresses, in turn, may be portable and adapted for use
on many different systems or architectures, provided that the translation
mechanisms of those addresses are compatible.

Physical Address Space: Physical addresses are addresses that specify actual
(real) physical locations in memory. It is a real memory location where the data is
stored. Hardware, such as the CPU and memory controller, can directly access
corresponding memory locations with physical addresses, and translation or
mapping is not involved.
Physical addresses are low-level addressing modes that refer to hardware
architecture and point to a specific computer's memory layout. The hardware uses
this mode directly to access memory locations and communicate with devices.

Page 72 of 159
Operating System

Physical addressing allows hardware to access memory locations directly. It points


exactly to where in the memory data is being written or read.
It is the map of the fixed and predetermined hardware design configuration
and memory, relating the physical addresses to the memory locations. The size of
the physical addresses is directly proportional to the quantity of physical memory
in the system. In lay terms, the size of the physical address space shall be
determined according to the total amount of memory installed.
Physical addresses cannot be moved from one system or architecture to
another since they are directly related and associated with a computer system's
hardware and memory layout. Physical addresses are more effective in memory
accessing than logical addresses. This justifies that overhead from translations and
mappings of the address is avoided.

Logical Address Space: The logical addresses are the virtual addresses of a
CPU generated at run time. These do not exist in the memories as physical
addresses; they act as pointers for the CPU to access real memory locations.
Logical addresses are addresses used by software programs and operating
systems to simplify memory management and provide a more flexible and abstract
way of accessing memory or devices. Logical addresses are part of the virtual
memory abstraction, which allows programs to operate in a larger logical address
space than the available physical memory.
A logical address must be mapped or translated to its corresponding
physical addresses before the hardware can use it. This translation is typically
performed by a hardware component called the Memory Management Unit (MMU).
Using logical addresses allows for memory protection mechanisms, where different
programs or processes are isolated from each other's memory spaces, enhancing
security and stability.
Logical addresses are generally more portable and can be used across
different systems or architectures as long as the address translation mechanisms
are compatible.
The abstraction provided by logical addresses enables features like demand
paging, swapping, and shared memory, which are crucial for efficient memory
management and resource utilization.

Tabular Comparison Between Physical Address and Logical


Address: Here's a tabular comparison of the key differences between physical
addresses and logical addresses:

Benchmark Physical Address Logical Address

A virtual or symbolic
Represents the actual
representation of memory
Representation physical location of data in
locations, used by software
memory or devices
programs
Generated based on the
Generated by the CPU while a
Generation hardware architecture and
program is running
memory configuration

Page 73 of 159
Operating System

Must be translated or mapped to


Used directly by hardware
its corresponding physical address
Address to access memory
by the Memory Management Unit
Translation locations, no translation
(MMU) before being used by the
required
hardware
Limited by the amount of Can be part of a larger virtual
Address Space installed physical memory address space, exceeding the
in the system available physical memory
Tied to a specific hardware More portable across different
architecture and memory systems and architectures, as long
Portability
layout, not portable across as the address translation
systems mechanisms are compatible
Directly accessed by Used by software programs and
hardware components for operating systems for memory
Purpose
low-level operations and management, memory protection,
efficient data transfer. and efficient resource sharing
Abstraction Low-level, hardware- Higher-level, software-friendly
Level specific addresses addresses
No mapping is required, Mapped or translated to physical
Mapping
directly used by hardware addresses by the MMU
Memory Provides direct access to Provides an abstraction layer over
Access physical memory locations physical memory access
Enables features like virtual
Memory
Limited memory memory, demand
Management
management features paging, swapping, and shared
Features
memory

SWAPPING
Swapping in OS is one of those schemes which fulfil the goal of maximum
utilization of CPU and memory management by swapping in and swapping out
processes from the main memory. Swap-in removes the process from the hard drive
(secondary memory) and swap-out removes the process from RAM (main memory).
Let's suppose several processes like P1, P2, P3, and P4 are ready to be
executed inside the ready queue, and processes P1 and P2 are very memory
consuming so when the processes start executing there may be a scenario where
the memory will not be available for the execution of the process P3 and P4 as there
is a limited amount of memory available for process execution.
Swapping in the operating system is a memory management scheme that
temporarily swaps out an idle or blocked process from the main memory to
secondary memory which ensures proper memory utilization and memory
availability for those processes that are ready to be executed.

Page 74 of 159
Operating System

When that memory-consuming process goes into a termination state means


its execution is over due to which the memory dedicated to their execution becomes
free Then the swapped-out processes are brought back into the main memory and
their execution starts.
The area of the secondary memory where swapped-out processes are stored
is called swap space. The swapping method forms a temporary queue of swapped
processes in the secondary memory. In the case of high-priority processes, the
process with low priority is swapped out of the main memory and stored in swap
space then the process with high priority is swapped into the main memory to be
executed first.
The main goals of an operating system include Maximum utilization of the
CPU. This means that there should be a process execution every time,
the CPU should never stay idle and there should not be any Process
starvation or blocking.

There are two important concepts in the process of swapping which are as follows:
1. Swap In
2. Swap Out
Swap In: The method of removing a process from secondary memory (Hard Drive)
and restoring it to the main memory (RAM) for execution is known as the Swap
In method.
Swap Out: It is a method of bringing out a process from the main memory (RAM)
and sending it to the secondary memory (hard drive) so that the processes with
higher priority or more memory consumption will be executed known as the Swap
Out method.

Advantages of Swapping: The advantages of the swapping method are listed


as follows:
• Swapping in OS helps in achieving the goal of Maximum CPU Utilization.
• Swapping ensures proper memory availability for every process that needs to be
executed.
• Swapping helps avoid the problem of process starvation means a process should
not take much time for execution so that the next process should be executed.

Page 75 of 159
Operating System

• CPU can perform various tasks simultaneously with the help of swapping so that
processes do not have to wait much longer before execution.
• Swapping ensures proper RAM (main memory) utilization.
• Swapping creates a dedicated disk partition in the hard drive for swapped
processes which is called swap space.
• Swapping in OS is an economical process.
• Swapping method can be applied on priority-based process scheduling where
a high-priority process is swapped in and a low-priority process is swapped
out which improves the performance.

Disadvantages of Swapping: There are some limited disadvantages of the


swapping method which are listed as follows:
• If the system deals with power cuts during bulky swapping activity, then the
user may lose all the information that is related to the program.
• If the swapping method uses an algorithm that is not up to the mark, then the
number of page faults can be increased and therefore this decreases the
complete performance.
• There may be inefficiency in a case when there is some common resource used
by the processes that are participating in the swapping process.

MEMORY MANAGEMENT REQUIREMENT


Memory management in OS is the process of regulating and organizing
computer memory to allocate and deallocate memory space efficiently for programs
and applications that require it. This helps to guarantee that the system runs
efficiently and has enough memory to run apps and tasks.

Functions of Memory Management: Some of the major functions fulfilled


by memory management are discussed below.
• Memory Allocation: Memory management ensures that the needed memory
space is allocated to the new process whenever a process is created and requires
memory. Memory Management also keeps track of the system’s allocated and
free memory.
• Memory Deallocation: Like memory allocation, whenever a process completes
its execution, memory management ensures that the space and the memory
resource it holds are released. Any newly created process can use the freed
memory.
• Memory Sharing: Memory sharing is also one of the main goals of memory
management in OS. Some processes might require the same memory
simultaneously. Memory management ensures that this is made possible
without breaking any authorization rules.
• Memory Protection: Memory Protection refers to preventing any unauthorized
memory access to any process. Memory management ensures memory protection
by assigning correct permissions to each process.

Page 76 of 159
Operating System

Why is Memory Management Required


• Memory management allows computer systems to run programs that need more
main memory than the amount of free main memory available on the system. By
moving data between primary and secondary memory, memory management can
be achieved
• Memory management maintains track of the status of every memory location,
whether it is allocated or free.
• It is the responsibility of memory management to protect the memory allocated
to all the processes from being corrupted by other processes. The computer may
exhibit faulty/unexpected behaviour if this is not done.
• Memory management allows sharing of memory spaces between processes, with
the help of which multiple programs can consist at the same memory location.
• Memory management takes care of the system's primary memory by giving
abstraction such that the programs running on the system perceive a large
memory is assigned to them.

Role of Memory Management: Memory management in an operating system


(OS) is crucial in efficiently utilizing the computer's memory resources. It's
responsible for allocating and deallocating memory to different processes and
ensuring they don't interfere with each other. Memory management keeps track of
which parts of memory are in use and which are available, preventing conflicts and
crashes. It enables processes to share memory and optimizes memory usage to
avoid wastage. Proper memory management ensures that applications run
smoothly, preventing crashes due to memory exhaustion and enabling seamless
multitasking, making the computer more reliable and efficient.

DYNAMIC LOADING
The process of getting a program from secondary storage (hard disk) to the
main memory (RAM) is known as loading. In simple words, loading loads the
program in the main memory.
The entire program and all data of a process must be in physical memory for
the process to execute. The size of a process is thus limited to the size of physical
memory. To obtain better memory-space utilization, we can use dynamic loading.
With dynamic loading, a routine is not loaded until it is called. All routines are kept
on disk in a relocatable load format. The main program is loaded into memory and
is executed. When a routine needs to call another routine, the calling routine first
checks to see whether the other routine has been loaded. If not, the relocatable
linking loader is called to load the desired routine into memory and to update the
program's address tables to reflect this change. Then control is passed to the newly
loaded routine.
The advantage of dynamic loading is that an unused routine is never loaded.
This method is particularly useful when large amounts of code are needed to
handle infrequently occurring cases, such as error routines. In this case, although
the total program size may be large, the portion that is used (and hence loaded)
may be much smaller.

Page 77 of 159
Operating System

Dynamic loading does not require special support from the operating system.
It is the responsibility of the users to design their programs to take advantage of
such a method. Operating systems may help the programmer, however, by
providing library routines to implement dynamic loading.

DYNAMIC LINKING
Linking all the required modules of the program to continue the program
execution is known as linking. It takes the object code through an assembler and
combines them to make an executable module.

Dynamic linking in an operating system refers to the process of linking


program modules at runtime rather than at compile time. In dynamic linking, the
system loads and links the required libraries or functions when a program is
executed, instead of embedding all the library code into the executable during
compilation. This offers several benefits, such as reduced memory usage, easier
updates, and modular software development.

Working of Dynamic Linking:


Executable and Libraries Separation: The program is compiled without
directly including the library code in the executable. Instead, it includes references
to the shared libraries. These libraries are usually shared object files (.so in
Unix/Linux or .dll in Windows) that are loaded at runtime.
Loader and Linker at Runtime: When the program is launched, the operating
system’s dynamic linker/loader (like ld.so in Unix/Linux) takes over. It identifies
the required shared libraries and loads them into memory if they aren’t already
loaded. The addresses of the required functions or symbols from these libraries are
then resolved (i.e., mapped into the program's memory space).
Shared Libraries: Multiple programs can share the same library in memory. This
reduces redundancy and memory usage since the shared library code is loaded
only once and reused by multiple applications.

Advantages:
• Memory Efficiency: Programs don't need to include full copies of libraries,
leading to smaller executables and efficient use of memory.
• Upgradability: Libraries can be updated independently of the programs. A bug
fix or update to the library will affect all programs that use it without needing
recompilation.

Page 78 of 159
Operating System

• Modularity: Programs can be divided into modules, which can be dynamically


loaded as needed.

Disadvantages:
• Runtime Overhead: Loading libraries and resolving symbols at runtime
introduces a slight performance cost.
• Dependency Issues: If a required shared library is missing or incompatible, the
program might fail to execute properly. This is often referred to as "DLL hell" in
Windows.

MEMORY ALLOCATION METHOD


Two types of memory allocation techniques operating systems use:
contiguous and non-contiguous memory allocation. In contiguous memory
allocation, memory is assigned to a process in a contiguous block. In non-
contiguous memory allocation, memory is assigned to a process in non-adjacent
blocks.

Contiguous Memory Allocation: Contiguous memory allocation is a


technique where the operating system allocates a contiguous block of memory to a
process. This memory is allocated in a single, continuous chunk, making it easy for
the operating system to manage and for the process to access the memory.
Contiguous memory allocation is suitable for systems with limited memory sizes
and where fast memory access is important. Contiguous memory allocation can be
done in two ways.
• Fixed Partitioning: In fixed partitioning, the memory is divided into fixed-size
partitions, and each partition is assigned to a process. This technique is easy to
implement but can result in wasted memory if a process does not fit perfectly
into a partition.
• Dynamic Partitioning: In dynamic partitioning, the memory is divided into
variable-size partitions, and each partition is assigned to a process. This
technique is more efficient as it allows the allocation of only the required
memory to the process, but it requires more overhead to keep track of the
available memory.

Advantages of Contiguous Memory Allocation


• Simplicity: Contiguous memory allocation is a relatively simple technique for
memory management. It requires less overhead and is easy to implement.
• Efficiency: Contiguous memory allocation is an efficient technique for memory
management. Once a process is allocated contiguous memory, it can access the
entire memory block without any interruption.
• Low fragmentation: Since the memory is allocated in contiguous blocks, there
is a lower risk of memory fragmentation. This can result in better memory
utilization, as there is less memory wastage.

Disadvantages of Contiguous Memory Allocation

Page 79 of 159
Operating System

• Limited flexibility: Contiguous memory allocation is not very flexible as it


requires memory to be allocated in a contiguous block. This can limit the
amount of memory that can be allocated to a process.
• Memory wastage: If a process requires a memory size that is smaller than the
contiguous block allocated to it, there may be unused memory, resulting in
memory wastage.
• Difficulty in managing larger memory sizes: As the size of memory increases,
managing contiguous memory allocation becomes more difficult. This is because
finding a contiguous block of memory that is large enough to allocate to a
process becomes challenging.
• External Fragmentation: Over time, external fragmentation may occur as a
result of memory allocation and deallocation, which may result in non −
contiguous blocks of free memory scattered throughout the system.

Non-contiguous Memory Allocation: Non-contiguous memory


allocation, on the other hand, is a technique where the operating system allocates
memory to a process in non-contiguous blocks. The blocks of memory allocated to
the process need not be contiguous, and the operating system keeps track of the
various blocks allocated to the process. Non-contiguous memory allocation is
suitable for larger memory sizes and where efficient use of memory is important.
Non-contiguous memory allocation can be done in two ways.
• Paging: In paging, the memory is divided into fixed-size pages, and each page is
assigned to a process. This technique is more efficient as it allows the allocation
of only the required memory to the process.
• Segmentation: In segmentation, the memory is divided into variable-sized
segments, and each segment is assigned to a process. This technique is more
flexible than paging but requires more overhead to keep track of the allocated
segments.
Non-contiguous memory allocation is a memory management technique that
divides memory into non-contiguous blocks, allowing processes to allocate memory
that is not necessarily contiguous.

Advantages of Non-Contiguous Memory Allocation


• Reduced External Fragmentation: One of the main advantages of non-
contiguous memory allocation is that it can reduce external fragmentation, as
memory can be allocated in small, non-contiguous blocks.
• Increased Memory Utilization: Non-contiguous memory allocation allows for
more efficient use of memory, as small gaps in memory can be filled with
processes that need less memory.
• Flexibility: This technique allows for more flexibility in allocating and
deallocating memory, as processes can be allocated memory that is not
necessarily contiguous.
• Memory Sharing: Non-contiguous memory allocation makes it easier to share
memory between multiple processes, as memory can be allocated in non-
contiguous blocks that can be shared between multiple processes.

Disadvantages of Non-Contiguous Memory Allocation

Page 80 of 159
Operating System

• Internal Fragmentation: One of the main disadvantages of non-contiguous


memory allocation is that it can lead to internal fragmentation, as memory can
be allocated in small, non-contiguous blocks that are not fully utilized.
• Increased Overhead: This technique requires more overhead than contiguous
memory allocation, as the operating system needs to maintain data structures to
track memory allocation.
• Slower Access: Memory access can be slower than contiguous memory
allocation, as memory can be allocated in non-contiguous blocks that may
require additional steps to access.

SINGLE PARTITION ALLOCATION


The multiprogramming concept emphasizes maximizing CPU utilization by
overlapping CPU and I/O. Memory may be allocated as:
• Single large partition for processes to use or
• Multiple partitions with a single process using a single partition.
This approach keeps the Operating System in the lower part of the memory
and other user processes in the upper part. With this scheme, the Operating
System can be protected from updating in user processes. Relocation-register
scheme known as dynamic relocation is useful for this purpose. It not only protects
user processes from each other but also from changing OS code and data. Two
registers are used: the relocation register, which contains the value of the smallest
physical address, and the limit register, which contains the logical address range.
Both these are set by the Operating System when the job starts. At load time of the
program (i.e., when it has to be relocated) we must establish “addressability” by
adjusting the relocation register contents to the new starting address for the
program. The scheme is shown in Figure below.
The contents of a relocation register are implicitly added to any address
references generated by the program. Some systems use base registers as
relocation registers for easy addressability as these are within the programmer’s
control. Also, in some systems, relocation is managed and accessed by the
Operating System only.

0
R + Yes Is A
CPU
Physical Relocation A<L
Address register

L Error in Addressing
Max Limit
Register

In a dynamic relocation scheme if the logical address space range is 0 to


Max, then the physical address space range is R + 0 to R + Max (where R is
relocation register contents). Similarly, a limit register is checked by H/W to be

Page 81 of 159
Operating System

sure that the logical address generated by the CPU is not bigger than the program's
size.

MULTIPLE PARTITIONS
This is also known as a static partitioning scheme as shown in the following
Figure. A simple memory management scheme divides memory into n (possibly
unequal) fixed-sized partitions, each of which can hold exactly one process. The
degree of multiprogramming is dependent on the number of partitions. IBM used
this scheme for systems 360 OS/MFT (Multiprogramming with a fixed number of
tasks). The partition boundaries are not movable (must reboot to move a job). We
can have one queue per partition or just a single queue for all the partitions.
d
Partition 3
c
Partition 2
Multiple Job Queues b
Partition 1

a
Partition 1
0
Multiple Partition System
Initially, whole memory is available for user processes and is like a large
block of available memory. The operating system keeps details of available memory
blocks and occupied blocks in tabular form. OS also keeps track of the memory
requirements of each process. As processes enter into the input queue and when
sufficient space for it is available, the process is allocated space and loaded. After
its execution is over it releases its occupied space and OS fills this space with other
processes in the input queue. The block of available memory is known as a Hole.
Holes of various sizes are scattered throughout the memory. When any process
arrives, it is allocated memory from a hole that is large enough to accommodate it.
This example is shown in the Figure given below:

OS OS OS
200K
Process A (50K) Process A (50K) Process A (50K)
Partition 1
(100K) Hole (50K) Process B Hole (50K) Process D Hole (50K)

Partition 2 Process B (50K) Terminated Process B (50K) Arrives Process B (50K)


(200K) Hole (50K) Hole (50K) Hole (50K)

Process C (50K) Process C (50K) Process C (50K)


Partition 3
(500K) Hole (50K) Hole (50K) Hole (50K)
Fixed-sized Partition System
If a hole is too large, it is divided into two parts:
1) One that is allocated to the next process of the input queue
2) Added with set of holes.

Page 82 of 159
Operating System

Within a partition, if two holes are adjacent then they can be merged to
make a single large hole. However, this scheme suffers from a fragmentation
problem. Storage fragmentation occurs either because the user processes do not
completely accommodate the allotted partition or partition remains unused, if it is
too small to hold any process from the input queue. Main memory utilization is
extremely inefficient. Any program, no matter how small, occupies the entire
partition. In our example, process B takes 150K of partition2 (200K size). We are
left with a 50 K-sized hole. This phenomenon, in which there is wasted space
internal to a partition, is known as internal fragmentation. It occurs because the
initial process is loaded in a partition that is large enough to hold it (i.e., allocated
memory may be slightly larger than requested memory). “Internal” here means
memory that is internal to a partition, but is not in use.

Variable-sized Partition: This scheme is also known as dynamic partitioning.


In this scheme, boundaries are not fixed. Processes accommodate memory
according to their requirement. There is no wastage as the partition size is the
same as the size of the user process. Initially, when processes start this wastage
can be avoided but later on when they terminate, they leave holes in the main
storage. Other processes can accommodate these, but eventually, they become too
small to accommodate new jobs as shown in Figure 8.

OS OS OS

Process A Process A Hole Process D Process D

Terminated Arrives
Process B Process B Process B

Process C Process C Process C

IBM used this technique for OS/MVT (Multiprogramming with a Variable


Number of Tasks) as the partitions are of variable length and number. But still,
fragmentation anomaly exists in this scheme. As time goes on and processes are
loaded and removed from memory, fragmentation increases, and memory
utilization declines. This wastage of memory, which is external to partition, is
known as external fragmentation. In this, though there is enough total memory to
satisfy a request but as it is not contiguous and it is fragmented into small holes,
that can’t be utilised.

COMPACTION
Compaction is a memory management technique in which the free space of a
running system is compacted, to reduce fragmentation problems and improve
memory allocation efficiency. Compaction is used by many modern operating
systems, such as Windows, Linux, and Mac OS X. As in the fig we have some used
memory (black color) and some unused memory (white color). The used memory is
combined. All the empty spaces are combined. This process is called compaction.
This is done to prevent and solve the problem of fragmentation, but it requires too
much CPU time.

Page 83 of 159
Operating System

By compacting memory, the operating system can reduce or eliminate


fragmentation and make it easier for programs to allocate and use memory.
The compaction process usually consists of two steps:
• Copying all pages that are not in use to one large contiguous area.
• Then, write the pages that are in use into the newly freed space.

In compaction, all the holes are contiguous, and OS combines all the loaded
processes in different partitions. Now, merged holes can accommodate new
processes according to their needs. This method is also known as de-
fragmentation. Let us explain through the diagram.
At the time of compaction, the CPU stops the execution of the current
process because it will resume the process from somewhere else after compaction.
If the CPU does not stop the execution of a process, then it may execute the
instructions from somewhere else locations instead of the next instruction of the
same process in memory.
Advantages of Compaction
• Reduces external fragmentation.
• Make memory usage efficient.
• Memory becomes contiguous.
• Since memory becomes contiguous more processes can be loaded to
memory, thereby increasing the scalability of OS.
• Fragmentation of the file system can be temporarily removed by compaction.
• Improves memory utilization as there is less gap between memory blocks.
Disadvantages of Compaction
• System efficiency is reduced and latency is increased.
• A huge amount of time is wasted in performing compaction.
• CPU sits idle for a long time.
• Not always easy to perform compaction.
• It may cause deadlocks since it disturbs the memory allocation process.

RELOCATION

Page 84 of 159
Operating System

Operating systems can manage where each process is stored in memory


using a technique called relocation. The operating system is often stored in the
highest memory addresses.
When the program compiles and executes, it starts with zero (0) physical
address. The maximum address is equal to the total memory size minus the
operating system size. The process is loaded and allocated a contiguous segment of
memory. The first (smallest) physical address of the process is the base address
and the largest physical address the process can access is the limit address. There
are two methods of relocation:
Static Relocation: The operating system adjusts the memory address of a
process to reflect its starting position in memory. Once a process is assigned a
starting position in memory, it executes within the space it has been allocated.
Once the static relocation process has been completed, the operating system can
no longer relocate the process until it terminates. It is used by the IBM 360 as a
stop-gap solution. It is a slow and complicated process.
Dynamic Relocation: In this, the hardware adds a relocation register (base
value) to the virtual address generated by the compiler. The relocation register
allows translation to a physical memory address.

PAGING
Paging is a memory management technique for retrieving processes from
secondary memory storage units as pages and stored in the main memory’s frame.
When a program needs to access data, it sends a request to access a process
to the operating system, which stores the process in the main memory from the
secondary memory. Each process is divided into small fixed-sized chunks
called pages; similarly, the main memory will be divided into equal-fixed-sized
pieces called frames. The process pages are stored at a different location in the
main memory. The thing to note here is that the size of the page and frame will be
the same.

Like in the above fig, every page in secondary memory is 2 Kb, and in the
same way, every frame in main memory is also 2 Kb.
The problem is that physical memory is finite. When all of the spaces in
physical memory are filled with requests, the operating system has to start
swapping the processes that are not in use to make room for new ones. This
process is called swapping.

Page 85 of 159
Operating System

We have two types of addresses.


1. Logical address: An address generated by the CPU is commonly referred to as
a logical address. A set of logical addresses generated by a program is a logical
address space.
2. Physical address: An address seen by the memory unit - that is, the one loaded
into the memory address register-is commonly referred to as a physical address.
A set of physical addresses corresponding to these logical addresses is physical
address space.

Advantages of Paging
• Conserve memory by only keeping the active pages in memory. This is especially
helpful in large-scale systems where memory is scarce.
• Enables the operating system to manage more processes by allowing each
process to have its dedicated memory space. This maximizes efficiency and
performance by allowing the operating system to schedule and run each process
without conflicts.
• Allows for greater flexibility and scalability regarding the size and complexity of
the systems that can be created.
• Parts of the program are allowed to be stored at different locations in the main
memory.
• It solves the problem of external fragmentation.
• Swapping becomes very easy due to equal-sized pages and frames.

Disadvantages of Paging
• It can be very inefficient. When a process needs more memory, the operating
system must find a block of unused memory and copy it to the process.
• This process can take a long time and, in some cases, can even crash the
system.
• Paging can cause internal fragmentation, which makes the system run more
slowly.
• The page table is there, which takes some memory space.
• Have to maintain a page table for each process.
• Memory access time increases as the page table needs to be accessed.

When to Use Paging


• You have too many processes running and not enough physical memory to store
them all.
• When you have a large process that can be split into multiple pages.
• When you want to load a process into physical memory without loading the
entire process.

SEGMENTATION

Page 86 of 159
Operating System

Segmentation divides processes into smaller subparts known as modules.


The divided segments need not be placed in contiguous memory. Since there is no
contiguous memory allocation, internal fragmentation does not take place. The
length of the segments of the program and memory is decided by the purpose of the
segment in the user program.
We can say that logical address space or the main memory is a collection of
segments.

Types of Segmentation: Segmentation can be divided into two types:

1. Virtual Memory Segmentation: Virtual Memory Segmentation divides the


processes into n number of segments. All the segments are not divided at a time.
Virtual Memory Segmentation may or may not take place at the run time of a
program.
2. Simple Segmentation: Simple Segmentation also divides the processes
into n number of segments but the segmentation is done all together at once.
Simple segmentation takes place at the run time of a program. Simple
segmentation may scatter the segments into the memory such that one segment
of the process can be at a different location than the other (in a noncontinuous
manner).

Why Segmentation is required? Segmentation came into existence because


of the problems with the paging technique. In the case of the paging technique, a
function or piece of code is divided into pages without considering that the relative
parts of code can also get divided. Hence, for the process in execution, the CPU
must load more than one page into the frames so that the complete related code is
there for execution. Paging took more pages for a process to be loaded into the
main memory. Hence, segmentation was introduced in which the code is divided
into modules so that related code can be combined in one single block.
Other memory management techniques have also an important drawback -
the actual view of physical memory is separated from the user's view of physical

Page 87 of 159
Operating System

memory. Segmentation helps in overcoming the problem by dividing the user's


program into segments according to the specific need.

Advantages of Segmentation in OS
• No internal fragmentation is there in segmentation.
• Segment Table is used to store the records of the segments. The segment table
itself consumes less memory as compared to a page table in paging.
• Segmentation provides better CPU utilization as an entire module is loaded at
once.
• Segmentation is near to the user's view of physical memory. Segmentation allows
users to partition the user programs into modules. These modules are nothing
but the independent codes of the current process.
• The Segment size is specified by the user but in Paging, the hardware decides
the page size.
• Segmentation can be used to separate the security procedures and data.

Disadvantages of Segmentation in OS
• During the swapping of processes, the free memory space is broken into small
pieces, which is a major problem in the segmentation technique.
• Time is required to fetch instructions or segments.
• The swapping of segments of unequal sizes is not easy.
• There is an overhead of maintaining a segment table for each process as well.
• When a process is completed, it is removed from the main memory. After the
execution of the current process, the unevenly sized segments of the process are
removed from the main memory. Since the segments are of uneven length it
creates unevenly sized holes in the main memory. These holes in the main
memory may remain unused due to their very small size.

Characteristics of Segmentation in OS: Some of the characteristics of


segmentation are discussed below:
• Segmentation partitions the program into variable-sized blocks or segments.
• Partition size depends upon the type and length of modules.
• Segmentation is done considering that the relative data should come in a
single segment.
• Segments of the memory may or may not be stored continuously depending
upon the segmentation technique chosen.
• The Operating System maintains a segment table for each process.

Example of Segmentation: Let's take the example of segmentation to


understand how it works.
Let us assume we have five segments namely: Segment-0, Segment-1,
Segment-2, Segment-3, and Segment-4. Initially, before the execution of the
process, all the segments of the process are stored in the physical memory space.
We have a segment table as well. The segment table contains the beginning entry
address of each segment (denoted by base). The segment table also contains the
length of each of the segments (denoted by limit).

Page 88 of 159
Operating System

As shown in the image below, the base address of Segment-0 is 1400 and its
length is 1000, the base address of Segment-1 is 6300 and its length is 400, the
base address of Segment-2 is 4300 and its length is 400, and so on.
The pictorial representation of the above segmentation with its segment table
is shown below.

SEGMENTATION WITH PAGING


Paged segmentation and segmented paging are two memory management
techniques that offer the advantages of both paging and segmentation A memory
management technique known as Segmentation with Paging in OS combines the
benefits of both segmentation and paging.
The main memory is split into variable-size segments, which are
subsequently partitioned into segmentation with paging in OS. Each segment has a
page table, and each process has many page tables.
Each table includes information for each segment page, whereas the
segment table has information for each segment. Page tables are linked to segment
tables and segment tables to individual pages within a segment.

Segmented Paging: Segmented paging is a way to manage computer memory


that breaks it into small chunks called segments, and within each segment, there
are fixed-sized pages. It's like dividing a big book into chapters, and each chapter
into pages, to make it easier to read and manage.
Pages are created from segments. Implementation necessitates STR (segment
table register) and PMT (page map table). Each virtual address in this method
consists of a segment number, a page number, and an offset within that page. The
segment number indexes into the segment table, which returns the page table's
base address for that segment.

Page 89 of 159
Operating System

The page number is an index into the page table, each item of which
represents a page frame. The physical address is obtained by adding the PFN (page
frame number) and the offset. As a result, addressing may be defined by the
function:
va = (s,p,d)
here,
va is the virtual address,
s determines the number of segments (size of ST),
p determines the number of pages per segment (size of PT),
d determines page size.

Advantages of Segmented Paging: The benefits of segmented paging are as


follows:
• Each segment is represented by a single entry in the segment table. It lowers
memory use
• The segment size determines the size of the Page Table
• It reduces the issue of external fragmentation

Disadvantages of Segmented Paging: The downsides of segmented paging are


as follows:
• Internal fragmentation plagues segmented paging
• When compared to paging, the complexity level is substantially higher
• Managing both segmentation and paging tables increases overhead
• Require additional effort to design and implement

Paged Segmentation: Paged Segmentation is used to solve the problem


segmented paging has. In this, every process has a different number of segments.
When the segment table is large, it causes external fragmentation due to varying
segment table sizes. So to solve this, the process requires the segment table to be
paged. Also, the page table can have invalid pages even after Segmentation paging.
So paged Segmentation is used instead of multi-level paging along with segmented
paging, giving the optimum solution to the larger tables.

Page 90 of 159
Operating System

Advantages of Paged Segmentation


• It offers protection in specific segments
• It uses less memory than paging
• There is no external fragmentation
• It optimizes resource allocation
• Easiness in adding or removing segments and pages
• It also facilitates multi-programming
• It supports large programs
Disadvantages of Paged Segmentation
• It is costly
• In the case of swapping, segments with different sizes are not good
• Programmer intervention is required
• introduce additional overhead in terms of memory and processing
• adds complexity to memory management
• fragmentation can still occur over time
• The learning curve is steep

Difference Between Paging and Segmentation


Paging Segmentation
In paging, the program is divided into In segmentation, the program is divided
fixed or mounted-size pages. into variable-size segments.
For the paging, the operating system is For segmentation, the compiler is
accountable. accountable.
Here, the segment size is given by the
Page size is determined by hardware.
user.

It is faster in comparison to Segmentation is slow.

Page 91 of 159
Operating System

segmentation.
Paging could result in internal Segmentation could result in external
fragmentation. fragmentation.
In paging, the logical address is split Here, the logical address is split into
into a page number and a page offset. segment number and segment offset.
Paging comprises a page table that Segmentation also comprises the
encloses the base address of every segment table which encloses the
page. segment number and segment offset.
In segmentation, the operating system
In paging, the operating system must
maintains a list of holes in the main
maintain a free frame list.
memory.
Paging is invisible to the user. Segmentation is visible to the user.
In paging, the processor needs the page In segmentation, the processor uses
number, and offset to calculate the segment number, and offset to calculate
absolute address. the full address.
It is hard to allow the sharing of Facilitates sharing of procedures
procedures between processes. between the processes.
n paging, a programmer cannot It can efficiently handle data
efficiently handle data structure. structures.
Easy to apply for protection in
This protection is hard to apply.
segmentation.
The size of the page needs to always be There is no constraint on the size of
equal to the size of the frames. segments.
A page is referred to as a physical unit A segment is referred to as a logical
of information. unit of information.
Segmentation results in a more efficient
Paging results in a less efficient system.
system.

PROTECTION
When several users share computer resources such as CPU, memory, and
other resources, security is more crucial. It is the job of the operating system to
provide a mechanism that protects each process from other processes. All assets
that require protection in a multiuser environment are categorized as objects, and
individuals who seek to access these things are referred to as subjects. Distinct
'access privileges are granted to different subjects by the operating system.
Protection is a method that limits the access of programs, processes, or
users to the resources defined by a computer system. Protection can be used to
allow several users to safely share a common logical namespace, such as a
directory or files, in multi-programming operating systems. It necessitates the
safeguarding of computer resources such as software, memory, and processors. As
assistance to multiprogramming OS, users should apply protective steps so that

Page 92 of 159
Operating System

several users can safely access a common logical namespace like a directory or
data. Maintaining secrecy, honesty, and availability in the OS might provide
protection. The device must be protected against unauthorized access, viruses,
worms, and other malware.

Why is it important? There could be security issues such as illegal reading,


writing, or modification, or the system failing to function properly for authorized
users. It helps protect data, processes, and programs from unauthorized user or
program access. It is critical to guarantee that there are no breaches in access
permissions, malware, or illegal access to existing data. Its goal is to ensure that
only the policies of the systems have access to programs, resources, and data.

Need of protection OS
• Isolation: Protection OS ensures isolation between different processes and
users, preventing unauthorized access to resources.
• Security: It protects system resources, such as memory and files, from
unauthorized access, modification, or corruption.
• Stability: Protection OS enhances system stability by preventing one process
from interfering with or crashing other processes.
• Fairness: It ensures fair resource allocation among competing processes,
preventing one process from monopolizing system resources.

Goals of Protection: Protection mechanisms in an operating system serve


several goals:
• Confidentiality: Ensuring that sensitive information is accessible only to
authorized users or processes.
• Integrity: Guaranteeing that data remains unaltered and trustworthy
throughout its lifecycle.
• Availability: Ensuring that resources and services are available and accessible
to authorized users when needed.
• Isolation: Preventing interference and unauthorized access between different
processes, users, or components.
• Auditability: Providing mechanisms for tracking and monitoring system
activities to detect and investigate security incidents or violations.

FILE MANAGEMENT SYSTEM


Files is a collection of co-related information that is recorded in some format
(such as text, pdf, docs, etc.) and is stored on various storage mediums such as
flash drives, hard disk drives (HDD), magnetic tapes, optical disks, and tapes, etc.
Files can be read-only or read-write. Files are simply used as a medium for
providing input(s) and getting output(s).
So, file management is one of the basic but important features provided by
the operating system. File management in the operating system is nothing but

Page 93 of 159
Operating System

software that handles or manages the files (binary, text, pdf, docs, audio, video,
etc.) present in computer software. The file system in the operating system is
capable of managing individual as well as groups of files present in the computer
system.
The file management in the operating system manages all the files present in
the computer system with various extensions (such as .exe, .pdf, .txt, .docx, etc.)
We can also use the file system in the operating system to get details of any file(s)
present on our system. The details can be:
• location of the file (the logical location where the file is stored in the computer
system)
• the owner of the file (who can read or write on the particular file)
• when was the file created (time of file creation and modification time)
• a type of file (format of the file for example text, pdfs, docs, etc.)
• state of completion of the file, etc.
For file management in the operating system or to make the operating
system understand a file, the file must be in a predefined structure or format.
There are three types of file structures present in the operating systems:
1. text file: A text file is a non-executable file containing a sequence of numbers,
symbols, and letters organized in the form of lines.
2. source file: A source file is an executable file that contains a series of functions
and processes. In simple terms, we can say that a source file is a file that
contains the instructions of a program.
3. object file: An object file is a file that contains object codes in the form of
assembling language code or machine language code. In simple terms, we can
say that object files contain program instructions in the form of a series of
bytes and are organized in the form of blocks.

Functions of File Management in Operating System: Now let us talk


about some of the most important functions of file management in operating
systems.
• Allows users to create, modify, and delete files on the computer system.
• Manages the locations of files present on the secondary memory or primary
memory.
• Manages and handles the permissions of a particular file for various users and
groups.
• Organize the files in a tree-like structure for better visualization.
• Provides interface to I/O operations.
• Secures files from unauthorized access and hackers.

Objectives of File Management in Operating Systems: In the last


section, we gained a good basic understanding of files, operating systems, and file
management in operating systems. Let us now learn some of the objectives of file
management in operating systems.

Page 94 of 159
Operating System

• The file management in the operating system allows users to create a new file,
and modify and delete the old files present at different locations of the computer
system.
• The operating system file management software manages the locations of the file
store so that files can be extracted easily.
• As we know, process shares files so, one of the most important features of file
management in operating systems is to make files sharable between processes. It
helps the various processes to securely access the required information from a
file.
• The operating system file management software also manages the files so that
there is very little chance of data loss or data destruction.
• The file management in the operating system provides input-output operation
support to the files so that the data can be written, read, or extracted from the
file(s).
• It also provides a standard input-output interface for the user and system
processes. The simple interface allows the easy and fast modification of data.
• The file management in the operating system also manages the various user
permissions present on a file. There are three user permissions provided by the
operating system, they are: read, write, and execute.
• The file management in the operating system supports various types of storage
devices such as flash drives, hard disk drives (HDD), magnetic tapes, optical
disks, tapes, etc., and it also allows the user(s) to store and extract them
conveniently.
• It also organizes the files in a hierarchal manner in the form of files and folders
(directories) so that management of these files can be easier from the user's
perspective as well. Refer to the diagram below for better visualization.

Advantages of File Management in OS: Some of the main advantages that


the file system in the operating system provides are:
• Protection of the files from unauthorized access.
• Recovers the free space created when files are removed or deleted from the hard
disk.
• Assigns the disk space to various files with the help of disk management
software of the operating system.
• As we know, a file may be stored at various locations in the form of segments so
the file management in the operating system also keeps track of all the blocks or
segments of a particular file.
• Helps to manage the various user permissions so that only authorized persons
can perform the file modifications.
• It also keeps our files secure from hackers with the help of security management
in the operating system.

Disadvantages of File Management in OS: Some of the main advantages


that the file system in the operating system provides are:
• If the size of the files becomes large then the management takes a good amount
of time due to hierarchical order.

Page 95 of 159
Operating System

• To get more advanced management features, we need an advanced version of the


file management system. One of the advanced features is the document
management feature (DMS) which can organize important documents.
• The file system in the operating system can only manage the local files present in
the computer system.
• Security can be an issue sometimes as a virus in a file can spread across various
other files due to a tree-like (hierarchal) structure.
• Due to the hierarchal structure, file accessing can be slow sometimes.

FILE ACCESSING METHODS


A file is a collection of bits/bytes or lines that is stored on secondary storage
devices like a hard drive (magnetic disks).
File access methods in OS are nothing but techniques to read data from the
system's memory. There are various ways in which we can access the files from the
memory like:
• Sequential Access
• Direct/Relative Access, and
• Indexed Sequential Access.
These methods by which the records in a file can be accessed are referred to
as the file access mechanism. Each file access mechanism has its own set of
benefits and drawbacks, which are discussed further in this article.

Types of File Access Methods


Sequential Access: The operating system reads the file word by word in a
sequential access method of file accessing. A pointer is made, which first links to
the file's base address. If the user wishes to read the first word of the file, the
pointer gives it to them and raises its value to the next word. This procedure
continues till the file is finished. It is the most basic way of file access. The data in
the file is evaluated in the order that it appears in the file and that is why it is easy
and simple to access a file's data using a sequential access mechanism. For
example, editors and compilers frequently use this method to check the validity of
the code.

Advantages of Sequential Access:


• The sequential access mechanism is very easy to implement.
• It uses lexicographic order to enable quick access to the next entry.

Disadvantages of Sequential Access:

Page 96 of 159
Operating System

• Sequential access will become slow if the next file record to be retrieved is not
present next to the currently pointed record.
• Adding a new record may need relocating a significant number of records of the
file.

Direct (or Relative) Access: A Direct/Relative file access mechanism is


mostly required with the database systems. In the majority of the circumstances,
we require filtered/specific data from the database, and in such circumstances,
sequential access might be highly inefficient. Assume that each block of storage
holds four records and that the record we want to access is stored in the tenth
block. In such a situation, sequential access will not be used since it will have to
traverse all of the blocks to get to the required record, while direct access will allow
us to access the required record instantly.
The direct access mechanism requires the OS to perform some additional
tasks but eventually leads to much faster retrieval of records as compared to
sequential access.

Advantages of Direct/Relative Access:


• The files can be retrieved right away with a direct access mechanism, reducing
the average access time of a file.
• There is no need to traverse all of the blocks that come before the required block
to access the record.
Disadvantages of Direct/Relative Access:
• The direct access mechanism is typically difficult to implement due to its
complexity.
• Organizations can face security issues as a result of direct access as the users
may access/modify the sensitive information. As a result, additional security
processes must be put in place.

Indexed Sequential Access: It's the other approach to accessing a file


that's constructed on top of the sequential access mechanism. This method is
practically similar to the pointer-to-pointer concept in which we store the address
of a pointer variable containing the address of some other variable/record in
another pointer variable. The indexes, similar to a book's index (pointers), contain a
link to various blocks present in the memory. To locate a record in the file, we first

Page 97 of 159
Operating System

search the indexes and then use the pointer-to-pointer concept to navigate to the
required file.
Primary index blocks contain the links of the secondary inner blocks which contain
links to the data in the memory.

Advantages of Indexed Sequential Access:


• If the index table is appropriately arranged, it accesses the records very quickly.
• Records can be added at any position in the file quickly.

Disadvantages of Indexed Sequential Access:


• When compared to other file access methods, it is costly and less efficient.
• It needs additional storage space.

FILE DIRECTORIES
On a computer, a directory is used to store, arrange, and segregate files and
folders. It is similar to a telephone directory in that it just contains lists of names,
phone numbers, and addresses rather than the real papers. It uses a hierarchical
structure to organize files and directories. On many computers, directories are
referred to as drawers or folders, much like a workbench or a standard filing
cabinet in an office. You may, for instance, create a directory for images and
another for all of your documents. You could easily access the type of file you
wanted to see by saving particular file types in a folder.

Page 98 of 159
Operating System

There are several logical structures of a directory, these are given below.
• Single-level directory
• Two-level directory
• Tree structure or hierarchical directory
• Acyclic graph directory
• General graph directory structure and Data Structure

Single-Level Directory Structure: It is the simplest directory structure. In a


single-level directory, there is only one directory in the system, meaning there is
only one folder, and all the files are stored in that single directory. There is no way
to segregate important files from non-important files.
Implementation of a single-level directory is the simplest. However, there are
various disadvantages to it. The pictorial representation of a single-level directory is
given below. There is only one directory (root directory), and all the files are stored
in the same directory. Here f1, f2, f3, f4, f5 represent the five different files.
Practically it can be thought of as a structure where all the files are stored in the
same folder.

Advantages of single-level directory


• The main advantage of a single-level directory is that it is very simple to
implement.
• Since all the files are present in the same directory, in case the number of files is
less, then searching for a particular file is faster and easier.
• Simple operations like file creation, search, deletion, and updating are possible
with a single-level directory structure.
• The single-level directory is easier to understand in practical life.

Disadvantages of single-level directory


• In case we want to organize the files in some groups, it is not possible to do so
since we cannot create subdirectories.
• Two file names cannot be the same. In case two files are given the same name,
the previous one is overridden.
• If the number of files is very large, searching a particular file is very inefficient.
• Segregation of important and unimportant files is not possible.
• The single-level directory is not useful for multi-user systems.

Two-Level Directory Structure: We saw how the single-level directory


proves to be inefficient if multiple users are accessing the system. If two different

Page 99 of 159
Operating System

users wanted to create a file with the same name (say report.doc), it was not
allowed in a single-level directory.
In a two-level directory structure, there is a master node that has a separate
directory for each user. Each user can store the files in that directory. It can be
practically thought of as a folder that contains many folders, each for a particular
user, and now each user can store files in the allocated directory just like a single-
level directory.
The pictorial representation of a two-level directory is shown below. For
every user, there is a separate directory. At the next level, every directory stores the
files just like a single-level directory. Although not very efficient, the two-level
directory is better than a single-level directory structure.

Advantages of a two-level directory


• Searching is very easy.
• There can be two files with the same name in two different user directories.
Since they are not in the same directory, the same name can be used.
• Grouping is easier.
• A user cannot enter another user’s directory without permission.
• Implementation is easy.
Disadvantages of two-level directory
• One user cannot share a file with another user.
• Even though it allows multiple users, still a user cannot keep two same-type
files in a user directory.
• It does not allow users to create subdirectories.

Tree-Structured Directory Structure: This type of directory is used in our


PCs. The biggest disadvantage of a two-level directory was that one could not create
sub-directories in a directory. The tree-structured directory solved this problem. In
a tree-structured directory, there is a root directory at the peak. The root directory
contains directories for each user. The users can, however, create subdirectories
inside their directory and also store the files.

Page 100 of 159


Operating System

The pictorial representation of a tree-structured directory is shown below.


The root directory is highly secured, and only the system administrator can access
it. We can see how there can be subdirectories inside the user directories. A user
cannot modify the root directory data. Also, a user cannot access another user's
directory.
Advantages of tree-structured directory
• Highly scalable compared to the previous two types of directories.
• Allows subdirectories inside a directory.
• Searching is easy.
• Allows grouping.
• Segregation of important and unimportant files is easy.
Disadvantages of tree-structured directory
• As one user cannot enter another user’s directory, this restricts sharing of
files.
• Too many subdirectories may make the search complicated.
• Users cannot modify the root directory’s data.
• Files might have to be saved in multiple directories in case all of them do not
fit into one.

Acyclic Graph Directory Structure: Suppose there is a file abcd.txt. Out of


the three types of directories we studied above, none of them provide the flexibility
to access the file abcd.txt from multiple directories, i.e., we cannot access a
particular file or subdirectory from two or more directories. The file or the
subdirectory can be accessed only by the directory it is present inside.
The solution to this problem is presented in the acyclic graph directory. In
this type of directory, we can access a file or a subdirectory from multiple
directories. Hence files can be shared between directories. It is designed in such a
way that multiple directories point to a particular directory or file with the help of
links.

Page 101 of 159


Operating System

Advantages of acyclic- graph directory


• Allows sharing of files or subdirectories from more than one directory.
• Searching is very easy.
• Provides more flexibility to the users.
Disadvantages of acyclic-graph directory
• Harder to implement in comparison to the previous three.
• Since the files are accessed from multiple directories, deleting a file may
cause some errors if the user is not cautious.
• If the files are linked by a hard link, then it is necessary to delete all the
references to that file to permanently delete the file.

General-Graph Directory Structure: This is an extension to the acyclic-


graph directory. In the general-graph directory, there can be a cycle inside a
directory.

In the above image, we can see that a cycle is formed in the user 2 directory.
Although it provides greater flexibility, it is complex to implement this structure.
Advantages of General-graph directory
• Compared to the others, the General-Graph directory structure is more
flexible.
• Cycles are allowed in the directory for general graphs.

Page 102 of 159


Operating System

Disadvantages of General-graph directory


• It costs more than alternative solutions.
• Garbage collection is an essential step here.

FILE ALLOCATION METHODS


When a hard drive is formatted, a system has numerous tiny spaces to store
files called blocks. The operating system stores data in memory blocks using
various file allocation methods, which enables the hard disk to be used efficiently
and the file to be accessed.

Types of File Allocation Methods


• Contiguous File Allocation
• Linked File Allocation
• Indexed File Allocation
• File Allocation Table (FAT)
• Inode

Contiguous File Allocation: In the contiguous allocation methods, the files


are allocated the disk blocks in a contiguous manner, meaning that if a file's
starting disk block address is x, then it will be allocated the blocks with address
x+1, x+2, x+3,……., provided the blocks are not already occupied.
Suppose a file abcd.doc has a starting disk block address of 2, and it
requires four such blocks; hence in the contiguous allocation method, the file will
be allocated the disk blocks with the addresses 2, 3, 4, and 5.

The operating system also maintains a directory table that includes the file
name along with the starting address and the length of the blocks allocated. The
length represents the number of disk blocks required by the file.
In the above figure, it can be seen that the file "os.pdf" requires four disk
blocks and its starting disk block address is 1. Therefore, the blocks allocated to
the file are 1, 2, 3, and 4. Similarly, for “dbms.doc” the blocks allocated are 7, 8,
and 9 since its starting address is 7 and length is 3.
Advantages of contiguous file allocation

Page 103 of 159


Operating System

• Since the blocks are allocated in sequential order, therefore it can be accessed
sequentially since the starting address and the length are already available in
the directory table.
• The block allocation is similar to the array. Given the starting address, we can
"jump" to any block address by simply adding the block size to the starting
address, just as we do while accessing any block in an array. Hence the
contiguous allocation also allows random access to the blocks.
• The seek time is less because of the contiguous allocation of blocks. This makes
it very fast.
Disadvantages of contiguous file allocation
• It suffers from internal fragmentation. Suppose the size of a block is 2KB, but
the file that has to be stored is just 1 KB. In that case, an extra 1KB remains
unutilized and the memory is wasted.
• It suffers from external fragmentation. If there are sufficient blocks available to
store a file, but if they are not contiguous, the file cannot be stored.
• The size of the file can be increased only if free contiguous disk blocks are
available.
• Although it allows fast access, memory management is poor.

Linked File Allocation: The linked allocation works just like the linked list.
The problem with contiguous allocation was that memory remained unutilized due
to external fragmentation. The solution to the problem was to allocate the disk
block in the form of a linked list where every block was a node.
The blocks are allocated in such a way that every block contains a pointer to
the next block that is allocated to the file.

The above image shows how the linked allocation works. The file "os.pdf" has
to be allocated some blocks. The first block allocated is 4. Block 4 will have a
pointer to block 8, block 8 will have a pointer to block 10, block 10 will have a
pointer to block 11, block 11 will have a pointer to block 2, and finally, block 2 will
point to 3. In this manner, a total of six blocks are allocated to the file non-
contiguously. The ending block (block 3) will not point to any other block.
Advantages of linked file allocation
• There is no external fragmentation because blocks can be allocated in random
order with the help of pointers. Contiguous allocation is not required.

Page 104 of 159


Operating System

• File size can be increased, even if the contiguous blocks are not available,
provided there are enough blocks to store the file.
• Judicious use of memory.

Disadvantages of linked file allocation


• Random access is not allowed since the memory allocation is not contiguous.
Therefore, with the help of the starting block address, we cannot directly jump to
some other block, just as we cannot jump to any node in the linked list with just
the head pointer.
• It is relatively slow because of more seek time.
• Every block needs to contain extra information about the pointer to the next
block.

Indexed File Allocation: Although linked allocation efficiently used the


memory, it was slower than contiguous allocation. There was a need for an
allocation method such that the disk blocks are used properly and also the access
time is less. This is where indexed allocation comes to the rescue. It can be thought
of as a mixture of linked and contiguous allocation that is more efficient in terms of
space and time.
There are index blocks that contain pointers to the blocks occupied by the
file. Thus every file has its index block, which is a disk block, but instead of storing
the file itself, it contains the pointers to other blocks that store the file. This helps
in randomly accessing the files and also avoiding external fragmentation.

From the above image, we can see that block number 8 does not store the
file but contains the pointers to various other blocks, which store the file. The
directory table contains only the file name and the index block for the respective
files.
Below is the pictorial representation of index block 8, which contains the
pointers that determine the address of the blocks that store the "os.pdf" file.

Page 105 of 159


Operating System

Since the size of every block is limited, there will be problems if the numbers
of pointers to other blocks are very large in number such that a block is not
sufficient to store it.
Advantages of Indexed allocation
• No external fragmentation.
• Allows random access to disk blocks.
• Allows direct access, reducing complexity.
Disadvantages of Indexed allocation
• It is very complex.
• Extra memory for index blocks.
• Large pointer overhead.
• For very large files, a single index block may not be able to hold all the
pointers.

File Allocation Table (FAT): The File Allocation Table (FAT) is a file system
format commonly used in older computer systems and removable storage devices.
It organizes data storage by maintaining a table that tracks the allocation status of
individual file clusters on a storage medium. While less common today, FAT was
instrumental in early computing, providing a straightforward way to manage files
and directories.
Advantages of File Allocation Table (FAT)
• FAT is widely supported across different operating systems and devices, making
it easy to share data between various platforms without compatibility issues.
• FAT's straightforward structure makes it relatively easy to implement and
understand, which was particularly advantageous in the early days of
computing.
• The minimalistic design of FAT requires less storage space and processing power
compared to more modern file systems, making it suitable for systems with
limited resources.
• Due to its simplicity, FAT file systems are often recoverable using basic data
recovery tools, allowing for potential retrieval of lost or deleted data.

Page 106 of 159


Operating System

Disadvantages of File Allocation Table (FAT)


• FAT lacks advanced metadata capabilities, such as support for extended file
attributes, which limits the types of information that can be associated with files.
• As files are created, modified, and deleted, the FAT file system can suffer from
fragmentation, leading to slower file access times and inefficient use of storage
space.
• FAT does not offer robust security features or fine-grained access controls,
leaving files more vulnerable to unauthorized access and modifications.
• FAT file systems have limitations on file sizes and partition sizes, which can be
restrictive when dealing with large files or storage devices.

Inode: An inode, short for "index node," is a data structure in Unix-like file
systems that stores metadata about a file, such as its permissions, ownership, size,
and location of data blocks on the storage device. Inodes play a crucial role in
efficient file management and data retrieval, as they enable the operating system to
quickly locate and access files. Each file or directory on the file system corresponds
to an inode, allowing for organized and optimized storage allocation.
Advantages of Inode
• Inodes enable rapid access to file metadata and data blocks, making file
operations like opening, reading, and writing faster and more efficient.
• Inodes allow sparse files—files with unallocated gaps—to be managed effectively,
as they only allocate space for actual data blocks, optimizing storage usage.
• Inodes facilitate the creation of hard links, which are multiple directory entries
pointing to the same inode.
• Inodes enhance file system stability by maintaining data consistency and
reducing the risk of data corruption.
Disadvantages of Inode
• Inodes consume a fixed amount of storage space regardless of file size.
• File systems have a finite number of inodes available.
• As directories grow in size, the number of inodes used to represent directory
entries can increase.
• Inode allocation and management can contribute to storage fragmentation.

REVIEW QUESTIONS
1. Write short notes on (1) Swapping and (2) Memory Allocation.
2. What is dynamic loading and dynamic linking? Explain.
3. Describe the following memory allocation methods: (1) Single partition
allocation and (2) Multiple partition allocation.
4. What is paging? Explain.
5. Explain segmentation with paging.
6. Write short notes on (i) Paging and (ii) Compaction.

Page 107 of 159


Operating System

7. What is swapping? Explain the swap-in and swap-out process with a well-
labelled diagram.
8. Explain memory management requirements.
9. Explain: (i) Logical vs. physical address space and (ii) Internal vs. external
fragmentation.
10. Explain the single partition allocation mechanism with an example.
11. Explain the concept of paging.
12. What is Paging? Explain. Write the advantages and disadvantages of paging.
13. What is Segmentation? Explain. Write advantages of segmentation.
14. What is Long Term Scheduling?
15. Explain Relocation.
16. Explain the method of multiple partition memory management.
17. Write a short note on 'relocation and protection'.
18. What is disk space management? Explain record blocking.
19. Differentiate between logical and physical address space.
20. Differentiate between paging and segmentation.

Page 108 of 159


Operating System

UNIT – IV

BASIC CONCEPT OF LINUX

INTRODUCTION
Linux is a community of open-source Unix-like operating systems that are
based on the Linux Kernel. It was initially released by Linus Torvalds on September
17, 1991. It is a free and open-source operating system and the source code can be
modified and distributed to anyone commercially or noncommercially under the
GNU General Public License.
Initially, Linux was created for personal computers and gradually it was
used in other machines like servers, mainframe computers, supercomputers, etc.
Nowadays, Linux is also used in embedded systems like routers, automation
controls, televisions, digital video recorders, video game consoles, smartwatches,
etc. The biggest success of Linux is Android (operating system) it is based on the
Linux kernel that is running on smartphones and tablets. Android Linux has the
largest installed base of all general-purpose operating systems. Linux is generally
packaged in a Linux distribution.

Linux Distribution- Linux distribution is an operating system that is made


up of a collection of software based on the Linux kernel or you can say distribution
contains the Linux kernel and supporting libraries and software. You can get Linux
based operating system by downloading one of the Linux distributions and these
distributions are available for different types of devices like embedded devices,
personal computers, etc. Around 600 + Linux Distributions are available and some
of the popular Linux distributions are:
MX Linux
Manjaro
Linux Mint
elementary
Ubuntu
Debian
Solus
Fedora
openSUSE
Deepin

Components of Linux System- Linux architecture has the following


components:

Kernel: Kernel is the core of the Linux-based operating system. It virtualizes the
common hardware resources of the computer to provide each process with its
virtual resources. This makes the process seem as if it is the sole process running

Page 109 of 159


Operating System

on the machine. The kernel is also responsible for preventing and mitigating
conflicts between different processes. Different types of the kernel are:
Monolithic Kernel
Hybrid kernels
Exo kernels
Microkernels

System Library: These are the special types of functions that are used to
implement the functionality of the operating system.

Shell: It is an interface to the kernel which hides the complexity of the kernel’s
functions from the users. It takes commands from the user and executes the
kernel’s functions.

Hardware Layer: This layer consists of all peripheral devices like RAM/ HDD/
CPU, etc.

System Utility: It provides the functionalities of an operating system to the


user.

Advantages of Linux
➢ The main advantage of Linux is that it is an open-source operating system.
This means the source code is easily available for everyone and you are allowed
to contribute, modify, and distribute the code to anyone without any
permissions.
➢ In terms of security, Linux is more secure than any other operating system. It
does not mean that Linux is 100 percent secure, it has some malware, but is
less vulnerable than any other operating system. So, it does not require any
anti-virus software.
➢ The software updates in Linux are easy and frequent.
➢ Various Linux distributions are available so that you can use them according to
your requirements or according to your taste.
➢ Linux is freely available to use on the internet.

Page 110 of 159


Operating System

➢ It has large community support.


➢ It provides high stability. It rarely slows down or freezes and there is no need to
reboot it after a short time.
➢ It maintains the privacy of the user.
➢ The performance of the Linux system is much higher than other operating
systems. It allows a large number of people to work at the same time and it
handles them efficiently.
➢ It is network-friendly.
➢ The flexibility of Linux is high. There is no need to install a complete Linux
suite; you can only install the required components.
➢ Linux is compatible with a large number of file formats.
➢ It is fast and easy to install from the web. It can also install on any hardware,
even on your old computer system.
➢ It performs all tasks properly even if it has limited space on the hard disk.

Disadvantages of Linux
➢ It is not very user-friendly. So, it may be confusing for beginners.
➢ It has small peripheral hardware drivers as compared to Windows.

LOGGING IN AND LOGGING OUT


Once the booting procedure is completed, the Linux operating system is
loaded into memory. To enter the system, we need to identify ourselves by a
system, which is known as logging in the Linux operating system.
A login, logging in, or logging on is the entering of identifier information into
a system by a user to access the system, i.e., a computer or a website. A login
requires the user to enter two pieces of information:

1. User Name- A user name or account name is a string (i.e., sequence of


characters) that uniquely identifies the user. User names can be the same as or
related to the real name of the user or different.
2. Password- A password is also a string, but it differs from a username, which
is to be kept secret and is known only to its user or to a system administrator.
This information is entered into a login window on a GUI or the command
line in a console (i.e., all text mode) depending on the system and situation.
If the password is correct, the system may display the following message, at
the end it displays a Linux prompt (displays $ for normal user or it displays % for
superuser), indicating that the system is ready to accept commands from the user.
Welcome to LINUX System
Last login Mon Aug 11 8:45 tty02
$

Page 111 of 159


Operating System

LOGOUT- To shut down the normal user terminal of system commands may
vary from different versions of the operating system. One may try following
commands to shutdown
➢ <ctrl> + d
➢ <ctrl> + z
➢ Logout
➢ logoff
On executing the above command, it disconnects our terminal from the
server. When a user logs out, the following message may appear on the screen.
Safe to power off
Or
Press any key to reboot
There are two ways of logging out
1. Logging Out of the system from a (text) terminal- To log out (exit) of a
text terminal, type logout.
2. Logging Out of the System from an X-Windows session- Use the mouse
to exit an X-Windows session; click the right button and choose the “Logout” or
“Exit” option.

ANATOMY OF LINUX OS
The Linux operating system's architecture mainly contains some of the
components: the Kernel, System Library, Hardware layer, System, and Shell utility.

Kernel: The kernel is one of the core sections of an operating system. It is


responsible for each of the major actions of the Linux OS. This operating system
contains distinct types of modules and cooperates with underlying hardware
directly. The kernel facilitates the required abstraction for hiding details of low-level
hardware or application programs in the system. There are some of the important
kernel types mentioned below:
Monolithic Kernel

Page 112 of 159


Operating System

Microkernels
Exo kernels
Hybrid kernels

System Libraries: These libraries can be specified as having some special


functions. These are applied for implementing the operating system's functionality
and don't need code access rights for the modules of the kernel.

System Utility Programs: It is responsible for doing specialized level and


individual activities.

Hardware layer: Linux operating system contains a hardware layer that


consists of several peripheral devices like CPU, HDD, and RAM.

Shell- It is an interface between the kernel and the user. It can afford the services
of kernel. It can take commands through the user and runs the functions of the
kernel. The shell is available in distinct types of OSes. These operating systems are
categorized into two different types, which are graphical shells and command-line
shells.
The graphical line shells facilitate the graphical user interface, while the
command line shells facilitate the command line interface. Thus, both of these
shells implement operations. However, the graphical user interface shells work
slower as compared to the command-line interface shells.
There are a few types of these shells which are categorized as follows:
Korn shell
Bourne shell
C shell
POSIX shell

DIRECTORY STRUCTURE
In Linux/Unix operating system, everything is a file, even directories are
files, files are files, and devices like mice, keyboards, printers, etc, are also files.
Here we are going to see the Directory Structure in Linux.

Types of files in the Linux system


General Files- It is also called ordinary files. It may be an image, video,
program, or simple text file. These types of files can be in ASCII or Binary format. It
is the most commonly used file in the Linux system.

Directory Files- These types of files are a warehouse for other file types. It may
be a directory file within a directory (subdirectory).

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 devices are represented as files. As, /dev/sda1, /dev/sda2, and so on.

Page 113 of 159


Operating System

We know that in a 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:
/Directory- The root directory, denoted by a single forward slash (/), is the
uppermost directory in the Linux directory structure. It contains all directories,
sub-directories, and files on your Linux system. It’s from the root directory where
the Linux directory hierarchy starts.

/root Directory- The /root directory is the home directory for the root user,
which is also referred to as the root user’s home directory (and not as the root (/)
directory).
The root account, also referred to as the superuser, administrative user,
system administrator or just the root user, has all the access to commands and
system files in Linux.

/boot Directory- This is one of the most vital directories in a Linux system. As
the name implies, the boot directory contains Linux boot files such as the
bootloader, the kernel, and its associated files. Files such as ‘vmlinuz’ represent the
compressed image of the Linux kernel.

/bin Directory - The /bin directory contains user binaries, executable


programs, and common system commands that are used by all users in the
system. These include ls, pwd, cat, mkdir, cd, mv, cp, du, df, tar, rpm, wc, history,
etc.

/etc Directory - The /etc directory contains system configuration files for all
the services, scripts, and third-party applications that are installed. This directory
is considered the nerve center of the Linux system.

/home Directory - The /home directory is a directory that contains a user’s


folders and files. On a graphical Linux system, the home directory, by default,

Page 114 of 159


Operating System

contains folders such as Desktop, Documents, Downloads, Pictures, Videos, and


Public.
In addition, the /home directory contains personal configuration files which
are prefixed with a dot (.). These are hidden files that contain user-specific settings
for the login shell session.

/opt Directory - The /opt directory is a directory that is reserved for add-on
packages and third-party software applications that are not included by default in
the system’s official repositories.
For example, when you install applications such as Skype, Discord, Spotify,
and Java, to mention a few, they get stored in the /opt directory.

/tmp Directory - The /tmp directory is a directory that stores temporary files
and many programs use this directory to create lock files and keep the temporary
storage of data.
Do not delete files under the/tmp directory unless you know exactly what
you are doing! Many of these files are critical for presently running programs and
removing them may affect a system crash.

/usr Directory - The /usr directory ranks as one of the most important
directories due to the enormous amount of data it holds. The directory contains
system-wide read-only files. These include libraries, user binaries and their
documentation, programs, and system utilities.

/var Directory - The /var directory stores system-generated variable files,


which include log files, caches, and spool files

/dev Directory- The /dev directory contains device files or special files for
devices that are attached to the system such as the hard drive, keyboard, and
mouse. As mentioned, these are not regular that a user can read and write to.
These are abstractions of standard devices that applications on your system
interact with through input and output system calls.

Some other directories in the Linux system:

/proc Directory- The /proc directory is somewhat of a strange directory. It is a


virtual or pseudo filesystem that contains vital information about running
processes. It is considered the control and information center for the Linux kernel.
The filesystem is created on the fly upon system startup and is destroyed
once the system is powered off.

/mnt Directory- The /mnt directory along with its subdirectories is a directory
intended to be used as a temporary mount point for mounting storage devices such
as Hard disk drives, USB drives, and CDROMs.

/sys Directory- The /sys is a virtual file system that contains a set of virtual
files that provide an interface to the Linux kernel.

/media Directory- The /media directory is a directory where the system


mounts removable media such as USB drives.

Page 115 of 159


Operating System

/run Directory- The /run directory is a temporary filesystem that contains


volatile runtime data that shows the system has since it was booted. Files under
the /run directory must be deleted (removed or truncated as convenient) at the
start of the boot process.

/lib Directory- The /lib directory stores all the essential standard libraries
required by user binaries in the /bin directory.

/lost+found Directory- The lost+found directory is installed during the


installation of Linux, useful for recovering files that may be broken due to an
unexpected shut-down.

/srv Directory- The /srv directory is the service directory and is abbreviated as
‘srv‘. This directory contains server-specific and service-related files.
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 the Windows operating system. In the Linux system, some
well-defined configuration files, binaries, and man pages information files are
available for every process.

/usr DIRECTORY
The /usr directory consists of several subdirectories that contain additional
UNIX commands and data files. It is also the default location of the user’s home
directories.
Although /usr is pronounced like the user, remember that it stands for Unix
System Resources. The /usr hierarchy should contain shareable, read-only data.
Some people choose to mount /usr as read-only.
The /usr/bin directory contains more UNIX commands. These commands
are used less frequently or are considered nonessential to UNIX system operation.
/usr/bin is a standard directory on Unix-like operating systems that contains most
of the executable files (i.e., ready-to-run programs) that are not needed for booting
(i.e., starting) or repairing the system.
The /usr/include directory contains header files for compiling C programs.
The /usr/lib directory contains more libraries and data files used by various
UNIX commands.
The /usr/spool directory contains various directories for storing files to be
printed, mailed, or passed through networks.
The /usr/tmp directory contains more temporary files.
The /usr/adm directory contains data files associated with system
administration and accounting. In particular, the /usr/adm/messages and
/usr/adm/Syslog files contain a record of system error messages, including those
sent to the system console. These files are especially useful for locating system
problems, including hardware problems. For example, an unusual number of disk
errors on a drive indicates a defective or misaligned drive. Because messages in the
file can accumulate rapidly, these files must be deleted periodically.

Page 116 of 159


Operating System

FILE TYPES
A file type helps us in identifying the type of content that is saved in the file.
Linux supports seven different types of files. These file types are the Regular file,
Directory file, Link file, Character special file, Block special file, Socket file, and
Named pipe file.

User data files (Regular or Ordinary Files)- Regular or ordinary files


store data of various content types such as text, audio, video, images, scripts, and
programs. There are hundreds of content types. In Linux, regular files can be
created with or without an extension.
An extension is a group of characters that is used with the file name to give
it a special identity or to group it with files of a similar content type. For easy
recognition and processing, files of different content types often use well-known file
extensions.
Although the Linux file system does not need file extensions, you still use
them. They help us in identifying the types of content that are stored in files. For
example, if a file has a .mp4 extension, you may know that it is a video file.

Directory files- To organize files in a hierarchy, file systems use directories.


Directories are also files, but instead of storing data, they store the location of other
files. To store the location of files placed in the directory, the directory uses
directory entries. Each directory entry stores the name and location of a single file.
Linux file system starts with a directory called / or root directory. All files
and directory files are created under this directory. Except for the root directory,
each directory has a parent directory.

System data files or Special files- Linux treats all hardware devices
(such as hard drives, printers, monitors, terminal emulators, and CD/DVD drives)
as special files. This means that an application program can access and use files
and devices in the same way. This feature makes developing programs in Linux
easier and more flexible.
Linux places all special files or device files under the /dev directory. There
are two types of special files: a character special file and a block special file. A
character special file represents a device that transfers data in bytes such as a
monitor or a printer. A block special file represents a device that transfers data in
blocks, such as a hard drive.

Link files- Link files allow us to use a file with a different filename and from a
different location. For this, we use link files. A link file is a pointer to another file.
There are two types of links: a hard link and a symbolic or soft link.
A hard link creates a mirror copy of the original file. A hard link cannot be
created to a directory or a file on another filesystem. A soft or symbolic link creates
a pointer to the original file. A soft link can be created to a directory or a file on
another filesystem.

Page 117 of 159


Operating System

Socket files- A socket is a communication endpoint that applications use to


exchange data. For example, if an application wants to communicate with another
application, it connects with the socket of that application.
Each application that provides services to other applications or remote
clients uses a socket to accept connections. Each socket has an associated IP
address and port number that allow it to accept connections from clients.
For example, if an application of the local system wants to communicate
with another application of a remote system, it connects to the socket of that
application by using the associated IP address and port number of that socket.

Named pipe files- Linux allows us to send the output of any process or
command to another process or command as the input. This feature is known as
the pipe. Pipes work only when both processes are started by the same user and
exist in the same parent process space.
If processes are executed under different user names and permissions, then
standard pipes do not work. In such circumstances, named pipes are used. Named
pipes are similar to the standard pipes except that they can be accessed as part of
the filesystem.
Named pipe files are empty pipe files. The kernel processes named pipe files
without writing them to the file system. Named pipe files can exist anywhere in the
file system. Named pipe files are also known as the FIFO (First In First Out) files.
Executable files- An executable file is a file that is used to perform various
functions or operations on a computer. Unlike a data file, an executable file cannot
be read because it's compiled. On an IBM-compatible computer, common
executable files are .BAT, .COM, .EXE, and .BIN.

NAMING FILES AND DIRECTORIES


Linux adheres to the following rules for file and directory names:
➢ A file or directory name can have from 1 to 256 characters in length.
➢ Linux is case-sensitive, uppercase characters are different from lowercase
characters.
➢ File or directory names can include letters, numbers, and the following special
characters: period (.), underscore (_), and dash (-).
➢ The special characters comma (,), quotes (‘,’ “ “, or ` ), slash ( / ), backslash ( \ ),
and space can be used in file or directory names, but are highly advised
against.
Linux provides two wildcard characters to represent ambiguous characters
or strings in file or directory names:
➢ The question mark (?) represents any single character. (e.g.: ls file?.txt would
list file1.txt file2.txt, but not file50.txt)
➢ The asterisk (*) represents any string of zero or more characters. (e.g.: ls
file*.txt would list file.txt file1.txt file2.txt file50.txt, but not file01.data)

Page 118 of 159


Operating System

General File naming conventions- Unlike Windows, Linux is not strict


with the naming convention. We can use any number or letter in a file name. We
can also use an underscore, space, period, and comma. Some special characters
such as a question mark, asterisk, and slash are not allowed in a file name. These
characters are reserved for shell functions. Just like Windows, we can use a dot to
create a file extension. Although file extensions are not compulsory in Linux, they
should still be used wherever possible as they provide a good way to manage files.
➢ A file name can contain any character or number.
➢ The maximum length for a file name is 256 characters.
➢ A file name can use a space, underscore, minus, period, and comma.
➢ A file name cannot use a question mark, asterisk, or slash.
➢ A file extension is not compulsory. A file can be created with or without an
extension.
➢ A file whose name starts with a period (.) is treated as a hidden file.
➢ If a file name contains a white space, it needs to be quoted before it can be
accessed at the command prompt.

SHELL
The shell can be defined as a command interpreter within an operating
system like Linux/GNU or Unix. It is a program that runs other programs. The
shell facilitates every user of the computer as an interface to the Unix/GNU Linux
system. Hence, the user can execute different tools/utilities or commands with a
few input data.
The shell sends the result to the user over the screen when it has completed
running a program, which is the common output device. That's why it is known as
a "command interpreter".
The shell is not just a command interpreter. Also, the shell is a programming
language with complete constructs of a programming language such as functions,
variables, loops, conditional execution, and many others.

Category of Shell- Broadly, the shell is categorized into two main categories
which are explained below:

1) Graphical Shells- These shells specify the manipulation of programs that


are based on the graphical user interface (GUI) by permitting operations like
moving, closing, resizing, and opening windows and switching focus among
windows as well. Ubuntu OS or Windows OS could be examined as a good
example that offers a graphical user interface for the user to interact with the
program. Various users don't need to type in any command for all the actions.

2) Command-line Shell- Various shells could be accessed with the help of a


command-line interface by users. A unique program known as Command
prompt in Windows or Terminal in macOS/Linux is offered for typing in
human-understandable commands like "ls", "cat", etc, and after that, it is run.
The result is further shown to the user on the terminal.

Page 119 of 159


Operating System

Working on a command-line shell is complicated for many beginners


because it is hard to remember several commands. Command-line shell is very
dominant and it permits users to store commands in a file and run them
together. In this way, a repetitive action could be automated easily. Usually,
these files are known as Shell scripts in macOS/Linux systems and batch files
in Windows.

Types of Shell- There are various types of shells which are discussed as
follows:

a) Bash Shell- In the bash shell, bash means Bourne Again Shell. It is a default
shell over several distributions of Linux today. It is a sh-compatible shell. It
could be installed over Windows OS. It facilitates practical improvements on
the sh for interactive and programming use, which contains:
Job Control
Command-line editing
Shell Aliases and Functions
Unlimited size command history
Integer arithmetic in a base from 2-64

b) Csh/Tcsh Shell- Tcsh is an upgraded C shell. This shell can be used as a


shell script command processor and interactive login shell. Tcsh shell includes
the following characteristics:
C like syntax
Filename completion and programmable word
Command-line editor
Job control
Spelling correction

c) Ksh Shell- Ksh means Korn shell. It was developed and designed by David G.
Korn. Ksh shell is a high-level, powerful, and complete programming language,
and it is a reciprocal command language as well, just like various other
GNU/Unix Linux shells. The usage and syntax of the C shell are very similar to
that of the C programming language.

d) Zsh Shell- Zsh shell is developed to be reciprocal and it combines various


aspects of other GNU/Unix Linux shells like ksh, tcsh, and bash. Also, the
POSIX shell standard specifications were based on the Korn shell. Also, it is a
strong scripting language like other available shells. Some of its unique
features are listed as follows:
Startup files
Filename generation
Login/Logout watching
Concept index
Closing comments
Variable index

Page 120 of 159


Operating System

Key index
Function index and various others that we could find out within the
man pages.
All these shells do a similar job but take different commands and
facilitate distinct built-in functions.

e) Fish- Fish stands for "friendly interactive shell". It was produced in 2005. Fish
shell was developed to be fully user-friendly and interactive just like other
shells. It contains some good features which are mentioned below:
Web-based configuration
Man page completions
Auto-suggestions
Support for term256 terminal automation
Completely scripted with clean scripts

CREATING USER ACCOUNT


The Linux server allows us to create more than one user after installation.
Linux is a multi-user system, which means more than one user can work in the
same system at the same time. We are allowed to do so through the Setup agent.
We must create an account to work with Linux as we cannot keep working
with the root account. We have one administrative account; a system administrator
account is responsible for managing the user accounts and groups of the system.
Adding or removing a user is one of the most basic tasks of a new Linux server.
We are given only a root user account on a new Linux server. Adding a user
account provides lots of power and accessibility to a particular user. It is a useful
but insecure utility of the Linux server. It is good to add an unprivileged user to do
common tasks. However, we can access the administrative privilege through the
Sudo command-line utility.

Create a User in Linux (Ubuntu)- There are two most common ways to
add a user to a Linux server.
Graphically through the user manager
By the useradd command (Terminal)

Graphically through the user manager- Linux GUI allows us to create a


user from its functions. It is a straightforward process. To create a user for your
Linux server, follow the below steps:
Step 1: Goto system search and search for the setting and navigate to Detail->
About.
Step 2: Click on the Users after the Unlock option given on the header. It will ask
for the system security password to enter the password and click ok to continue.
Consider the below image:

Page 121 of 159


Operating System

Step 3: Click on the Add User option to add a new user.

Step 4: Enter the user details like username and password and account type. We
can create two types of accounts, which are Standard and Administrator. The
standard account does not contain the sudo privilege. However, we can provide it
later.

Page 122 of 159


Operating System

Step 5: Now, we have successfully created a new user called JTP2. Consider the
below image.

By the Linux useradd command, in Linux, the useradd command is a


command-line utility that is used to add or remove a user on a Linux server and
Unix-based operating system. In a different type of Linux distribution, the useradd
command may be slightly different. The useradd command performs the below
tasks:
➢ It edits the files for newly created users like /etc/passwd, /etc/shadow,
/etc/group, and /etc/gshadow.
➢ It creates and opens a new home directory.
➢ It allows us to set ownerships and permissions to the home directory.
Syntax: useradd [options] username
To use the useradd command, we have to log in with root or sudo access.
Before using the Linux useradd command, understand some common terms that
are used in the Linux command line.
Username: A username is a name that is used to log in to the Linux system. It is
displayed when we turn on our machine. The username length should be between
1 to 32 characters.
Password: A password is a secret code that is used to protect your system from
unauthorized access. It is stored in an etc/shadow file in an encrypted format.
User ID (UID): Linux provides a unique ID to every user; it is called a user
identification number or User ID or UID. By default, the UID for the root user is
reserved as zero, and the remaining UID from 1 to 99 is reserved for other
predefined accounts. Further, UIDs from 100-999 are reserved for groups and
system accounts.
Group ID (GID): The GID or Group ID is a group identification number provided
by the Linux system. It is stored in the/etc/group file.
User Info: It allows us to define some additional information about the user, such
as the user’s full name. It is optional.

Page 123 of 159


Operating System

Home Directory: It is an absolute location for a user.


Shell: It is an absolute location of a user's shell, i.e. /bin/bash.
To create a new user by the useradd command, execute the useradd
command followed by the username as follows:
sudo useradd JTP3
The above command will ask for the system administration password, enter
the password. It will create a user named JTP3. This username is used by the user
to log in to the system. The username must be unique. Consider the below output:
To set the password for the newly created user, execute the below command:
sudo passwd JTP3
The above command will ask for the new password, enter the password and
retype the new password. It will update the password for the specified user.
Consider the below output:

Create a user with a home directory- Sometimes the newly created user
may or may not assign a home directory. To create a user and forcefully assign a
home directory to it, execute the below command:
Sudo useradd -m Demo
The above command will ask for the system administration password and
create a directory home/Demo for the user Demo.

Create a user with a different home directory- Linux allows us to create


a home directory in a different place instead of the default folder. Use the -d option
with the useradd command to create a different home directory. Execute the below
command:
sudo useradd -m -d /Demo1 Demo1
The above command will create a Demo1 folder under the root directory for the
user Demo1.
SHELL CONCEPT
A shell is a special user program that provides an interface for the user to
use operating system services. Shell accepts human-readable commands from a
user and converts them into something which the kernel can understand. It is a
command language interpreter that executes commands read from input devices
such as keyboards or files. The shell gets started when the user logs in or starts the
terminal.
Shell is broadly classified into two categories-
Command Line Shell
Graphical shell

Page 124 of 159


Operating System

Command Line Shell- Shell can be accessed by the user using a command
line interface. A special program called Terminal in linux/macOS or Command
Prompt in Windows OS is provided to type in human-readable commands such as
“cat”, “ls” etc. and then it is executed. The result is then displayed on the terminal
to the user. A terminal in the Ubuntu 16.4 system looks like this –

In the above screenshot, the “ls” command with the “-l” option is executed. It
will list all the files in the current working directory in a long listing format.
Working with a command line shell is a bit difficult for beginners because it’s hard
to memorize so many commands. It is very powerful; it allows users to store
commands in a file and execute them together. This way any repetitive task can be
easily automated. These files are usually called batch files in Windows and Shell
Scripts in Linux/macOS systems.

Graphical Shells- Graphical shells provide means for manipulating programs


based on the graphical user interface (GUI), by allowing for operations such as
opening, closing, moving, and resizing windows, as well as switching focus between
windows. Window OS or Ubuntu OS can be considered as good examples which
provide a GUI to the user for interacting with the program. Users do not need to
type in a command for every action. A typical GUI in the Ubuntu system-

Page 125 of 159


Operating System

There are several shells available for Linux systems -


➢ BASH (Bourne Again SHell)- It is the most widely used shell in Linux
systems. It is used as the default login shell in Linux systems and macOS. It
can also be installed on Windows OS.
➢ CSH (C SHell)- The C shell’s syntax and usage is very similar to the C
programming language.
➢ KSH (Korn SHell)- The Korn Shell also was the base for the POSIX Shell
standard specifications, etc.
Each shell does the same job but understands different commands and
provides different built-in functions.

SHELL PROGRAM
Usually, shells are interactive that mean, they accept the command as input
from users and execute them. However, sometimes we want to execute a bunch of
commands routinely, so we have to type in all commands each time in the
terminal.
As a shell can also take commands as input from the file, we can write these
commands in a file and execute them in the shell to avoid this repetitive work.
These files are called Shell Scripts or Shell Programs. Shell scripts are similar
to the batch file in MS-DOS. Each shell script is saved with a .sh file extension, eg,
myscript.sh.
A shell script has syntax just like any other programming language. If you
have any prior experience with any programming language like Python, C/C++,
etc., it would be very easy to get started with it.
A shell script comprises the following elements-
➢ Shell Keywords – if, else, break, etc.
➢ Shell commands – cd, ls, echo, pwd, touch, etc.
➢ Functions
➢ Control flow – if..then..else, case and shell loops, etc.

Page 126 of 159


Operating System

Need for shell scripts- There are many reasons to write shell scripts:
➢ To avoid repetitive work and automation
➢ System admins use shell scripting for routine backups
➢ System monitoring
➢ Adding new functionality to the shell, etc.

Advantages of shell scripts


➢ The command and syntax are the same as those directly entered in the
command line, so programmers do not need to switch to entirely different
syntax
➢ Writing shell scripts is much quicker
➢ Quick start
➢ Interactive debugging etc.

Disadvantages of shell scripts


➢ Prone to costly errors, a single mistake can change the command, which
might be harmful
➢ Slow execution speed
➢ Design flaws within the language syntax or implementation
➢ Not well suited for the large and complex tasks
➢ Provide minimal data structure, unlike other scripting languages. etc.

BASH SHELL
BASH is an acronym for Bourne Again Shell, a punning name, which is a
tribute to Bourne Shell (i.e., invented by Steven Bourne).
Bash is a shell program written by Brian Fox as an upgraded version of the
Bourne Shell program 'sh'. It is an open-source GNU project. It was released in
1989 as one of the most popular shell distributions of GNU/Linux operating
systems. It provides functional improvements over Bourne Shell for both
programming and interactive uses. It includes command line editing, key bindings,
command history with unlimited size, etc.
In basic terms, Bash is a command line interpreter that typically runs in a
text window where the user can interpret commands to carry out various actions.
The combination of these commands as a series within a file is known as a Shell
Script. Bash can read and execute the commands from a Shell Script.
Bash is the default login shell for most Linux distributions and Apple's mac
OS. It is also accessible for Windows 10 with a version and default user shell in
Solaris 11.
When you first launch the shell, it uses a startup script located in the
.bashrc or .bash_profile file which allows you to customize the behavior of the shell.
When a shell is used interactively, it displays a $ when it is waiting for a command
from the user. This is called the shell prompt.

Page 127 of 159


Operating System

[username@host ~] $
If the shell is running as root, the prompt is changed to #. The superuser
shell prompt looks like this:
[root@host ~] #
Bash is very powerful as it can simplify certain operations that are hard to
accomplish efficiently with a GUI. Remember that most servers do not have a GUI,
and it is best to learn to use the powers of a command line interface (CLI).

CHANGING SHELL PROMPT


The shell prompt can be changed to display information such as the
username for a user, the directory in which the user is working, the current date
and time, or some special message. To change the shell prompt for your current
login session, follow the steps given below:
1. Display the shell variables. At the shell prompt, type ‘printenv’ and press Enter.
If you cannot read the entire screen, send the printenv command to the less
pager.
(Type printenv |less)
2. Look for the PS1 variable. It may look as follows:
PS1=[\u@\r\w] \$
This variable tells the system to display the username for whoever is
using the host computer and that user’s current working directory inside
brackets followed by the $ prompt (or # prompt for the root account).
3. Write down the information from the PS1 variable.
4. Decide how you want the prompt to look. The following table shows the
characters to customize the shell prompt.

Character code Description


\! Display the number used in the history list to access the
command that would be executed at the shell prompt.
\$ Display a $ in the prompt for the regular user and a # for
the superuser.
\\ Display the backslash character.
\d Display the current date
\h Display the hostname of the computer at which you are
working
\t Display the current time
\u Display the username of the user who is logged in to the
system
\w Display the current working directory

Page 128 of 159


Operating System

\xxx Display any special comment.

Example: If you want to change the prompt so that it will display the time, the
date, the current working directory, and the $ prompt, then type PS1= [\t \d \w]\$‘
and press Enter. The shell prompt will change to display the following: [12:18:40
Sat Jan 21 aditi] $
Once you log out of your account, the shell prompt will return to the default
shell prompt.

COMMANDS
The Linux command is a utility of the Linux operating system. All basic and
advanced tasks can be done by executing commands. The commands are executed
on the Linux terminal. The terminal is a command-line interface to interact with
the system, which is similar to the command prompt in Windows OS. Commands
in Linux are case-sensitive.
Linux provides a powerful command-line interface compared to other
operating systems such as Windows and macOS. We can do basic work and
advanced work through its terminal. We can do some basic tasks such as creating
a file, deleting a file, moving a file, and more. In addition, we can also perform
advanced tasks such as administrative tasks (including package installation, and
user management), networking tasks (ssh connection), security tasks, and many
more.
Linux terminal is a user-friendly terminal as it provides various support
options. To open the Linux terminal, press the "CTRL + ALT + T" keys together, and
execute a command by pressing the 'ENTER' key.

Basic Syntax for A Command- Linux commands can be executed by


typing their name on the command line, followed by the options list, and then
parameters. A typical command line is as follows:
$ commandname –option [parameter]
The single-line command is referred to as the command line and it is typed
after the shell prompt. The command name always appears first, it tells the shell
WHAT to do. To execute the command press Enter. The minimum usage for most
commands is the command and one or more command options.
Command options are typed after the command and are used to modify the
result of the command. There must be a space between the command and the
command option. Then, the dash (-) character must precede the command option.
The dash tells Linux to treat each letter that follows the dash as a command
option. There is no space between the dash and the command option. There can be
more than one command option, but do not put any space between the dash and
the option.
For example, $ls –l /usr/doc
The ls command displays a file list for the contents of the current working
directory. The command option ls –l, modifies the information displayed about each

Page 129 of 159


Operating System

file in the listing. The parameter tells the command to list the contents of the
/usr/doc directory instead of the current working directory.
A command parameter can be a file, a directory, or a period. Parameters are
not preceded by a dash. Parameters specify which file or directory command is to
be acted upon, or they can set a time limit in which the command is to be
executed.

Creating an alias for a long command: A command alias is a unique


short name that is assigned to a command. It is possible to assign a short name for
a long command using an alias. To create an alias for the following command
cd /user/doc/file1
Suppose ’move’ is a sample alias name, to create this alias, type at the shell
prompt:
$alias move= “cd/user/doc/file1”
The alias command is followed by the alias name. The equal sign (=)
precedes the command that will be executed when the alias name is typed at the
shell prompt. Do not give space to either side of the equal sign. Now to change to
the ‘file1’ directory just type ‘move’ and press Enter.

EXPLORING THE HOME DIRECTORY


The Linux home directory is a directory for a particular user of the system
and consists of individual files. It is also referred to as the login directory. This is
the first place that occurs after logging into a Linux system. It is automatically
created as "/home" for each user in the directory. It is a standard subdirectory of
the root directory. The root directory contains all other directories, subdirectories,
and files on the system. It is denoted by a forward slash (/).
The home directory can be said as a personal working space for all the users
except root. There is a separate directory for every user. For example, two users
'jtp1' and 'jtp2' will have directories like "/home/jtp1" and "/home/jtp2". These
users will have all the rights under their directory files.
The root (administrative) user is the only user who has its home directory in
a different location by default. The path of the root user is '/root/', where it has
control under all the directories and files.

How to find the Home Directory? There are multiple ways to access
and return to the home directory. Some commands are very helpful for the
directories such as cd, pwd, mkdir, pwd, ls, and rmdir. To access the home
directory graphically, open the Files application, and click on the Home option from
the left side menu. Consider the below image:

Page 130 of 159


Operating System

Here, we can explore our home directory.


Generally, our terminal opens with the user's particular directory. To change
the directory to the home directory, execute the cd command as follows:
cd /home
The above command will change the directory to home. To list the home
directory, execute the ls command as follows:
ls
Consider the below output:

We can get back to our home directory by executing the cd command


without any argument. It will go back to our home directory from any directory we
are working on. Execute it as follows:
cd
Consider the below output:

From the above output, we were in /Directory/files, by executing only the cd


command, we reached our home directory. We can use the "cd ~ or cd $HOME"
commands as well, to do the same. Consider the below commands:
cd ~
cd $Home
To display the current working directory, execute the pwd command as follows:
pwd
Consider the below output:

Page 131 of 159


Operating System

ls COMMAND
ls is a Linux shell command that lists directory contents of files and
directories. Some practical examples of the ls command are shown below.

Open Last Edited File Using ls -t


ls -t: It sorts the file by modification time, showing the last edited file first.
head -1 picks up this first file. To open the last edited file in the current directory
use the combination of ls and head commands as shown below.

This will open the last file you edited (i.e., second.txt)

Display One File Per Line Using ls -1

Display All Information About Files/Directories Using ls -l


$ ls -l: To show long listing information about the file/directory.

-rw-rw-r– 1 maverick maverick 1176 Feb 16 00:19 1.c

Page 132 of 159


Operating System

1st Character – File Type: The first character specifies the type of the file. In
the example above, the hyphen (-) in the 1st character indicates that this is a
normal file. Following are the possible file type options in the 1st character of the ls
-l output.
Field Explanation
– normal file
d: directory
s: socket file
l: link file
Field 1- File Permissions: Next 9 characters specify the permission of the file.
Every 3 characters specify read, write, and execute permissions for the user(root),
group, and others respectively in order. Taking the above example, -rw-rw-r–
indicates read-write permission for the user(root), read permission for the group,
and no permission for others respectively. If all three permissions are given to the
user(root), group, and others, the format looks like this -rwxrwxrwx
Field 2- Number of links: Second field specifies the number of links for that file.
In this example, 1 indicates only one link to this file.
Field 3- Owner: The third field specifies the owner of the file. In this example,
this file is owned by the username ‘maverick’.
Field 4- Group: Fourth field specifies the group of the file. In this example, this
file belongs to the ”maverick” group.
Field 5- Size: Fifth field specifies the size of the file in bytes. In this example,
‘1176’ indicates the file size in bytes.
Field 6- Last modified date and time: The sixth field specifies the date and
time of the last modification of the file. In this example, ‘Feb 16 00:19’ specifies the
last modification time of the file.
Field 7- File name: The last field is the name of the file. In this example, the file
name is 1.c.

Display File Size in Human Readable Format Using ls -lh


ls -lh (h stands for human readable form): To display file size in an easy-to-read
format. i.e., M for MB, K for KB, G for GB.

Display Directory Information Using ls -ld

Page 133 of 159


Operating System

When you use “ls -l” you will get the details of the directory's content. But if
you want the details of the directory then you can use the -d option as., For
example, if you use ls -l /etc, it will display all the files under the etc directory. But,
if you want to display the information about the /etc/ directory, use the -ld option
as shown below.
$ ls -l /etc

$ ls -ld /etc

Order Files Based on Last Modified Time Using ls -lt


ls -lt: To sort the file names displayed in the order of the last modification
time. You will find it handy to use it in combination with the -l option.

Order Files Based on Last Modified Time (In Reverse Order)


Using ls -ltr
$ ls -ltr: To sort the file names in the last modification time in reverse order.
This will be showing the last edited file in the last line, which will be handy when
the listing goes beyond a page.

Page 134 of 159


Operating System

Display Hidden Files Using ls -a (or) ls -A


$ ls -a: To show all the hidden files in the directory, use the ‘-a option’. Hidden
files in Unix start with ‘.’ in their file name. It will show all the files including the ‘.’
(current directory) and ‘..’ (parent directory).

$ ls -A: To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent
directory).

Display Files Recursively Using ls -R


$ ls /etc/apt

Page 135 of 159


Operating System

$ ls -R /etc/apt: To show all the files recursively. When you do this from /, it
shows all the unhidden files in the whole file system recursively.

Display File Inode Number Using ls -i


Sometimes you may want to know the inode number of a file for internal
maintenance. Use the -i option as shown below to display the inode number. Using
the inode number, you can remove files that have special characters in their name.
$ ls -i

$ ls -i /etc/apt

Page 136 of 159


Operating System

Hide Control Characters Using ls -q


ls -q: To print question marks instead of the non-graphics control characters.

Display File UID and GID Using ls -n


$ ls -n ~/kv: Lists the output like -l, but shows the uid and gid in numeric format
instead of names.

Visual Classification of Files with Special Characters Using ls


-F
$ ls -F: Instead of doing the ‘ls -l’ and then checking for the first character to
determine the type of file. You can use -F which classifies the file with different
special characters for different kinds of files.
/ – directory.
Nothing – normal file.
@ – link file.
* – Executable file

Page 137 of 159


Operating System

Visual Classification of Files with Colors Using ls -F


$ ls –color=auto: Recognizing the file type by the color in which it gets
displayed is another kind of classification of a file. In the below output, directories
get displayed in blue, soft links get displayed in green, and ordinary files get
displayed in the default color.

mkdir COMMAND
In Linux, if you want to create a new directory, you can do so by using the
mkdir command. The mkdir command is used to make a directory in Linux.
Before jumping into how we can create a directory (or in Windows
terminology - a folder), first, let's see how the mkdir command works.

Using the mkdir Command- Here is the syntax for the mkdir command:
mkdir [OPTION] [DIRECTORY]
We can provide some options and the path of one or more directories while
using the mkdir command. To see the available options, you can run the following
command in your terminal: mkdir --help.
Now let's see how we can create a directory using the mkdir command in Linux.

Creating a Directory- We will start by creating a single directory using the


mkdir command:
mkdir studytonight

Page 138 of 159


Operating System

The above command will create a new directory with the name studytonight
in the present working directory. You can use the ls command to list down the files
and directories in the current directory.
ls -l
Output- drwxrwxr-x 2 nobody nobody 6 Jul 2 17:29 studytonight
By default, the directory is created inside the current directory. If we want to
create the directory in some other location, then we can provide the complete path
while creating the directory.
Consider that we have a directory structure:
home: - someuser: - learn-linux - test-linux
And we are currently inside the directory someuser and want to create a new
directory with a name sample inside the directory test-linux, we can do so using
the following command:
mkdir text-linux/sample
Consider that you are currently inside the directory test-linux and you want
to create a directory inside the directory learn-linux with a name sample, we can do
this using the following command:
mkdir ../learn-linux/sample
The two dots, in the beginning, represents that we want to move out of the
test-Linux directory, then go inside the learn-linux directory and create a directory
named sample there.

Creating a directory along with Parent Directories- If we want to


create a directory structure for example in the above-given directory structure if
you are currently inside the someuser directory and you want to create a directory
sample inside the directory practice-Linux, how will you do so? Notice, we do not
have the practice-Linux directory inside someuser directory. We can use the mkdir
command to create the parent directory too while creating a directory using the -p
option.
mkdir -p practice-Linux/sample
When we run the above command, a new directory practice-Linux will be
created and inside it, a sample directory will also get created. This is the magic of
the -p option of the mkdir command. Similarly, you can use the -p option to create
multiple levels of parent directories.

Creating Multiple Directories- If we want to create multiple directories in


one go, we can easily do so using the mkdir command.
mkdir dir1 dir2 dir3
The above command will create 3 directories in the current directory.
Output-
drwxrwxr-x 2 nobody nobody 6 Jul 2 17:50 dir1
drwxrwxr-x 2 nobody nobody 6 Jul 2 17:50 dir2

Page 139 of 159


Operating System

drwxrwxr-x 2 nobody nobody 6 Jul 2 17:50 dir3


Similarly, you can provide different paths to create multiple directories all at
different locations by running a single command.

Creating Directory with custom Permissions- When we use the


mkdir command to create a directory in Linux, the default mode/permissions
assigned to it is 755, which means read and execute permission for all, but not
everyone can write to this directory. If you want to provide custom permissions like
700, 644, or even more restrictive 444, we can do so using the -m option (which
stands for mode) with the mkdir command. Here's an example,
mkdir -m 444 dir1
output- dr--r--r-- 2 nobody nobody 6 Jul 3 10:31 dir1
This will create a new directory with 444 mode/permission, which you can see at
the beginning of the output dr--r--r-- which represents the permissions. We can
change the permission for any directory or file, using the chmod command in Linux
rmdir COMMAND
rmdir command is used to remove empty directories from the filesystem in
Linux. The rmdir command removes every directory specified in the command line
only if these directories are empty. So if the specified directory has some directories
or files in it then this cannot be removed by the rmdir command.
Syntax: rmdir [-p] [-v | –verbose] [–ignore-fail-on-non-empty] directories …
Options:
➢ –help: It will print the general syntax of the command along with the various
options that can be used with the rmdir command as well as give a brief
description bout each option.
➢ rmdir -p: In this option each of the directory arguments is treated as a
pathname of which all components will be removed, if they are already empty,
starting from the last component.
➢ rmdir -v, –verbose: This option displays verbose information for every directory
being processed.
➢ rmdir –ignore-fail-on-non-empty: This option does not report a failure that
occurs solely because a directory is non-empty. Normally, when rmdir is being
instructed to remove a non-empty directory, it simply reports an error. This
option consists of all those error messages.
➢ rmdir –version: This option is used to display the version information and exit.
Example 1: This will first remove the child directory and then remove the parent
directory.
rmdir -p mydir/mydir1

Page 140 of 159


Operating System

Example 2: Remove the directories mydir1, mydir2, and mydir3, if they are
empty. If any of these directories are not empty, then an error message will be
printed for that directory, and the other directories will be removed.
rmdir mydir1 mydir2 mydir3

Example 3: Remove the directory mydir/mydir1 if it is empty. Then, remove


directory mydir, if it is empty after mydir/mydir1 was removed.
rmdir mydir/mydir1 mydir

stat COMMAND
The stat is a command which gives information about the file and filesystem.
Stat command gives information such as the size of the file, access permissions
and the user ID and group ID, and birth time access time of the file. Stat command
has another feature, by which it can also provide the file system information. This
is the best tool to use when we want the information on any file.
The basic syntax of using the stat command is as follows:
stat --options filenames
Stat can accept one or more filenames as input to the stat command. For
example
stat /etc/resolv.conf
Output:

Page 141 of 159


Operating System

The information we get from stat- The following is the information we get
about the file when we run the stat command.
➢ File: The name of the provided file. If the provided file is a symlink, then the
name will be different.
➢ Size: The size of a given file in Bytes.
➢ Blocks: Total number of allocated blocks to the file to store on the hard disk.
➢ IO Block: The size of every allocated block in bytes.
➢ File type: The file may be of the following types: Regular files, special files,
directories, or symbolic links.
➢ Device: Device number in hexadecimal format.
➢ Inode: Inode number of the file.
➢ Links: Number of hard links of the file.
➢ Access: Access permissions in the numeric and symbolic methods.
➢ Context: The field stores the SELinux security context.
➢ Access: The last time at which the file was accessed.
➢ Modify: The last time at which the file was modified.
➢ Change: The last time the at which file’s attribute or content was changed.
➢ Birth: The time at which the file was created.

Displaying the File System Information- stat command can provide


the file system information when we provide the file name with the -f (–file-system)
options. Syntax of using the -f option with the stat command:
stat -f filename

We can also provide the directory or file system as input to the stat
command as follows:
stat -f /
The information we get for the filesystem from the stat

Page 142 of 159


Operating System

➢ File: The name of provided file.


➢ ID: File system ID in hexadecimal format.
➢ Namelen: The maximum length (number of characters) of a file name.
➢ Fundamental block size: Total size of each block on the file system.
➢ Blocks:
• Total: Total number of blocks in the file system
• Free: Total number of free blocks in the file system
• Available: Total number of free blocks available for non-root users
➢ Inodes:
• Total: Total number of inodes in the file system.
• Free: Total number of free inodes in the file system.

Using Stat with Multiple Files- To get the information about multiple
files using the stat command, just mention the filenames separated by space:
stat locale.conf login.defs
This will show the information about the mentioned files.

cat COMMAND
cat(concatenate) command is very frequently used in Linux. It reads data
from the file and gives its content as output. It helps us to create, view, and
concatenate files. So let us see some frequently used cat commands.

To view a single file


Command: $cat filename
Output- It will show the content of the given filename

To view multiple files


Command: $cat file1 file2
Output- This will show the content of file1 and file2.

Page 143 of 159


Operating System

To view the contents of a file preceding with line numbers.


Command: $cat -n filename
Output- It will show content with line number
Example: $cat-n geeks.txt
Output- 1) This is geeks
2) A unique array

Create a file
Command: $ cat > newfile
Output- Will create a file named newfile

Copy the contents of one file to another file.


Command:
$cat [filename-whose-contents-is-to-be-copied] > [destination-
filename]
Output- The content will be copied into the destination file

Cat command can suppress repeated empty lines in the output


Command: $cat -s geeks.txt
Output- Will suppress repeated empty lines in the output

Cat command can append the contents of one file to the end of another
file.
Command: $cat file1 >> file2
Output- Will append the contents of one file to the end of another file

Cat command can display content in reverse order using tac command.
Command: $tac filename
Output- Will display content in reverse order

Cat command can highlight the end of the line.


Command: $cat -E "filename"
Output- Will highlight the end of the line

If you want to use the -v, -E, and -T options together, then instead of writing
-vET in the command, you can just use the -A command line option.
Command $cat -A "filename"

Cat command if the file has a lot of content and can’t fit in the
terminal.
Command: $cat "filename" | more
Output- Will show that much content, that could fit in the terminal, and will
ask to show more.

Cat command to merge the contents of multiple files.


Command: $cat "filename1" "filename2" "filename3" > "merged_filename"

Page 144 of 159


Operating System

Output- Will merge the contents of the file in respective order and will insert
that content in "merged_filename".

Cat command to display the content of all text files in the folder.
Command: $cat *.txt
Output- This will show the content of all text files present in the folder.

Cat command to write in an already existing file.


Command: $cat >> geeks.txt
The newly added text.
Output- Will append the text "The newly added text." to the end of the file.

rm COMMAND
rm stands for remove here. The rm command is used to remove objects such
as files, directories, symbolic links, and so on from the file system like UNIX. To be
more precise, rm removes references to objects from the filesystem, where those
objects might have had multiple references (for example, a file with two different
names). By default, it does not remove directories. This command normally works
silently and you should be very careful while running the rm command because
once you delete the files then you are not able to recover the contents of files and
directories.
Syntax: rm [OPTION]... FILE...

Example- Let us consider 5 files having names a.txt, b.txt, and so on till e.txt.
$ ls
Output- a.txt b.txt c.txt d.txt e.txt

Removing one file at a time


$ rm a.txt
$ ls
b.txt c.txt d.txt e.txt

Removing more than one file at a time


$ rm b.txt c.txt
$ ls
d.txt e.txt
No output is produced by rm since it typically only generates messages in the case
of an error.

Options:
1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask
the user for confirmation before removing each file, you have to press y to
confirm the deletion, any other key leaves the file un-deleted.
$ rm -i d.txt

Page 145 of 159


Operating System

rm: remove regular empty file 'd.txt'? y


$ ls
e.txt

2. -f (Force Deletion): rm prompts for confirmation removal if a file is write-


protected. The -f option overrides this minor protection and removes the file
forcefully.
$ ls -l
total 0
-r--r--r--+ 1 User User 0 Jan 2 22:56 e.txt
$ rm e.txt
rm: remove write-protected regular empty file 'e.txt'? n
$ ls
e.txt
$ rm -f e.txt
$ ls
-f option of the rm command will not work for write-protect directories.

3. -r (Recursive Deletion): With the -r (or -R) option rm command performs a


tree walk and will delete all the files and sub-directories recursively of the
parent directory. At each stage, it deletes everything it finds. Normally, rm
wouldn’t delete the directories but when used with this option, it will delete.
Below is the tree of directories and files:
$ ls
A
$ cd A
$ ls
BC
$ ls B
a.txt b.txt
$ ls C
c.txt d.txt
Now, deletion from A directory (as parent directory) will be done as:
$ rm *
rm: cannot remove 'B': Is a directory
rm: cannot remove 'C': Is a directory
$ rm -r *
$ ls
Every directory and file inside A directory is deleted.

mv COMMAND

Page 146 of 159


Operating System

mv stands for the move. mv is used to move one or more files or directories
from one place to another in a file system like UNIX. It has two distinct functions:
(i) It renames a file or folder.
(ii) It moves a group of files to a different directory.
No additional space is consumed on a disk during renaming. This command
normally works silently means no prompt for confirmation.
Syntax: mv [Option] source destination

Examples- Let us consider 4 files having names a.txt, b.txt, and so on till d.txt. To
rename the file a.txt to geek.txt (does not exist):
$ ls
a.txt b.txt c.txt d.txt
$ mv a.txt geek.txt
$ ls
b.txt c.txt d.txt geek.txt
If the destination file doesn’t exist, it will be created. In the above command,
mv simply replaces the source filename in the directory with the destination
filename (new name). If the destination file exists, then it will be overwritten and
the source file will be deleted. By default, mv doesn’t prompt for overwriting the
existing file.
For example, moving palash.txt to b.txt(exist):
$ ls
b.txt c.txt d.txt geek.txt
$ cat palash.txt
India
$ cat b.txt
Bachelor of Computer Application
$ mv geek.txt b.txt
$ ls
b.txt c.txt d.txt
$ cat b.txt
India

Options:

-i (Interactive): Like in cp, the -i option makes the command ask the user for
confirmation before moving a file that would overwrite an existing file, you have to
press y for confirm moving, any other key leaves the file as it is. This option doesn’t
work if the file doesn’t exist, it simply renames it or moves it to a new location.
$ ls
b.txt c.txt d.txt geek.txt
$ cat palash.txt
India

Page 147 of 159


Operating System

$ cat b.txt
bca
$ mv -i geek.txt b.txt
mv: overwrite 'b.txt'? y
$ ls
b.txt c.txt d.txt
$ cat b.txt
bca

-f (Force): mv prompts for confirmation overwriting the destination file if a file is


write-protected. The -f option overrides this minor protection and overwrites the
destination file forcefully and deletes the source file.
$ ls
b.txt c.txt d.txt geek.txt
$ cat b.txt
Linux Operating System
$ ls -l b.txt
-r--r--r--+ 1 User User 13 Jan 9 13:37 b.txt
$ mv geek.txt b.txt
mv: replace 'b.txt', overriding mode 0444 (r--r--r--)? n
$ ls
b.txt c.txt d.txt geek.txt
$ mv -f geek.txt b.txt
$ ls
b.txt c.txt d.txt
$ cat b.txt
India

-n (no-clobber): With the -n option, mv prevents an existing file from being


overwritten. In the following example, the effect is for nothing to happen as a file
would be overwritten.
$ ls
b.txt c.txt d.txt geek.txt
$ cat b.txt
Linux Operating System
$ mv -n geek.txt b.txt
$ ls
b.txt c.txt d.txt geek.txt
$ cat b.txt
Linux Operating System

Page 148 of 159


Operating System

-b(backup): With this option, it is easier to take a backup of an existing file that
will be overwritten as a result of the mv command. This will create a backup file
with the tilde character (~) appended to it.
$ ls
b.txt c.txt d.txt geek.txt
$ mv -b geek.txt b.txt
$ ls
b.txt b.txt~ c.txt d.txt

cp COMMAND
cp stands for a copy. This command is used to copy files or groups of files or
directories. It creates an exact image of a file on a disk with a different file name. cp
command requires at least two filenames in its arguments.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
The first and second syntax is used to copy the Source file to the Destination
file or Directory. The third syntax is used to copy multiple Sources(files) to the
Directory.
cp command works on three principal modes of operation and these
operations depend upon the number and type of arguments passed in the cp
command:

Two file names: If the command contains two file names, then it copies the
contents of 1st file to the 2nd file. If the 2nd file doesn’t exist, then first it creates
one, and content is copied to it. But if it existed then it is simply overwritten
without any warning. So be careful when you choose the destination file name.
cp Src_file Dest_file
Suppose there is a directory named bca having a text file a.txt.
Example:
$ ls
a.txt
$ cp a.txt b.txt
$ ls
a.txt b.txt

One or more arguments: If the command has one or more arguments,


specifying file names and following those arguments, an argument specifying
directory name then this command copies each source file to the destination

Page 149 of 159


Operating System

directory with the same name, created if not exist but if already existed then it will
be overwritten.
cp Src_file1 Src_file2 Src_file3 Dest_directory
Suppose there is a directory named bca having a text file a.txt, b.txt and a directory
name new in which we are going to copy all files.
Example:
$ ls
a.txt b.txt new
Initially new is empty
$ ls new
$ cp a.txt b.txt new
$ ls new
a.txt b.txt
For this case, the last argument must be a directory name. For the above
command to work, Dest_directory must exist because the cp command won’t create
it.

Two directory names: If the command contains two directory names, cp


copies all files of the source directory to the destination directory, creating any files
or directories needed. This mode of operation requires an additional option,
typically R, to indicate the recursive copying of directories.
cp -R Src_directory Dest_directory
In the above command, cp behavior depends upon whether Dest_directory
exists or not. If the Dest_directory doesn’t exist, cp creates it and copies the content
of the Src_directory recursively as it is. But if Dest_directory exists then a copy of
Src_directory becomes a sub-directory under Dest_directory.

Options: There are many options for the cp command, here we will discuss some
of the useful options:
Suppose a directory named bca contains two files having some content
named a.txt and b.txt. This scenario is useful in understanding the following
options.
$ ls bca
a.txt b.txt
$ cat a.txt
LOS
$ cat b.txt
LOS

-i (interactive): i stands for Interactive copying. With this option system first
warns the user before overwriting the destination file. cp prompts for a response, if
you press y then it overwrites the file and with any other option leaves it uncopied.
$ cp -i a.txt b.txt

Page 150 of 159


Operating System

cp: overwrite 'b.txt'? y


$ cat b.txt
LOS

-b (backup): With this option, the cp command creates the backup of the
destination file in the same folder with a different name and a different format.
$ ls
a.txt b.txt
$ cp -b a.txt b.txt
$ ls
a.txt b.txt b.txt~

-f(force): If the system is unable to open the destination file for writing operation
because the user doesn’t have writing permission for this file then by using the -f
option with the cp command, the destination file is deleted first, and then copying
of content is done from source to destination file.
$ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt
Users, groups, and others don’t have writing permission.
Without the -f option, command not executed
$ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied
With the -f option, the command is executed successfully
$ cp -f a.txt b.txt

-r or -R: Copying directory structure. With this option, the cp command shows its
recursive behavior by copying the entire directory structure recursively.
Suppose we want to copy the bca directory containing many files, and
directories into the bca1 directory (which does not exist).
$ ls bca/
a.txt b.txt b.txt~ Folder1 Folder2
Without the -r option, the error
$ cp bca bca1
cp: -r not specified; omitting directory 'geeksforgeeks'
With -r, execute successfully
$ cp -r bca bca1
$ ls bca1/
a.txt b.txt b.txt~ Folder1 Folder2

-p(preserve): With the—p option, cp preserves the following characteristics of


each source file in the corresponding destination file: the time of the last data
modification and the time of the last access, the ownership (only if it has
permission to do this), and the file permission bits.

Page 151 of 159


Operating System

For the preservation of characteristics, you must be the root user of the
system, otherwise, characteristics change.
$ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt
$ cp -p a.txt c.txt
$ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt
As we see above both a.txt and c.txt (created by copying) have the same
characteristics.

Examples:
Copying using * wildcard: The star wildcard represents anything i.e., all files
and directories. Suppose we have many text documents in a directory and want to
copy them to another directory, it takes lots of time if we copy files 1 by 1 or the
command becomes too long if specify all these file names as the argument, but by
using * wildcard it becomes simple.
Initially, Folder1 is empty
$ ls
a.txt b.txt c.txt d.txt e.txt Folder1
$ cp *.txt Folder1
$ ls Folder1
a.txt b.txt c.txt d.txt e.txt

MANAGING USERS ACCOUNTS


A user is an entity, in a Linux operating system, that can manipulate files
and perform several other operations. Each user is assigned an ID that is unique
for each user in the operating system. In this post, we will learn about users and
commands which are used to get information about the users. After installation of
the operating system, the ID 0 is assigned to the root user, and the IDs 1 to 999
(both inclusive) are assigned to the system users hence the IDs for local users begin
from 1000 onwards.
In a single directory, we can create 60,000 users. Now we will discuss the
important commands to manage users in Linux.

1. To list out all the users in Linux, use the awk command with the -F option.
Here, we are accessing a file and printing only the first column with the help of
print $1 and awk.
awk -F':' '{ print $1}' /etc/passwd

Page 152 of 159


Operating System

2. Using the id command, you can get the ID of any username. Every user has
an id assigned to it and the user is identified with the help of this id. By
default, this id is also the group id of the user.
id username
Example: id test

3. The command to add a user. useradd command adds a new user to the
directory. The user is given the ID automatically depending on which category
it falls in. The username of the user will be as provided by us in the command.
sudo useradd username
Example: sudo useradd geeks

Page 153 of 159


Operating System

4. Using the passwd command to assign a password to a user. After using this
command we have to enter the new password for the user and then the
password gets updated to the new password.
passwd username
Example: passwd geeks

5. Accessing a user configuration file.


cat /etc/passwd
This command prints the data of the configuration file. This file contains
information about the user in the format.
username : x : user id : user group id : : /home/username : /bin/bash

Now we will go through the commands to modify information.

6. The command to change the user ID for a user.


usermod -u new_id username
This command can change the user ID of a user. The user with the given
username will be assigned with the new ID given in the command and the old
ID will be removed.
Example: sudo usermod -u 1982 test

Page 154 of 159


Operating System

7. Command to Modify the group ID of a user.


usermod -g new_group_id username
This command can change the group ID of a user and hence it can even be
used to move a user to an already existing group. It will change the group ID of
the user whose username is given and set the group ID as the given
new_group_id.
Example: sudo usermod -g 1005 test

8. You can change the user login name using the usermod command. The below
command is used to change the login name of the user. The old login name of
the user is changed to the new login name provided.
sudo usermod -l new_login_name old_login_name
Example: sudo usermod -c John_Wick John_Doe

9. The command to change the home directory. The below command changes
the home directory of the user whose username is given and sets the new home
directory as the directory whose path is provided.
usermod -d new_home_directory_path username
Example: usermod -d new_home_directory test

Page 155 of 159


Operating System

10. You can also delete a user name. The below command deletes the user whose
username is provided. Make sure that the user is not part of a group. If the
user is part of a group then it will not be deleted directly, hence we will have to
first remove him from the group and then we can delete him.
sudo userdel -r username
Example: sudo userdel -r new_geeks

CHANGING PASSWORD
Both Unix and Linux-like operating systems apply the passwd commands for
changing the password of the user. The passwd command is used for updating the
authentication password or token of the user which is saved in the /etc/shadow
file.
The passwd command changes the passwords for group accounts and users.
A normal user might only modify the password for her/his account, the root user
or super user might modify the password for other accounts.
The group administrator might modify the password for a group. Also, the
passwd command changes the information of an account like the user's full name,
the login shell of the user, or the interval and expiry date of the password.
Type the following passwd command for changing our password:
$ passwd
Initially, the user is requested for her/his previous password when one is
available. The password is further compared and encrypted. The password is
compared to the stored or saved password.
The user has only a single chance of entering an accurate password. The
root user is allowed to bypass this step. Hence, the forgotten passwords might be
modified. A newer password is checked for complexity. The passwords must be a

Page 156 of 159


Operating System

combination of 10 to 20 characters containing multiple characters from all of the


below sets as a normal guideline:
Upper case alphabetic
Lower case alphabetic
Digits 0 to 9
Special characters or punctuation marks

Other account change password in Linux- If we want to change the


password for other accounts of the user in Linux, we have to log in as the super
user. Then, we have to type the below command to change the password for user
palash:
$ passwd Palash
Changing password for palash.
Current password:
New password:
OR
$ sudo passwd palash
Output:
[sudo] passwd for Palash:
New password:

Linux changes the password of a group- The password for a named


group will be changed when we use the -g option. In this example, we will change
the password for group1 group:
# passwd -g group1
The option -r is applied with the option -g for removing the latest password
through the named group. It permits group access to every member. Option -R is
applied with option -g for restricting the named group for every user.

Modifying user passwords in Linux- As a Linux sysadmin (system


administrator) we can change the password for the users on our server. For
changing the password on behalf of any user:
1. First of all, sign on "sudo" or "su" to the "super" or "root" account in Linux.
2. After that enter the following command for changing the password for john
user:
passwd john
3. The system will request us to type the password twice.
For changing or setting a new super user or root password, we have to enter
the following command:
$ sudo passwd

Page 157 of 159


Operating System

Forcing users to change the password at the next login- Linux


passwords by default never expire for any users. But we can also force the users to
change the password they log in by CLI or GUI methods next time. The syntax is as
follows:
$ sudo passwd -e {username}
$ sudo passwd --expire {username}
Let us expire the password of an account immediately, we can use the following
command:
$ sudo passwd -e Palash
Output-
$sudo passwd –expire Palash
Passwd-password expiry information changed
The system would confirm this:

Lock and unlock user passwords


Lock password: By using the following command, we can lock the user password:
$ sudo passwd -1 {username}
This option will disable the password by modifying it to any value which is
not matching to the possible encrypted value. It will add the ! sign at the start of
the password inside the /etc/shadow file.

Unlock password: We can use the following command to unlock a password:


$ sudo passwd -u {username}
The above option command will re-enable the password by modifying the
password back to the previous value.

CREATING GROUP ACCOUNTS


Groups in Linux refer to the user groups. In Linux, there can be many users
of a single system, (a normal user can take uid from 1000 to 60000, and one root
user (uid 0) and 999 system users (uid 1 to 999)). In a scenario where there are
many users, there might be some privileges that some users have and some don’t,
and it becomes difficult to manage all the permissions at the individual user level.
So, using groups, we can group several users, and set privileges and permissions
for the entire group. groupadd command is used to create a new user group.
Syntax: groupadd [option] group_name

Example: sudo groupadd developers


Every new group created is registered in the file “/etc/group“. To verify that
the group has been created, enter the command
$sudo tail /etc/group
Output: The file shows group information in the following format:

Page 158 of 159


Operating System

group_name: password: group-id: list-of-members

Options:
➢ -f, –force: This option forces the command to silently abort if the group with
the given already exists. If this option is used with the -g or –gid option and the
group id given already exists, the command forcefully ignores the given group
id and creates a new and unique group id.
Example- $groupadd developers
Goupadd: group ‘developers’ already exists.
$groupadd -f developers
$
➢ -g, –gid GID: This option is used to provide a group id (numeric) to the new
group, and it should be non-negative and unique unless explicitly created to be
non-unique (using the -o option). If this option is not used, the default id is
assigned, which is greater than every other group already present.
➢ -h, –help: Display help message and exit.
➢ -K, –key KEY=VALUE: Overrides /etc/login.defs defaults (GID_MIN,
GID_MAX and others). Multiple -K options can be specified. GID_MIN and
GID_MAX are the parameters set in /etc/login.defs which defines the minimum
and maximum values that a group id can take.
Example: groupadd -K GID_MIN=500 -K GID_MAX=700
➢ -o, –non-unique: This option permits to add of a group with a non-unique
GID.
➢ -p, –password PASSWORD: The encrypted password, as returned by crypt
(3). The default is to disable the password. This password is visible to the
users. You should make sure the password respects the system’s password
policy. The group passwords are stored in the “/etc/gshadow” file.
➢ -r, –system: Create a system group. The numeric identifiers of new system
groups are chosen in the SYS_GID_MIN-SYS_GID_MAX range, defined in
login.defs, instead of GID_MIN and GID_MAX.
➢ -R, –root CHROOT_DIR: Apply changes in the CHROOT_DIR directory and
use the configuration files from the CHROOT_DIR directory.

Page 159 of 159

You might also like