0% found this document useful (0 votes)
99 views

OS

The document provides an overview of operating systems. It defines an operating system as software that manages computer hardware and acts as an interface between users and computers. Operating systems allocate resources, manage processes and memory, provide file systems and I/O management, and offer security features and user interfaces. The history and types of operating systems are also summarized, including batch, time-sharing, distributed, network, and real-time systems.

Uploaded by

Sonu Lakra
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)
99 views

OS

The document provides an overview of operating systems. It defines an operating system as software that manages computer hardware and acts as an interface between users and computers. Operating systems allocate resources, manage processes and memory, provide file systems and I/O management, and offer security features and user interfaces. The history and types of operating systems are also summarized, including batch, time-sharing, distributed, network, and real-time systems.

Uploaded by

Sonu Lakra
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/ 100

Introduction of Operating System

An operating system acts as an intermediary between the user of a computer and


computer hardware. The purpose of an operating system is to provide an
environment in which a user can execute programs conveniently and efficiently.
An operating system is software that manages computer hardware. The hardware
must provide appropriate mechanisms to ensure the correct operation of the
computer system and to prevent user programs from interfering with the proper
operation of the system.
Operating System: Definition:
• An operating system is a program that controls the execution of
application programs and acts as an interface between the user of a
computer and the computer hardware.
• A more common definition is that the operating system is the one
program running at all times on the computer (usually called the kernel),
with all else being application programs.
• An operating system is concerned with the allocation of resources and
services, such as memory, processors, devices, and information. The
operating system correspondingly includes programs to manage these
resources, such as a traffic controller, a scheduler, a memory
management module, I/O programs, and a file system.
Features of Operating system: Operating system has the following features:
1. Convenience: An OS makes a computer more convenient to use.
2. Efficiency: An OS allows the computer system resources to be used
efficiently.
3. Ability to Evolve: An OS should be constructed in such a way as to
permit the effective development, testing, and introduction of new
system functions at the same time without interfering with service.
4. Throughput: An OS should be constructed so that It can give
maximum throughput(Number of tasks per unit time).
Major Functionalities of Operating System:
• Resource Management: When parallel accessing happens in the OS
means when multiple users are accessing the system the OS works as
Resource Manager, Its responsibility is to provide hardware to the user.
It decreases the load in the system.
• Process Management: It includes various tasks like scheduling and
termination of the process. It is done with the help of CPU
Scheduling algorithms.
• Storage Management: The file system mechanism used for the
management of the storage. NIFS, CFS, CIFS, NFS, etc. are some file
systems. All the data is stored in various tracks of Hard disks that are
all managed by the storage manager. It included Hard Disk.
• Memory Management: Refers to the management of primary memory.
The operating system has to keep track of how much memory has been
used and by whom. It has to decide which process needs memory space
and how much. OS also has to allocate and deallocate the memory space.
• Security/Privacy Management: Privacy is also provided by the
Operating system by means of passwords so that unauthorized
applications can’t access programs or data. For example, Windows
uses Kerberos authentication to prevent unauthorized access to data.
The process operating system as User Interface:
1. User

1
2. System and application programs
3. Operating system
4. Hardware
Every general-purpose computer consists of hardware, an operating system(s),
system programs, and application programs. The hardware consists of memory,
CPU, ALU, I/O devices, peripheral devices, and storage devices. The system
program consists of compilers, loaders, editors, OS, etc. The application program
consists of business programs and database programs.

Fig1: Conceptual view of a computer system


Every computer must have an operating system to run other programs. The
operating system coordinates the use of the hardware among the various system
programs and application programs for various users. It simply provides an
environment within which other programs can do useful work.
The operating system is a set of special programs that run on a computer system
that allows it to work properly. It performs basic tasks such as recognizing input
from the keyboard, keeping track of files and directories on the disk, sending
output to the display screen, and controlling peripheral devices.
OS is designed to serve two basic purposes:
1. It controls the allocation and use of the computing System’s resources
among the various user and tasks.
2. It provides an interface between the computer hardware and the
programmer that simplifies and makes it feasible for coding and
debugging of application programs.
The Operating system must support the following tasks. The tasks are:
1. Provides the facilities to create and modify of programs and data files
using an editor.
2. Access to the compiler for translating the user program from high-level
language to machine language.
3. Provide a loader program to move the compiled program code to the
computer’s memory for execution.
4. Provide routines that handle the details of I/O programming.
I/O System Management: The module that keeps track of the status of devices
is called the I/O traffic controller. Each I/O device has a device handler that
resides in a separate process associated with that device.
The I/O subsystem consists of

2
• A memory Management component that includes buffering caching and
spooling.
• A general device driver interface.
Drivers for specific hardware devices.
Assembler: The input to an assembler is an assembly language program. The
output is an object program plus information that enables the loader to prepare
the object program for execution. At one time, the computer programmer had at
his disposal a basic machine that interpreted, through hardware, certain
fundamental instructions. He would program this computer by writing a series
of ones and Zeros (Machine language), and place them into the memory of the
machine. Examples of assembly languages include
Compiler and Interpreter: The High-level languages- examples are C, C++, Java,
Python etc (around 300+ famous high level languages) are processed by
compilers and interpreters. A compiler is a program that accepts a source
program in a “high-level language “and produces machine code in one go. Some
of the compiled languages are FORTRAN, COBOL, C, C++ , Rust and Go. An
interpreter is a program that does the same thing but converts high-level code
to machine code line-by-line and not all at once. Example of interpreted
languages are Python, Perl and Ruby.
Loader: A Loader is a routine that loads an object program and prepares it for
execution. There are various loading schemes: absolute, relocating, and direct-
linking. In general, the loader must load, relocate and link the object program.
The loader is a program that places programs into memory and prepares them
for execution. In a simple loading scheme, the assembler outputs the machine
language translation of a program on a secondary device and a loader places it
in the core. The loader places into memory the machine language version of the
user’s program and transfers control to it. Since the loader program is much
smaller than the assembler, those make more core available to the user’s
program.
History of Operating system: The operating system has been evolving through
the years. The following table shows the history of OS.

Generation Year Electronic device used Types of OS Devices


First 1945-55 Vacuum Tubes Plug Boards
Second 1955-65 Transistors Batch Systems
Third 1965-80 Integrated Circuits(IC) Multiprogramming
Fourth Since 1980 Large Scale Integration PC

Types of Operating Systems


• Batch Operating System- Sequence of jobs in a program on a computer
without manual interventions.
• Time-sharing operating System- allows many users to share computer
resources. (Max utilization of the resources).
• Distributed operating System- Manages a group of different computers
and makes appear to be a single computer.
• Network operating system- computers running in different operating
systems can participate in a common network (It is used for security
purposes).
• Real-time operating system – used where any or all the jobs have strict
time constraints.

3
Features of operating systems:

Memory Management: The operating system manages the computer’s memory,


ensuring that programs have the necessary memory to run and allocating
memory efficiently to maximize performance.
Process Management: The operating system is responsible for managing the
processes running on the computer. It schedules processes to run on the CPU,
allocates system resources to processes, and terminates processes when they are
no longer needed.
File System Management: The operating system manages the file system, which
is responsible for storing and retrieving files on the computer’s hard disk or
other storage devices.
Device Management: The operating system manages the computer’s input and
output devices, such as the keyboard, mouse, and printer, ensuring that they are
properly configured and functioning correctly.
Security: The operating system provides security features to protect the
computer from unauthorized access, viruses, and other types of malware.
User Interface: The operating system provides a graphical user interface (GUI)
or a command-line interface (CLI) to interact with the computer, making it easier
for users to access and use the computer’s resources.
Networking: The operating system provides networking features that enable
computers to communicate with each other over a network, allowing users to
share resources and collaborate with others.
Examples of Operating Systems are as follows:
•Windows (GUI-based, PC)
•GNU/Linux (Personal, Workstations, ISP, File, and print server, Three-
tier client/Server)
• macOS (Macintosh), used for Apple’s personal computers and
workstations (MacBook, iMac).
• Android (Google’s Operating System for
smartphones/tablets/smartwatches)
• iOS (Apple’s OS for iPhone, iPad, and iPod Touch)
HOW OS TAKES SYSTEM CONTROL:-
All computers rely on an OS to provide the interface for interaction between users,
applications, and hardware. The OS boots the computer and manages the file
system. Almost all modern operating systems can support more than one user,
task, or CPU.
To understand the capabilities of an operating system, it is important to
understand some basic terms. The following terms are often used when comparing
operating systems:
• Multi-user – Two or more users can work with programs and share peripheral
devices, such as printers, at the same time.
• Multi-tasking – The computer is capable of operating multiple applications at the
same time.
• Multi-processing – The computer can have two or more central processing units
(CPUs) that programs share.
• Multi-threading – A program can be broken into smaller parts that can be loaded
as needed by the operating system. Multi-threading allows individual programs to
be multi-tasked.

4
A technician might be asked to choose and install an operating system for a
customer. The type of OS selected depends on the customer's requirements for
the computer. There are two distinct types of operating systems: desktop
operating systems and network operating systems. A desktop operating system is
intended for use in a Small Office/Home Office (SOHO) with a limited number of
users. A Network Operating System (NOS) is designed for a corporate environment
serving multiple users with a wide range of needs.
A network OS has the following characteristics:
• Supports multiple users
• Runs multi-user applications
• Is robust and redundant
• Provides increased security compared to desktop operating systems
These are the most common network operating systems:
• Microsoft Windows – Network operating systems offered by Microsoft are
Windows 2000 Server, Windows Server 2003, and Windows Server 2008. Windows
Server operating systems use a central database called Active Directory to manage
network resources.
• Novell NetWare – Novell NetWare was the first OS to meet network OS
requirements and enjoy widespread deployment in PC-based Local Area Networks
(LANs) back in the 1980s.
• Linux – Linux operating systems include Red Hat, Caldera, SUSE, Debian, Fedora,
Ubuntu, and Slackware.
• UNIX – Various corporations offer proprietary operating systems based on UNIX.
Why OS is essential :-
The operating system (OS) acts as a manager for all the I/O device, memory, CPU,
file storage resources and allocates them to specific programs and users,
whenever necessary to perform a particular task. Therefore, the operating system
is the resource manager that means it can manage the resources of a computer
system internally.
The operating systems are important and should be correctly used when writing
the user applications. Large and complex systems have high economic impact and
this result in interesting problems of management.
Few systems are involved in the design and implementation of OS but,
nevertheless many general techniques have to be learnt and applied.
Operating systems combine concepts from many other areas of Computer
Science: Architecture, Languages, Data Structures, Algorithms, etc.
So finally, we can say that an operating system is the most important software
that runs on any system. It manages a computer's resources like memory,
processes involved, software and hardware.
Operating systems provide an interface to communicate with the computer
without learning the computer's language i.e. machine language. Basically, an
operating system provides a layer between the program and hardware to enable
a program to use a standard interface irrespective of what hardware is used.

5
Operating System does the following
An operating system provides a user interface by which we can run our
applications on computer hardware. It depends on the point of view.
Users always think about convenience, ease of use and good performance and do
not care about resource utilization.
OS shared computers like mainframe or minicomputer making all users happy.
OS dedicated system users such as workstations have dedicated resources but
frequently use shared resources from servers.
There is little or no user interface for some computer systems, such as embedded
computers in devices and automobiles.
Functions of operating system
The functions of an operating system are as follows−
•Provide a user interface
•Resource Management − An OS manages a computer's resources like
memory, processes involved, etc very efficiently.
• Handles I/O operations
• Security − Operating system protects the data and resources of
computer
• Helps in coordination between software and users.
Operating System Goals
The operating system has mainly three goals which are convenience, efficiency,
and capability of evolution. A more complete explanation is as follows −
• Convenience makes computer use more comfortable.
• We can efficiently use the resources of a computer system with the
help of an operating system.
• There is a capability of evolution, in building an operating system it is
possible to make it easier to develop, test, and use new systems.

Evolution of Operating System


Operating System is a type of software that acts as an interface between the user
and the hardware. It is responsible to handle various critical functions of the
computer or any other machine. Various tasks that are handled by OS are file
management, task management, garbage management, memory management,
process management, disk management, I/O management, peripherals
management, etc.

Evolution of Operating System:

Operating Systems, has evolved in past years. It went through several changes
before getting its original form. These changes in the operating system are
known as the evolution of operating systems. The evolution of the operating
system went through four generations. Let us see these generations in detail:

First Generation:

• This phase is considered from 1945-1955. Earlier mechanical systems


were used which involved the use of large machines whose parts were
manually handled by workers. The limited capacity to work of a human

6
and the tendency to commit an error by a person often lead to problems.
Thus electronic machines were introduced to do the work. Though these
machines did not use an OS at all still this phase is regarded as the
beginning of the Operating Systems era.
• The electronic machines supplied instructions that were to be executed
by the machine immediately because if an error occurred, the whole
process was to be restarted again. The calculation speed in these
machines was limited and still errors could occur if there was any error
in the instruction provided to the machine. These systems were referred
to as serial processing systems.

Second Generation:

• The phase from 1955 to 1965 marked the second generation of


Operating Systems. The systems in this generation were regarded as
Batch Systems. These were known as batch operating systems as the
jobs to be done were supplied in a batch to the machine. A Batch
referred to a set of similar tasks or jobs. Job Control Language was used
to create the instructions to execute the job. The instructions were
punched on a card which was then loaded onto a tape that had several
such cards and then it was finally sent to the processor.
• The second generation of OS ensured that the machines remained as
busy as possible so that maximum efficiency could be obtained. The
machine took up a job, process the instructions, and then move to the
next job that was present on the card loaded onto the tape. If an error
occurred during a particular job, then the job needs to be restarted
again.
• These systems made it possible to execute multiple jobs on a single
machine and laid the foundation for multitasking.
• These systems had a drawback in that when the I/O operations were
carried out, the processor had to remain idle.

Third Generation:

• The phase from 1965-1980 is considered as the third generation of OS.


It saw the rise of multi-programmed batched systems. These systems
were very similar to the batched operating systems. These systems had
the ability of multitasking and multiprogramming where the tasks of
multiple users could be run simultaneously.
• Users could connect to the machine and submit their tasks through an
online terminal. The OS held all the tasks in the main memory and
managed the tasks to be executed using various scheduling algorithms
such as FCFS, SJF, LJF, etc. This also ensured that the tasks or jobs do
not suffer from starvation.

Fourth Generation:

• 1980 onwards, the generation of OS is known as the fourth generation.


With the development of computer networking and various networking
protocols, these operating systems allowed the users to know the
existence of other users on the network.

7
• The operating systems in this generation saw the use of a Graphical User
Interface (GUI) which made it very easy to interact with the operating
system and in turn with the hardware.
• The fourth generation of operating systems saw the invention of time-
shared operating systems and the Macintosh operating systems. Let us
see about the time-shared operating systems and the Macintosh
operating systems in brief.
In other operating systems, many times users felt that their work was not being
done as some tasks took too long to get completed. This lead to starvation of
processes or jobs. Time-shared operating systems solved this problem by
ensuring that no user feels that his work is not being done. These operating
systems made use of scheduling algorithms such as Round Robin in which the
work of a user was done for a specific time and then the OS moved to the next
job. Thus in actuality, these OS shared the time between multiple users and
hence got the name Time Shared Operating Systems.
These types of operating systems paved the way for Mac and Windows OS. The
use of personal computers grew as the OS evolved more and more and the
computers became more user-centric instead of being shared by multiple users.
Personal computers became increasingly cheap as the hardware costs decreased.
As the hardware evolved more and more, the color Macs were developed and
Microsoft implemented Windows based on the Graphical User Interface operating
system.

Operating System Generations:-


Operating Systems have evolved over the years. So, their evolution through the
years can be mapped using generations of operating systems. There are four
generations of operating systems. These can be described as follows −
The First Generation ( 1945 - 1955 ): Vacuum Tubes and Plugboards
Digital computers were not constructed until the second world war. Calculating
engines with mechanical relays were built at that time. However, the mechanical
relays were very slow and were later replaced with vacuum tubes. These machines
were enormous but were still very slow.
These early computers were designed, built and maintained by a single group of
people. Programming languages were unknown and there were no operating
systems so all the programming was done in machine language. All the problems
were simple numerical calculations.
By the 1950’s punch cards were introduced and this improved the computer
system. Instead of using plugboards, programs were written on cards and read
into the system.
The Second Generation ( 1955 - 1965 ): Transistors and Batch Systems
Transistors led to the development of the computer systems that could be
manufactured and sold to paying customers. These machines were known as
mainframes and were locked in air-conditioned computer rooms with staff to
operate them.
The Batch System was introduced to reduce the wasted time in the computer. A
tray full of jobs was collected in the input room and read into the magnetic tape.
After that, the tape was rewound and mounted on a tape drive. Then the batch
operating system was loaded in which read the first job from the tape and ran it.
The output was written on the second tape. After the whole batch was done, the
input and output tapes were removed and the output tape was printed.

8
The Third Generation ( 1965 - 1980 ): Integrated Circuits and
Multiprogramming
Until the 1960’s, there were two types of computer systems i.e the scientific and
the commercial computers. These were combined by IBM in the System/360. This
used integrated circuits and provided a major price and performance advantage
over the second generation systems.
The third generation operating systems also introduced multiprogramming. This
meant that the processor was not idle while a job was completing its I/O
operation. Another job was scheduled on the processor so that its time would not
be wasted.
The Fourth Generation ( 1980 - Present ): Personal Computers
Personal Computers were easy to create with the development of large-scale
integrated circuits. These were chips containing thousands of transistors on a
square centimeter of silicon. Because of these, microcomputers were much
cheaper than minicomputers and that made it possible for a single individual to
own one of them.
The advent of personal computers also led to the growth of networks. This
created network operating systems and distributed operating systems. The users
were aware of a network while using a network operating system and could log in
to remote machines and copy files from one machine to another.

Operating System Structure:-


An operating system is a construct that allows the user application programs to
interact with the system hardware. Since the operating system is such a complex
structure, it should be created with utmost care so it can be used and modified
easily. An easy way to do this is to create the operating system in parts. Each of
these parts should be well defined with clear inputs, outputs and functions.
Simple Structure
There are many operating systems that have a rather simple structure. These
started as small systems and rapidly expanded much further than their scope. A
common example of this is MS-DOS. It was designed simply for a niche amount
for people. There was no indication that it would become so popular.
An image to illustrate the structure of MS-DOS is as follows −

9
It is better that operating systems have a modular structure, unlike MS-DOS. That
would lead to greater control over the computer system and its various
applications. The modular structure would also allow the programmers to hide
information as required and implement internal routines as they see fit without
changing the outer specifications.
Layered Structure
One way to achieve modularity in the operating system is the layered approach.
In this, the bottom layer is the hardware and the topmost layer is the user
interface.
An image demonstrating the layered approach is as follows −

As seen from the image, each upper layer is built on the bottom layer. All the
layers hide some structures, operations etc from their upper layers.
One problem with the layered structure is that each layer needs to be carefully
defined. This is necessary because the upper layers can only use the
functionalities of the layers below them.
Operating System – Processes:-
A process is basically a program in execution. The execution of a process must
progress in a sequential fashion.
A process is defined as an entity which represents the basic unit of work to be
implemented in the system.
To put it in simple terms, we write our computer programs in a text file and when
we execute this program, it becomes a process which performs all the tasks
mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image shows
a simplified layout of a process inside main memory −

10
S.N. Component & Description
1 Stack
The process Stack contains the temporary data such as
method/function parameters, return address and local variables.
2
Heap
This is dynamically allocated memory to a process during its run
time.
3 Text
This includes the current activity represented by the value of
Program Counter and the contents of the processor's registers.
4
Data
This section contains the global and static variables.
Program
A program is a piece of code which may be a single line or millions of lines. A
computer program is usually written by a computer programmer in a
programming language. For example, here is a simple program written in C
programming language −

#include <stdio.h>

int main() {
printf("Hello, World! \n");
return 0;
}

11
A computer program is a collection of instructions that performs a specific task
when executed by a computer. When we compare a program with a process, we
can conclude that a process is a dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as
an algorithm. A collection of computer programs, libraries and related data are
referred to as a software.
Process Life Cycle
When a process executes, it passes through different states. These stages may
differ in different operating systems, and the names of these states are also not
standardized.
In general, a process can have one of the following five states at a time.
S.N. State & Description
1 Start
This is the initial state when a process is first started/created.
2 Ready
The process is waiting to be assigned to a processor. Ready
processes are waiting to have the processor allocated to them by
the operating system so that they can run. Process may come into
this state after Start state or while running it by but interrupted
by the scheduler to assign CPU to some other process.
3
Running
Once the process has been assigned to a processor by the OS
scheduler, the process state is set to running and the processor
executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a
resource, such as waiting for user input, or waiting for a file to
become available.
5
Terminated or Exit
Once the process finishes its execution, or it is terminated by the
operating system, it is moved to the terminated state where it
waits to be removed from main memory.

12
Components of Operating System:-
There are various components of an Operating System to perform well defined
tasks. Though most of the Operating Systems differ in structure but logically they
have similar components. Each component must be a well-defined portion of a
system that appropriately describes the functions, inputs, and outputs.
There are following 8-components of an Operating System:

1. Process Management
2. I/O Device Management
3. File Management
4. Network Management
5. Main Memory Management
6. Secondary Storage Management
7. Security Management
8. Command Interpreter System

Following section explains all the above components in more detail:


Process Management
A process is program or a fraction of a program that is loaded in main memory. A
process needs certain resources including CPU time, Memory, Files, and I/O
devices to accomplish its task. The process management component manages the
multiple processes running simultaneously on the Operating System.
A program in running state is called a process.
The operating system is responsible for the following activities in connection with
process management:

• Create, load, execute, suspend, resume, and terminate processes.


• Switch system among multiple processes in main memory.
• Provides communication mechanisms so that processes can communicate
with each others
• Provides synchronization mechanisms to control concurrent access to
shared data to keep shared data consistent.
• Allocate/de-allocate resources properly to prevent or avoid deadlock
situation.

I/O Device Management


One of the purposes of an operating system is to hide the peculiarities of specific
hardware devices from the user. I/O Device Management provides an abstract
level of H/W devices and keep the details from applications to ensure proper use
of devices, to prevent errors, and to provide users with convenient and efficient
programming environment.
Following are the tasks of I/O Device Management component:

• Hide the details of H/W devices


• Manage main memory for the devices using cache, buffer, and spooling
• Maintain and provide custom drivers for each device.

13
File Management
File management is one of the most visible services of an operating system.
Computers can store information in several different physical forms; magnetic
tape, disk, and drum are the most common forms.
A file is defined as a set of correlated information and it is defined by the creator
of the file. Mostly files represent data, source and object forms, and programs.
Data files can be of any type like alphabetic, numeric, and alphanumeric.
A files is a sequence of bits, bytes, lines or records whose meaning is defined by
its creator and user.
The operating system implements the abstract concept of the file by managing
mass storage device, such as types and disks. Also files are normally organized
into directories to ease their use. These directories may contain files and other
directories and so on.
The operating system is responsible for the following activities in connection with
file management:

• File creation and deletion


• Directory creation and deletion
• The support of primitives for manipulating files and directories
• Mapping files onto secondary storage
• File backup on stable (nonvolatile) storage media

Network Management
The definition of network management is often broad, as network management
involves several different components. Network management is the process of
managing and administering a computer network. A computer network is a
collection of various types of computers connected with each other.
Network management comprises fault analysis, maintaining the quality of service,
provisioning of networks, and performance management.
Network management is the process of keeping your network healthy for an
efficient communication between different computers.
Following are the features of network management:

• Network administration
• Network maintenance
• Network operation
• Network provisioning
• Network security

Main Memory Management


Memory is a large array of words or bytes, each with its own address. It is a
repository of quickly accessible data shared by the CPU and I/O devices.
Main memory is a volatile storage device which means it loses its contents in the
case of system failure or as soon as system power goes down.
The main motivation behind Memory Management is to maximize memory
utilization on the computer system.

14
The operating system is responsible for the following activities in connections
with memory management:

• Keep track of which parts of memory are currently being used and by whom.
• Decide which processes to load when memory space becomes available.
• Allocate and deallocate memory space as needed.

Secondary Storage Management


The main purpose of a computer system is to execute programs. These programs,
together with the data they access, must be in main memory during execution.
Since the main memory is too small to permanently accommodate all data and
program, the computer system must provide secondary storage to backup main
memory.
Most modern computer systems use disks as the principle on-line storage
medium, for both programs and data. Most programs, like compilers, assemblers,
sort routines, editors, formatters, and so on, are stored on the disk until loaded
into memory, and then use the disk as both the source and destination of their
processing.
The operating system is responsible for the following activities in connection with
disk management:

• Free space management


• Storage allocation

Disk scheduling
Security Management
The operating system is primarily responsible for all task and activities happen in
the computer system. The various processes in an operating system must be
protected from each other’s activities. For that purpose, various mechanisms
which can be used to ensure that the files, memory segment, cpu and other
resources can be operated on only by those processes that have gained proper
authorization from the operating system.
Security Management refers to a mechanism for controlling the access of
programs, processes, or users to the resources defined by a computer controls to
be imposed, together with some means of enforcement.
For example, memory addressing hardware ensure that a process can only execute
within its own address space. The timer ensure that no process can gain control
of the CPU without relinquishing it. Finally, no process is allowed to do it’s own
I/O, to protect the integrity of the various peripheral devices.
Command Interpreter System
One of the most important component of an operating system is its command
interpreter. The command interpreter is the primary interface between the user
and the rest of the system.
Command Interpreter System executes a user command by calling one or more
number of underlying system programs or system calls.
Command Interpreter System allows human users to interact with the Operating
System and provides convenient programming environment to the users.

15
Many commands are given to the operating system by control statements. A
program which reads and interprets control statements is automatically executed.
This program is called the shell and few examples are Windows DOS command
window, Bash of Unix/Linux or C-Shell of Unix/Linux.
Structures of Operating Systems:-
Operating system can be implemented with the help of various structures. The
structure of the OS depends mainly on how the various common components of
the operating system are interconnected and melded into the kernel. Depending
on this we have following structures of the operating system:
Simple structure:
Such operating systems do not have well defined structure and are small, simple
and limited systems. The interfaces and levels of functionality are not well
separated. MS-DOS is an example of such operating system. In MS-DOS
application programs are able to access the basic I/O routines. These types of
operating system cause the entire system to crash if one of the user programs
fails.
Diagram of the structure of MS-DOS is shown below.

Advantages of Simple structure:


• It delivers better application performance because of the few interfaces
between the application program and the hardware.
• Easy for kernel developers to develop such an operating system.
Disadvantages of Simple structure:
• The structure is very complicated as no clear boundaries exists between
modules.
• It does not enforce data hiding in the operating system.
Layered structure:
An OS can be broken into pieces and retain much more control on system. In this
structure the OS is broken into number of layers (levels). The bottom layer (layer
0) is the hardware and the topmost layer (layer N) is the user interface. These
layers are so designed that each layer uses the functions of the lower level layers
only. This simplifies the debugging process as if lower level layers are debugged
and an error occurs during debugging then the error must be on that layer only
as the lower level layers have already been debugged.

16
The main disadvantage of this structure is that at each layer, the data needs to
be modified and passed on which adds overhead to the system. Moreover careful
planning of the layers is necessary as a layer can use only lower level layers.
UNIX is an example of this structure.

Advantages of Layered structure:


• Layering makes it easier to enhance the operating system as
implementation of a layer can be changed easily without affecting the
other layers.
• It is very easy to perform debugging and system verification.
Disadvantages of Layered structure:
• In this structure the application performance is degraded as compared
to simple structure.
• It requires careful planning for designing the layers as higher layers use
the functionalities of only the lower layers.
Micro-kernel:
This structure designs the operating system by removing all non-essential
components from the kernel and implementing them as system and user
programs. This result in a smaller kernel called the micro-kernel.
Advantages of this structure are that all new services need to be added to user
space and does not require the kernel to be modified. Thus it is more secure and
reliable as if a service fails then rest of the operating system remains untouched.
Mac OS is an example of this type of OS.
Advantages of Micro-kernel structure:
• It makes the operating system portable to various platforms.
• As microkernels are small so these can be tested effectively.
Disadvantages of Micro-kernel structure:
• Increased level of inter module communication degrades system
performance.
Modular structure or approach:
It is considered as the best approach for an OS. It involves designing of a modular
kernel. The kernel has only set of core components and other services are added
as dynamically loadable modules to the kernel either during run time or boot
time. It resembles layered structure due to the fact that each kernel has defined
and protected interfaces but it is more flexible than the layered structure as a

17
module can call any other module.
For example Solaris OS is organized as shown in the figure.

Operating System Services:-


An Operating System provides services to both the users and to the programs.

• It provides programs an environment to execute.


• It provides users the services to execute the programs in a convenient
manner.
Following are a few common services provided by an operating system −

• Program execution
• I/O operations
• File System manipulation
• Communication
• Error Detection
• Resource Allocation
• Protection
Program execution
Operating systems handle many kinds of activities from user programs to system
programs like printer spooler, name servers, file server, etc. Each of these
activities is encapsulated as a process.
A process includes the complete execution context (code to execute, data to
manipulate, registers, OS resources in use). Following are the major activities of
an operating system with respect to program management −

• Loads a program into memory.


• Executes the program.
• Handles program's execution.
• Provides a mechanism for process synchronization.
• Provides a mechanism for process communication.
• Provides a mechanism for deadlock handling.

18
I/O Operation
An I/O subsystem comprises of I/O devices and their corresponding driver
software. Drivers hide the peculiarities of specific hardware devices from the
users.
An Operating System manages the communication between user and device
drivers.

• I/O operation means read or write operation with any file or any
specific I/O device.
• Operating system provides the access to the required I/O device when
required.
File system manipulation
A file represents a collection of related information. Computers can store files on
the disk (secondary storage), for long-term storage purpose. Examples of storage
media include magnetic tape, magnetic disk and optical disk drives like CD, DVD.
Each of these media has its own properties like speed, capacity, data transfer rate
and data access methods.
A file system is normally organized into directories for easy navigation and usage.
These directories may contain files and other directions. Following are the major
activities of an operating system with respect to file management −

• Program needs to read a file or write a file.


• The operating system gives the permission to the program for
operation on file.
• Permission varies from read-only, read-write, denied and so on.
• Operating System provides an interface to the user to create/delete
files.
• Operating System provides an interface to the user to create/delete
directories.
• Operating System provides an interface to create the backup of file
system.
Communication
In case of distributed systems which are a collection of processors that do not
share memory, peripheral devices, or a clock, the operating system manages
communications between all the processes. Multiple processes communicate with
one another through communication lines in the network.
The OS handles routing and connection strategies, and the problems of contention
and security. Following are the major activities of an operating system with
respect to communication −

• Two processes often require data to be transferred between them


• Both the processes can be on one computer or on different computers,
but are connected through a computer network.
• Communication may be implemented by two methods, either by
Shared Memory or by Message Passing.

19
Error handling
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices
or in the memory hardware. Following are the major activities of an operating
system with respect to error handling −

• The OS constantly checks for possible errors.


• The OS takes an appropriate action to ensure correct and consistent
computing.
Resource Management
In case of multi-user or multi-tasking environment, resources such as main
memory, CPU cycles and files storage are to be allocated to each user or job.
Following are the major activities of an operating system with respect to resource
management −

• The OS manages all kinds of resources using schedulers.


• CPU scheduling algorithms are used for better utilization of CPU.
Protection
Considering a computer system having multiple users and concurrent execution
of multiple processes, the various processes must be protected from each other's
activities.
Protection refers to a mechanism or a way to control the access of programs,
processes, or users to the resources defined by a computer system. Following are
the major activities of an operating system with respect to protection −

• The OS ensures that all access to system resources is controlled.


• The OS ensures that external I/O devices are protected from invalid
access attempts.
• The OS provides authentication features for each user by means of
passwords.
System Call:-
In computing, a system call is a programmatic way in which a computer program
requests a service from the kernel of the operating system it is executed on. A
system call is a way for programs to interact with the operating system. A
computer program makes a system call when it makes a request to the operating
system’s kernel. System call provides the services of the operating system to the
user programs via Application Program Interface(API). It provides an interface
between a process and an operating system to allow user-level processes to
request services of the operating system. System calls are the only entry points
into the kernel system. All programs needing resources must use system calls.
Services Provided by System Calls :
1. Process creation and management
2. Main memory management
3. File Access, Directory, and File system management
4. Device handling(I/O)
5. Protection
6. Networking, etc.
1. Process control: end, abort, create, terminate, allocate, and
free memory.
2. File management: create, open, close, delete, read files,s, etc.
3. Device management
4. Information maintenance

20
5. Communication

Features of system calls:

1. Interface: System calls provide a well-defined interface between user


programs and the operating system. Programs make requests by calling
specific functions, and the operating system responds by executing the
requested service and returning a result.
2. Protection: System calls are used to access privileged operations that
are not available to normal user programs. The operating system uses
this privilege to protect the system from malicious or unauthorized
access.
3. Kernel Mode: When a system call is made, the program is temporarily
switched from user mode to kernel mode. In kernel mode, the program
has access to all system resources, including hardware, memory, and
other processes.
4. Context Switching: A system call requires a context switch, which
involves saving the state of the current process and switching to the
kernel mode to execute the requested service. This can introduce
overhead, which can impact system performance.
5. Error Handling: System calls can return error codes to indicate
problems with the requested service. Programs must check for these
errors and handle them appropriately.
6. Synchronization: System calls can be used to synchronize access to
shared resources, such as files or network connections. The operating
system provides synchronization mechanisms, such as locks or
semaphores, to ensure that multiple programs can access these
resources safely

System Programs:-
System Programming can be defined as the act of building Systems Software
using System Programming Languages. According to Computer Hierarchy, one
which comes at last is Hardware. Then it is Operating System, System Programs,
and finally Application Programs. Program Development and Execution can be
done conveniently in System Programs. Some of the System Programs are simply
user interfaces, others are complex. It traditionally lies between the user interface
and system calls.

21
So here, the user can only view up-to-the System Programs he can’t see System
Calls.
System Programs can be divided into these categories :

1. File Management – A file is a collection of specific information stored in


the memory of a computer system. File management is defined as the
process of manipulating files in the computer system, its management
includes the process of creating, modifying and deleting files.
• It helps to create new files in the computer system and placing them at
specific locations.
• It helps in easily and quickly locating these files in the computer
system.
• It makes the process of sharing files among different users very easy
and user-friendly.
• It helps to store files in separate folders known as directories.
• These directories help users to search files quickly or to manage files
according to their types of uses.
• It helps users to modify the data of files or to modify the name of files
in directories.
2. Status Information – Information like date, time amount of available
memory, or disk space is asked by some users. Others providing detailed
performance, logging, and debugging information which is more complex.
All this information is formatted and displayed on output devices or
printed. Terminal or other output devices or files or a window of GUI is used
for showing the output of programs.
2. File Modification – For modifying the contents of files we use this. For Files
stored on disks or other storage devices, we used different types of editors.
For searching contents of files or perform transformations of files we use
special commands.
3. Programming-Language support – For common programming languages,
we use Compilers, Assemblers, Debuggers, and interpreters which are
already provided to users. It provides all support to users. We can run any
programming language. All languages of importance are already provided.
4. Program Loading and Execution – When the program is ready after
Assembling and compilation, it must be loaded into memory for execution.
A loader is part of an operating system that is responsible for loading
programs and libraries. It is one of the essential stages for starting a
program. Loaders, relocatable loaders, linkage editors, and Overlay loaders
are provided by the system.
5. Communications – Virtual connections among processes, users, and
computer systems are provided by programs. Users can send messages to
another user on their screen, User can send e-mail, browsing on web pages,
remote login, the transformation of files from one user to another.
Some examples of system program in O.S. are –
• Windows 10
• Mac OS X
• Ubuntu
• Linux
• Unix
• Android
• Anti-virus
• Disk formatting
• Computer language translators

22
Process:-
A process is basically a program in execution. The execution of a process must
progress in a sequential fashion.
A process is defined as an entity which represents the basic unit of work to be
implemented in the system.
To put it in simple terms, we write our computer programs in a text file and when
we execute this program, it becomes a process which performs all the tasks
mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image shows
a simplified layout of a process inside main memory −
Process States:-

• New (Create) – In this step, the process is about to be created but not
yet created, it is the program which is present in secondary memory
that will be picked up by OS to create the process.
• Ready – New -> Ready to run. After the creation of a process, the process
enters the ready state i.e. the process is loaded into the main memory.
The process here is ready to run and is waiting to get the CPU time for
its execution. Processes that are ready for execution by the CPU are
maintained in a queue for ready processes.
• Run – The process is chosen by CPU for execution and the instructions
within the process are executed by any one of the available CPU cores.
• Blocked or wait – Whenever the process requests access to I/O or needs
input from the user or needs access to a critical region(the lock for
which is already acquired) it enters the blocked or wait state. The
process continues to wait in the main memory and does not require CPU.
Once the I/O operation is completed the process goes to the ready state.
• Terminated or completed – Process is killed as well as PCB is deleted.
• Suspend ready – Process that was initially in the ready state but was
swapped out of main memory(refer Virtual Memory topic) and placed
onto external storage by scheduler is said to be in suspend ready state.
The process will transition back to ready state whenever the process is
again brought onto the main memory.
• Suspend wait or suspend blocked – Similar to suspend ready but uses
the process which was performing I/O operation and lack of main
memory caused them to move to secondary memory. When work is
finished it may go to suspend ready.

23
Here are the different states of a process in an operating system:

• New: When a process is created, it enters a new state. In this state, the
operating system allocates the necessary resources to the process, such
as memory and CPU time.
• Ready: When the process has been allocated all the necessary resources
and is waiting for the CPU to execute it, it enters the ready state. In this
state, the process is waiting to be scheduled for execution.
• Running: When the process is selected by the operating system to
execute on the CPU, it enters the running state. In this state, the process
is executing its instructions on the CPU.
• Waiting: When a process is waiting for an event to occur, such as
input/output operations, it enters the waiting state. In this state, the
process is waiting for the event to occur before it can continue
executing.
• Terminated: When a process has finished executing, or if it is
terminated by the operating system, it enters the terminated state. In
this state, the operating system frees the resources that were allocated
to the process.

Additional features of the process state:

• A process can move from the running state to the waiting state if it
needs to wait for a resource to become available.
• A process can move from the waiting state to the ready state when the
resource it was waiting for becomes available.
• A process can move from the ready state to the running state when it is
selected by the operating system for execution.
• The scheduling algorithm used by the operating system determines
which process is selected to execute from the ready state.
• The operating system may also move a process from the running state
to the ready state to allow other processes to execute
Process Control :-
Process Control Block is a data structure that contains information of the
process related to it. The process control block is also known as a task control
block, entry of the process table, etc.
It is very important for process management as the data structuring for processes
is done in terms of the PCB. It also defines the current state of the operating
system.
Structure of the Process Control Block
The process control stores many data items that are needed for efficient process
management. Some of these data items are explained with the help of the given
diagram −

24
Operating System services for Process Management :-

There are different processes in operating system.So there is need to manage them
properly.This article will tell you how an operating system manages different
processes.

Process management is a critical component of any operating system. Process


management is the backbone of every operating system. Processes are essential
for managing system resources and ensuring that tasks are completed efficiently
and effectively.

This post will discuss the different process states, different process operations,
context switching, and Process management by modern operating systems. So in
this article we will learn what is process management in os.

A process is a program in the execution phase. A process is created, executed, and


terminated. A Process goes through these states. We will discuss these states with
the explanation. So in the short process keeps on changing the states. According
to the activity of a process, the state of the process is determined.

Processes usually fall into two categories-

25
1. System processes

System processes are started by the operating system, and they usually have
something to do with running the OS itself.

2. User processes

User processes, as you might guess, are those that the user starts. The operating
system though handles various system processes, but it mainly handles the user
code execution.
Each process can run independently of other processes and have its own memory
space. This makes process management tricky, as the OS needs to ensure that each
process gets its fair share of resources (memory, CPU time, etc.) while ensuring
that the system remains stable.

Process management

It is an important part of the operating system. It allows you to control the way
your computer runs by managing the currently active processes. This includes
ending processes that are no longer needed, setting process priorities, and more.
You can do it on your computer also.

There are a few ways to manage your processes. The first is through the use of
Task Manager. This allows you to see all of the processes currently running on
your computer and their current status and CPU/memory usage. You can end any
process that you no longer need, set a process priority, or start or stop a service.

Process Scheduling:-
The process manager’s activity is process scheduling, which involves removing
the running process from the CPU and selecting another process based on a
specific strategy.
Multiprogramming OS’s process scheduling is critical. Multiple processes could
be loaded into the executable memory at the same time in such operating systems,
and the loaded processes share the CPU utilising temporal multiplexing.
All PCBs (Process Scheduling Blocks) are kept in Process Scheduling Queues by the
OS. Each processing state has its own queue in the OS, and PCBs from all processes
in the same execution state are put in the very same queue. A process’s PCB is
unlinked from its present queue and then moved to its next state queue when its
status changes.
The following major process scheduling queues are maintained by the Operating
System:
Job queue – It contains all of the system’s processes.
Ready queue – This queue maintains a list of all processes in the main memory
that are ready to run. This queue is always filled with new processes.
Device queue – This queue is made up of processes that are stalled owing to the
lack of an I/O device.

26
Each queue can be managed by the OS using distinct policies (FIFO, Priority, Round
Robin, etc.). The OS scheduler governs how tasks are moved between the ready
and run queues, each of which can only have one item per processor core on a
system; it has been integrated with the CPU in the preceding figure.

• The Job Queue stores all processes that are entered into the system.
• The Ready Queue holds processes in the ready state.
• Device Queues hold processes that are waiting for any device to become
available. For each I/O device, there are separate device queues.
The ready queue is where a new process is initially placed. It sits in the ready
queue, waiting to be chosen for execution or dispatched. One of the following
occurrences can happen once the process has been assigned to the CPU and is
running:

• The process could send out an I/O request before being placed in the I/O
queue.
• The procedure could start a new one and then wait for it to finish.
• As a result of an interrupt, the process could be forcibly removed from the
CPU and returned to the ready queue.

process creation:-
A process can create several new processes through creating process system calls
during the process execution. Creating a process we call it the parent process and
the new process is a child process.
Every new process creates another process forming a tree-like structure. It can be
identified with a unique process identifier that usually represents it as pid which
is typically an integer number. Every process needs some resources like CPU time,
memory, file, I/O devices to accomplish.
Whenever a process creates a sub process, and may be each sub process is able
to obtain its resources directly from the operating system or from the resources
of the parent process. The parent process needs to partition its resources among
all its children or it may be able to share some resources to several children.
Restricting a child process to a subset of the parent’s resources prevents any
process from overloading the system by creating too many sub-processes. A
process is going to obtain its resources whenever it is created.
Let us consider a tree of process on a typical Solaris system as follows −

27
Whenever a process creates a new process, there are two possibilities in terms of
execution, which are as follows −
• The parent continues to execute concurrently with its children.
• The parent waits till some or all its children have terminated.
There are two more possibilities in terms of address space of the new process,
which are as follows −
• The child process is a duplicate of the parent process.
• The child process has a new program loaded into it.

process termination:-
Whenever the process finishes executing its final statement and asks the
operating system to delete it by using exit() system call.
At that point of time the process may return the status value to its parent process
with the help of wait() system call.
All the resources of the process including physical and virtual memory, open
files, I/O buffers are deallocated by the operating system.
Reasons for process termination
The reasons that the process may terminate the execution of one of its children
are as follows −
• The child exceeds its usage of resources that it has been allocated.
• The task that is assigned to the child is no longer required.
• The parent is exiting and the operating system does not allow a child
to continue if its parent terminates.

28
Some systems, including VMS, do not allow a child to exist if its parent has
terminated. In such systems, if a process terminates either normally or
abnormally, then all its children have to be terminated. This concept is referred
to as cascading termination.
Causes for termination
The other causes for termination are as follows −
• Time slot expired − When the process execution does not complete
within the time quantum, then the process gets terminated from the
running state. The CPU picks the next job in the ready queue to
execute.
• Memory bound violation − If a process needs more memory than the
available memory.
• I/O failure − When the operating system does not provide an I/O
device, the process enters a waiting state.
• Process request − If the parent process requests the child process
about termination.
• Invalid instruction

A process is a program in execution and it is more than a program code called as


text section and this concept works under all the operating system because all
the task perform by the operating system needs a process to perform the task
The process executes when it changes state. The state of a process is defined by
the current activity of the process.
Each process may be any one of the following states −
• New − The process is being created.
• Running − In this state the instructions are being executed.
• Waiting − The process is in waiting state until an event is occur like
I/O operation completion or receiving a signal.
• Ready − The process is waiting to be assigned to a processor.
• Terminated − the process has finished execution.
It is important to know that only one process can be running on any processor at
any instant. Many processes may be ready and waiting.

Thread:-
Within a program, a thread is a separate execution path. It is a lightweight
process that the operating system can schedule and run concurrently with
other threads. The operating system creates and manages threads, and they
share the same memory and resources as the program that created them.
This enables multiple threads to collaborate and work efficiently within a
single program.

Advantages of Thread over Process :-


1. Responsiveness: If the process is divided into multiple threads, if one
thread completes its execution, then its output can be immediately
returned.
2. Faster context switch: Context switch time between threads is lower
compared to process context switch. Process context switching requires
more overhead from the CPU.

29
3. Effective utilization of multiprocessor system: If we have multiple
threads in a single process, then we can schedule multiple threads on
multiple processor. This will make process execution faster.
4. Resource sharing: Resources like code, data, and files can be shared
among all threads within a process. Note: stack and registers can’t be
shared among the threads. Each thread has its own stack and registers.
5. Communication: Communication between multiple threads is easier, as
the threads shares common address space. while in process we have to
follow some specific communication technique for communication
between two process.
6. Enhanced throughput of the system: If a process is divided into
multiple threads, and each thread function is considered as one job,
then the number of jobs completed per unit of time is increased, thus
increasing the throughput of the system.
Types of Threads
There are two types of threads:
• User Level Thread
• Kernel Level Thread

Multithreading Model:-

Multithreading allows the application to divide its task into individual threads. In
multi-threads, the same process or task can be done by the number of threads, or
we can say that there is more than one thread to perform the task in
multithreading. With the use of multithreading, multitasking can be achieved.

The main drawback of single threading systems is that only one task can be
performed at a time, so to overcome the drawback of this single threading, there
is multithreading that allows multiple tasks to be performed.

For example:

55M
995
Prime Ministers of India | List of Prime Minister of India (1947-2020)

30
In the above example, client1, client2, and client3 are accessing the web server
without any waiting. In multithreading, several tasks can run at the same time.

In an operating system, threads are divided into the user-level thread and the
Kernel-level thread. User-level threads handled independent form above the
kernel and thereby managed without any kernel support. On the opposite hand,
the operating system directly manages the kernel-level threads. Nevertheless,
there must be a form of relationship between user-level and kernel-level threads.

There exists three established multithreading models classifying these


relationships are:

o Many to one multithreading model


o One to one multithreading model
o Many to Many multithreading models

Many to one multithreading model:

The many to one model maps many user levels threads to one kernel thread. This
type of relationship facilitates an effective context-switching environment, easily
implemented even on the simple kernel with no thread support.

The disadvantage of this model is that since there is only one kernel-level thread
schedule at any given time, this model cannot take advantage of the hardware
acceleration offered by multithreaded processes or multi-processor systems. In
this, all the thread management is done in the userspace. If blocking comes, this
model blocks the whole system.

31
In the above figure, the many to one model associates all user-level threads to
single kernel-level threads.

One to one multithreading model

The one-to-one model maps a single user-level thread to a single kernel-level


thread. This type of relationship facilitates the running of multiple threads in
parallel. However, this benefit comes with its drawback. The generation of every
new user thread must include creating a corresponding kernel thread causing an
overhead, which can hinder the performance of the parent process. Windows
series and Linux operating systems try to tackle this problem by limiting the
growth of the thread count.

32
In the above figure, one model associates that one user-level thread to a single
kernel-level thread.

Many to Many Model multithreading model

In this type of model, there are several user-level threads and several kernel-level
threads. The number of kernel threads created depends upon a particular
application. The developer can create as many threads at both levels but may not
be the same. The many to many model is a compromise between the other two
models. In this model, if any thread makes a blocking system call, the kernel can
schedule another thread for execution. Also, with the introduction of multiple
threads, complexity is not present as in the previous models. Though this model
allows the creation of multiple kernel threads, true concurrency cannot be
achieved by this model. This is because the kernel can schedule only one process
at a time.

Many to many versions of the multithreading model associate several user-level


threads to the same or much less variety of kernel-level threads in the above
figure.

Threads and its types:-


thread is a single sequence stream within a process. Threads have same
properties as of the process so they are called as light weight processes. Threads
are executed one after another but gives the illusion as if they are executing in
parallel. Each thread has different states. Each thread has
1. A program counter
2. A register set
3. A stack space

Types of Threads:
1. User Level thread (ULT) – Is implemented in the user level library, they
are not created using the system calls. Thread switching does not need
to call OS and to cause interrupt to Kernel. Kernel doesn’t know about
the user level thread and manages them as if they were single-threaded
processes.
• Advantages of ULT –

33
• Can be implemented on an OS that doesn’t support
multithreading.
• Simple representation since thread has only program
counter, register set, stack space.
• Simple to create since no intervention of kernel.
• Thread switching is fast since no OS calls need to be
made.
• Limitations of ULT –
• No or less co-ordination among the threads and
Kernel.
• If one thread causes a page fault, the entire process
blocks.
2. Kernel Level Thread (KLT) – Kernel knows and manages the threads.
Instead of thread table in each process, the kernel itself has thread table
(a master one) that keeps track of all the threads in the system. In
addition kernel also maintains the traditional process table to keep track
of the processes. OS kernel provides system call to create and manage
threads.
• Advantages of KLT –
• Since kernel has full knowledge about the threads in
the system, scheduler may decide to give more time to
processes having large number of threads.
• Good for applications that frequently block.
• Limitations of KLT –
• Slow and inefficient.
• It requires thread control block so it is an overhead.

Process Scheduler:-
The process manager’s activity is process scheduling, which involves removing
the running process from the CPU and selecting another process based on a
specific strategy. The scheduler’s purpose is to implement the virtual machine so
that each process appears to be running on its own computer to the user.

34
Multiprogramming OS’s process scheduling is critical. Multiple processes could
be loaded into executable memory at the same time in such an OS, and the loaded
processes share the CPU utilising temporal multiplexing.

Types of Process Schedulers


Process schedulers are divided into three categories.

1. Long-Term Scheduler or Job Scheduler


The job scheduler is another name for Long-Term scheduler. It selects processes
from the pool (or the secondary memory) and then maintains them in the primary
memory’s ready queue.
The Multiprogramming degree is mostly controlled by the Long-Term Scheduler.
The goal of the Long-Term scheduler is to select the best mix of IO and CPU bound
processes from the pool of jobs.
If the job scheduler selects more IO bound processes, all of the jobs may become
stuck, the CPU will be idle for the majority of the time, and multiprogramming will
be reduced as a result. Hence, the Long-Term scheduler’s job is crucial and could
have a Long-Term impact on the system.

2. Short-Term Scheduler or CPU Scheduler

CPU scheduler is another name for Short-Term scheduler. It chooses one job from
the ready queue and then sends it to the CPU for processing.
To determine which work will be dispatched for execution, a scheduling method
is utilised. The Short-Term scheduler’s task can be essential in the sense that if it
chooses a job with a long CPU burst time, all subsequent jobs will have to wait in
a ready queue for a long period. This is known as hunger, and it can occur if the
Short-Term scheduler makes a mistake when selecting the work.

35
3. Medium-Term Scheduler

The switched-out processes are handled by the Medium-Term scheduler. If the


running state processes require some IO time to complete, the state must be
changed from running to waiting.
This is accomplished using a Medium-Term scheduler. It stops the process from
executing in order to make space for other processes. Swapped out processes are
examples of this, and the operation is known as swapping. The Medium-Term
scheduler here is in charge of stopping and starting processes.
The degree of multiprogramming is reduced. To have a great blend of operations
in the ready queue, swapping is required.
Scheduling algorithms:-
A Process Scheduler schedules different processes to be assigned to the CPU based
on particular scheduling algorithms. There are six popular process scheduling
algorithms which we are going to discuss in this chapter −

• First-Come, First-Served (FCFS) Scheduling


• Shortest-Job-Next (SJN) Scheduling
• Priority Scheduling
• Shortest Remaining Time
• Round Robin(RR) Scheduling
• Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive
algorithms are designed so that once a process enters the running state, it cannot
be preempted until it completes its allotted time, whereas the preemptive
scheduling is based on priority where a scheduler may preempt a low priority
running process anytime when a high priority process enters into a ready state.
First Come First Serve (FCFS)
• Jobs are executed on first come, first serve basis.
• It is a non-preemptive, pre-emptive scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.

36
Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time

P0 0-0=0

P1 5-1=4

P2 8-2=6

P3 16 - 3 = 13

Average Wait Time: (0+4+6+13) / 4 = 5.75


Shortest Job Next (SJN)
• This is also known as shortest job first, or SJF
• This is a non-preemptive, pre-emptive scheduling algorithm.
• Best approach to minimize waiting time.
• Easy to implement in Batch systems where required CPU time is known
in advance.
• Impossible to implement in interactive systems where required CPU
time is not known.
• The processer should know in advance how much time process will
take.
Given: Table of processes, and their Arrival time, Execution time

Process Arrival Time Execution Time Service Time

P0 0 5 0

P1 1 3 5

P2 2 8 14

37
P3 3 6 8

Waiting time of each process is as follows −

Process Waiting Time

P0 0-0=0

P1 5-1=4

P2 14 - 2 = 12

P3 8-3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25


Priority Based Scheduling
• Priority scheduling is a non-preemptive algorithm and one of the most
common scheduling algorithms in batch systems.
• Each process is assigned a priority. Process with highest priority is to
be executed first and so on.
• Processes with same priority are executed on first come first served
basis.
• Priority can be decided based on memory requirements, time
requirements or any other resource requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority.
Here we are considering 1 is the lowest priority.

Process Arrival Time Execution Time Priority Service Time

P0 0 5 1 0

P1 1 3 2 11

38
P2 2 8 1 14

P3 3 6 3 5

Waiting time of each process is as follows −

Process Waiting Time

P0 0-0=0

P1 11 - 1 = 10

P2 14 - 2 = 12

P3 5-3=2

Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6


Shortest Remaining Time
• Shortest remaining time (SRT) is the preemptive version of the SJN
algorithm.
• The processor is allocated to the job closest to completion but it can
be preempted by a newer ready job with shorter time to completion.
• Impossible to implement in interactive systems where required CPU
time is not known.
• It is often used in batch environments where short jobs need to give
preference.
Round Robin Scheduling
• Round Robin is the preemptive process scheduling algorithm.
• Each process is provided a fix time to execute, it is called a quantum.
• Once a process is executed for a given time period, it is preempted and
other process executes for a given time period.
• Context switching is used to save states of preempted processes.

39
Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time

P0 (0 - 0) + (12 - 3) = 9

P1 (3 - 1) = 2

P2 (6 - 2) + (14 - 9) + (20 - 17) = 12

P3 (9 - 3) + (17 - 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5


Multiple-Level Queues Scheduling
Multiple-level queues are not an independent scheduling algorithm. They make
use of other existing algorithms to group and schedule jobs with common
characteristics.

• Multiple queues are maintained for processes with common


characteristics.
• Each queue can have its own scheduling algorithms.
• Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound
jobs in another queue. The Process Scheduler then alternately selects jobs from
each queue and assigns them to the CPU based on the algorithm assigned to the
queue

Types of Scheduling Algorithms:-

The Purpose of a Scheduling algorithm


1. Maximum CPU utilization
2. Fare allocation of CPU
3. Maximum throughput
4. Minimum turnaround time
5. Minimum waiting time

40
6. Minimum response time

There are the following algorithms which can be used to schedule the jobs.

1. First Come First Serve

It is the simplest algorithm to implement. The process with the minimal arrival
time will get the CPU first. The lesser the arrival time, the sooner will the process
gets the CPU. It is the non-preemptive type of scheduling.

2. Round Robin

In the Round Robin scheduling algorithm, the OS defines a time quantum (slice).
All the processes will get executed in the cyclic way. Each of the process will get
the CPU for a small amount of time (called time quantum) and then get back to the
ready queue to wait for its next turn. It is a preemptive type of scheduling.

PlayNext
Unmute

Current Time 0:00

Duration 18:10
Loaded: 0.37%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
3. Shortest Job First

The job with the shortest burst time will get the CPU first. The lesser the burst
time, the sooner will the process get the CPU. It is the non-preemptive type of
scheduling.

4. Shortest remaining time first

It is the preemptive form of SJF. In this algorithm, the OS schedules the Job
according to the remaining time of the execution.

5. Priority based scheduling

In this algorithm, the priority will be assigned to each of the processes. The higher
the priority, the sooner will the process get the CPU. If the priority of the two
processes is same then they will be scheduled according to their arrival time.

6. Highest Response Ratio Next

In this scheduling Algorithm, the process with highest response ratio will be
scheduled next. This reduces the starvation in the system.

Process Synchronization:-
Process Synchronization is the coordination of execution of multiple processes
in a multi-process system to ensure that they access shared resources in a

41
controlled and predictable manner. It aims to resolve the problem of race
conditions and other synchronization issues in a concurrent system.
The main objective of process synchronization is to ensure that multiple
processes access shared resources without interfering with each other, and to
prevent the possibility of inconsistent data due to concurrent access. To achieve
this, various synchronization techniques such as semaphores, monitors, and
critical sections are used.
In a multi-process system, synchronization is necessary to ensure data
consistency and integrity, and to avoid the risk of deadlocks and other
synchronization problems. Process synchronization is an important aspect of
modern operating systems, and it plays a crucial role in ensuring the correct and
efficient functioning of multi-process systems.
On the basis of synchronization, processes are categorized as one of the
following two types:
• Independent Process: The execution of one process does not affect the
execution of other processes.
• Cooperative Process: A process that can affect or be affected by other
processes executing in the system.
Process synchronization problem arises in the case of Cooperative process also
because resources are shared in Cooperative processes.

Race Condition:

When more than one process is executing the same code or accessing the same
memory or any shared variable in that condition there is a possibility that the
output or the value of the shared variable is wrong so for that all the processes
doing the race to say that my output is correct this condition known as a race
condition. Several processes access and process the manipulations over the same
data concurrently, then the outcome depends on the particular order in which
the access takes place. A race condition is a situation that may occur inside a
critical section. This happens when the result of multiple thread execution in the
critical section differs according to the order in which the threads execute. Race
conditions in critical sections can be avoided if the critical section is treated as
an atomic instruction. Also, proper thread synchronization using locks or atomic
variables can prevent race conditions.

Critical Section Problem:

A critical section is a code segment that can be accessed by only one process at
a time. The critical section contains shared variables that need to be
synchronized to maintain the consistency of data variables. So the critical
section problem means designing a way for cooperative processes to access
shared resources without creating data inconsistencies.

42
In the entry section, the process requests for entry in the Critical Section.
Any solution to the critical section problem must satisfy three requirements:
• Mutual Exclusion: If a process is executing in its critical section, then
no other process is allowed to execute in the critical section.
• Progress: If no process is executing in the critical section and other
processes are waiting outside the critical section, then only those
processes that are not executing in their remainder section can
participate in deciding which will enter in the critical section next, and
the selection can not be postponed indefinitely.
• Bounded Waiting: A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is
granted.

Peterson’s Solution:

Peterson’s Solution is a classical software-based solution to the critical section


problem. In Peterson’s solution, we have two shared variables:
• boolean flag[i]: Initialized to FALSE, initially no one is interested in
entering the critical section
• int turn: The process whose turn is to enter the critical section.

43
Peterson’s Solution preserves all three conditions:
• Mutual Exclusion is assured as only one process can access the critical
section at any time.
• Progress is also assured, as a process outside the critical section does
not block other processes from entering the critical section.
• Bounded Waiting is preserved as every process gets a fair chance.
Disadvantages of Peterson’s solution:
• It involves busy waiting.(In the Peterson’s solution, the code statement-
“while(flag[j] && turn == j);” is responsible for this. Busy waiting is not
favored because it wastes CPU cycles that could be used to perform
other tasks.)
• It is limited to 2 processes.
• Peterson’s solution cannot be used in modern CPU architectures.

Semaphores:

A semaphore is a signaling mechanism and a thread that is waiting on a


semaphore can be signaled by another thread. This is different than a mutex as
the mutex can be signaled only by the thread that is called the wait function.
A semaphore uses two atomic operations, wait and signal for process
synchronization.
A Semaphore is an integer variable, which can be accessed only through two
operations wait() and signal().
There are two types of semaphores: Binary Semaphores and Counting
Semaphores.
• Binary Semaphores: They can only be either 0 or 1. They are also known
as mutex locks, as the locks can provide mutual exclusion. All the

44
processes can share the same mutex semaphore that is initialized to 1.
Then, a process has to wait until the lock becomes 0. Then, the process
can make the mutex semaphore 1 and start its critical section. When it
completes its critical section, it can reset the value of the mutex
semaphore to 0 and some other process can enter its critical section.
• Counting Semaphores: They can have any value and are not restricted
over a certain domain. They can be used to control access to a resource
that has a limitation on the number of simultaneous accesses. The
semaphore can be initialized to the number of instances of the resource.
Whenever a process wants to use that resource, it checks if the number
of remaining instances is more than zero, i.e., the process has an
instance available. Then, the process can enter its critical section
thereby decreasing the value of the counting semaphore by 1. After the
process is over with the use of the instance of the resource, it can leave
the critical section thereby adding 1 to the number of available
instances of the resource.

Advantages and Disadvantages:-

Advantages of Process Synchronization:

• Ensures data consistency and integrity


• Avoids race conditions
• Prevents inconsistent data due to concurrent access
• Supports efficient and effective use of shared resources

Disadvantages of Process Synchronization:

• Adds overhead to the system


• Can lead to performance degradation
• Increases the complexity of the system
• Can cause deadlocks if not implemented properly.

Inter Process Communication (IPC)


Interprocess communication is the mechanism provided by the operating system
that allows processes to communicate with each other. This communication could
involve a process letting another process know that some event has occurred or
the transferring of data from one process to another.
A diagram that illustrates interprocess communication is as follows −

A process can be of two types:


• Independent process.
• Co-operating process.

45
An independent process is not affected by the execution of other processes while
a co-operating process can be affected by other executing processes. Though one
can think that those processes, which are running independently, will execute
very efficiently, in reality, there are many situations when co-operative nature
can be utilized for increasing computational speed, convenience, and
modularity. Inter-process communication (IPC) is a mechanism that allows
processes to communicate with each other and synchronize their actions. The
communication between these processes can be seen as a method of co-operation
between them. Processes can communicate with each other through both:

1. Shared Memory
2. Message passing
Figure 1 below shows a basic structure of communication between processes via
the shared memory method and via the message passing method.

An operating system can implement both methods of communication. First, we


will discuss the shared memory methods of communication and then message
passing. Communication between processes using shared memory requires
processes to share some variable, and it completely depends on how the
programmer will implement it. One way of communication using shared memory
can be imagined like this: Suppose process1 and process2 are executing
simultaneously, and they share some resources or use some information from
another process. Process1 generates information about certain computations or
resources being used and keeps it as a record in shared memory. When process2
needs to use the shared information, it will check in the record stored in shared
memory and take note of the information generated by process1 and act
accordingly. Processes can use shared memory for extracting information as a
record from another process as well as for delivering any specific information to
other processes.
Let’s discuss an example of communication between processes using the shared
memory method.

i) Shared Memory Method


Ex: Producer-Consumer problem
There are two processes: Producer and Consumer. The producer produces some

46
items and the Consumer consumes that item. The two processes share a common
space or memory location known as a buffer where the item produced by the
Producer is stored and from which the Consumer consumes the item if needed.
There are two versions of this problem: the first one is known as the unbounded
buffer problem in which the Producer can keep on producing items and there is
no limit on the size of the buffer, the second one is known as the bounded buffer
problem in which the Producer can produce up to a certain number of items
before it starts waiting for Consumer to consume it. We will discuss the bounded
buffer problem. First, the Producer and the Consumer will share some common
memory, then the producer will start producing items. If the total produced item
is equal to the size of the buffer, the producer will wait to get it consumed by
the Consumer. Similarly, the consumer will first check for the availability of the
item. If no item is available, the Consumer will wait for the Producer to produce
it. If there are items available, Consumer will consume them. The pseudo-code
to demonstrate is provided below:
Shared Data between the two Processes

#define buff_max 25
#define mod %
struct item{

// different member of the produced data


// or consumed data
---------
}
// An array is needed for holding the items.
// This is the shared place which will be
// access by both process
// item shared_buff [ buff_max ];
// Two variables which will keep track of
// the indexes of the items produced by producer
// and consumer The free index points to
// the next free index. The full index points to
// the first full index.
int free_index = 0;
int full_index = 0;

Producer Process Code

item nextProduced;
while(1){
// check if there is no space
// for production.
// if so keep waiting.
while((free_index+1) mod buff_max == full_index)
shared_buff[free_index] = nextProduced;
free_index = (free_index + 1) mod buff_max;
}

Consumer Process Code

item nextConsumed;

47
while(1){

// check if there is an available


// item for consumption.
// if not keep on waiting for
// get them produced.
while((free_index == full_index);

nextConsumed = shared_buff[full_index];
full_index = (full_index + 1) mod buff_max;
}

In the above code, the Producer will start producing again when the
(free_index+1) mod buff max will be free because if it is not free, this implies
that there are still items that can be consumed by the Consumer so there is no
need to produce more. Similarly, if free index and full index point to the same
index, this implies that there are no items to consume.
ii) Messaging Passing Method
Now, We will start our discussion of the communication between processes via
message passing. In this method, processes communicate with each other
without using any kind of shared memory. If two processes p1 and p2 want to
communicate with each other, they proceed as follows:
• Establish a communication link (if a link already exists, no need to
establish it again.)
• Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)

The message size can be of fixed size or of variable size. If it is of fixed size, it
is easy for an OS designer but complicated for a programmer and if it is of
variable size then it is easy for a programmer but complicated for the OS
designer. A standard message can have two parts: header and body.
The header part is used for storing message type, destination id, source id,
message length, and control information. The control information contains

48
information like what to do if runs out of buffer space, sequence number,
priority. Generally, message is sent using FIFO style

Advantages of IPC:

1. Enables processes to communicate with each other and share resources,


leading to increased efficiency and flexibility.
2. Facilitates coordination between multiple processes, leading to better
overall system performance.
3. Allows for the creation of distributed systems that can span multiple
computers or networks.
4. Can be used to implement various synchronization and communication
protocols, such as semaphores, pipes, and sockets.

Disadvantages of IPC:

1. Increases system complexity, making it harder to design, implement,


and debug.
2. Can introduce security vulnerabilities, as processes may be able to
access or modify data belonging to other processes.
3. Requires careful management of system resources, such as memory and
CPU time, to ensure that IPC operations do not degrade overall system
performance.
Can lead to data inconsistencies if multiple processes try to access or
modify the same data at the same time.
4. Overall, the advantages of IPC outweigh the disadvantages, as it is a
necessary mechanism for modern operating systems and enables
processes to work together and share resources in a flexible and
efficient manner. However, care must be taken to design and implement
IPC systems carefully, in order to avoid potential security vulnerabilities
and performance issues.

Deadlock in Operating System:-


A process in operating system uses resources in the following way.
1. Requests a resource
2. Use the resource
3. Releases the resource
A deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired by some
other process.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in
front of each other. A similar situation occurs in operating systems when there
are two or more processes that hold some resources and wait for resources held
by other(s). For example, in the below diagram, Process 1 is holding Resource 1
and waiting for resource 2 which is acquired by process 2, and process 2 is
waiting for resource 1.

49
Examples Of Deadlock
1. The system has 2 tape drives. P1 and P2 each hold one tape drive and
each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as
follows:
• P0 executes wait(A) and preempts.
• P1 executes wait(B).
• Now P0 and P1 enter in deadlock.
P0 P1
wait(A); wait(B)
wait(B); wait(A)
3. Assume the space is available for allocation of 200K bytes, and the
following sequence of events occurs.

P0 P1
Request Request
80KB; 70KB;
Request 60KB; Request 80KB;
Deadlock occurs if both processes progress to their second request.
Deadlock can arise if the following four conditions hold simultaneously
(Necessary Conditions)
Mutual Exclusion: Two or more resources are non-shareable (Only one process
can use at a time)
Hold and Wait: A process is holding at least one resource and waiting for
resources.
No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
Circular Wait: A set of processes waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance:
Prevention:
The idea is to not let the system into a deadlock state. This system will make
sure that above mentioned four conditions will not arise. These techniques are
very costly so we use this in cases where our priority is making a system
deadlock-free.
One can zoom into each category individually, Prevention is done by negating
one of the above-mentioned necessary conditions for deadlock. Prevention can
be done in four different ways:

50
1. Eliminate mutual exclusion 3. Allow preemption
2. Solve hold and Wait 4. Circular wait
Solution
Avoidance:
Avoidance is kind of futuristic. By using the strategy of “Avoidance”, we have to
make an assumption. We need to ensure that all information about resources that
the process will need is known to us before the execution of the process. We use
Banker’s algorithm (Which is in turn a gift from Dijkstra) to avoid deadlock.
In prevention and avoidance, we get the correctness of data but performance
decreases.
2) Deadlock detection and recovery: If Deadlock prevention or avoidance is not
applied to the software then we can handle this by deadlock detection and
recovery. which consist of two phases:
1. In the first phase, we examine the state of the process and check
whether there is a deadlock or not in the system.
2. If found deadlock in the first phase then we apply the algorithm for
recovery of the deadlock.
In Deadlock detection and recovery, we get the correctness of data but
performance decreases.
3) Deadlock ignorance: If a deadlock is very rare, then let it happen and reboot
the system. This is the approach that both Windows and UNIX take. we use the
ostrich algorithm for deadlock ignorance.
In Deadlock, ignorance performance is better than the above two methods but
the correctness of data
data Access:-
When a file is used, information is read and accessed into computer memory and
there are several ways to access this information of the file. Some systems
provide only one access method for files. Other systems, such as those of IBM,
support many access methods, and choosing the right one for a particular
application is a major design problem.
There are three ways to access a file into a computer system: Sequential-Access,
Direct Access, Index sequential Method.
1. Sequential Access –
It is the simplest access method. Information in the file is processed in
order, one record after the other. This mode of access is by far the most
common; for example, editor and compiler usually access the file in this
fashion.
Read and write make up the bulk of the operation on a file. A read
operation -read next- read the next position of the file and automatically
advance a file pointer, which keeps track I/O location. Similarly, for the
-write next- append to the end of the file and advance to the newly
written material.
Key points:
• Data is accessed one record right after another record in an
order.
• When we use read command, it move ahead pointer by one
• When we use write command, it will allocate memory and move
the pointer to the end of the file
• Such a method is reasonable for tape.

Advantages of Sequential Access Method :

51
• It is simple to implement this file access mechanism.
• It uses lexicographic order to quickly access the next entry.
Disadvantages of Sequential Access Method :
• If the file record that needs to be accessed next is not present next to
the current record, this type of file access method is slow.
• Moving a sizable chunk of the file may be necessary to insert a new
record.
2.Direct Access –
Another method is direct access method also known as relative access method. A
fixed-length logical record that allows the program to read and write record
rapidly. in no particular order. The direct access is based on the disk model of a
file since disk allows random access to any file block. For direct access, the file
is viewed as a numbered sequence of block or record. Thus, we may read block
14 then block 59, and then we can write block 17. There is no restriction on the
order of reading and writing for a direct access file.
A block number provided by the user to the operating system is normally
a relative block number, the first relative block of the file is 0 and then 1 and so
on.

Advantages of Direct Access Method :


• The files can be immediately accessed decreasing the average access
time.
• In the direct access method, in order to access a block, there is no need
of traversing all the blocks present before it.
3.Index sequential method –
It is the other method of accessing a file that is built on the top of the sequential
access method. These methods construct an index for the file. The index, like an
index in the back of a book, contains the pointer to the various blocks. To find a
record in the file, we first search the index, and then by the help of pointer we
access the file directly.
Key points:
• It is built on top of Sequential access.
• It control the pointer by using index.
Control synchronization:-
When another process accesses the same shared data, changes made in one
process are not replicated. Process synchronisation is required in order to
prevent shared data from becoming inconsistent. There are various processes
in different states throughout the Operating System. We must divide our
available resources among many processes because we only have a finite
number of them. However, you should take care to prevent more than one
process from using the same resource at once as this could result in inconsistent
data.

In order to ensure process synchronization, the operating system needs to


support it. Cooperative processes are those that share resources among
themselves, and independent processes are those whose execution has no
bearing on other processes' operations.

52
We will study what is process synchronization in OS, what is synchronization in
OS, and what is the need for synchronization and we will explain in detail
process synchronization in this blog. And if you want in-depth knowledge about
operating systems to pursue a career, you can go for the Web Development
Course to understand the basics of operating systems and learn how to
use advanced tools to design and create dynamic websites.

What is control Synchronization in OS?

An operating system is a piece of software that controls all of the installed


programmes on a computer or other device, essentially making it run more
efficiently. Because of this, the operating system frequently has to handle
multiple tasks at once. Unless these processes running simultaneously share a
resource, this usually doesn't pose a synchronization problem in OS.

Take a bank as an example, which maintains a single database for each


customer's account balance. Let's say you had x rupees in your account at the
beginning. Now you withdraw some money from your bank account, and
someone tries to look at the balance of your account at the same time.

The total balance remaining in your account after the transaction will be less
than x because you are taking money out of it. However, since the transaction
takes some time, the person interprets x as your account balance, resulting in
inconsistent data. We could guarantee consistent data if we could somehow
make sure that only one process was running at once.

Source – Scalar.com

In the above image, if Process1 and Process2 occur simultaneously, User 2 will
receive the incorrect account balance as Y as a result of Process1 being executed
when the balance is X. When multiple processes share a single resource in a
53
system, inconsistent data can result, which is why process synchronization is
required in the operating system.

Critical Section:-
In computer science, a critical section refers to a segment of code that is
executed by multiple concurrent threads or processes, and which accesses
shared resources. These resources may include shared memory, files, or other
system resources that can only be accessed by one thread or process at a time
to avoid data inconsistency or race conditions.
1. The critical section must be executed as an atomic operation, which
means that once one thread or process has entered the critical section,
all other threads or processes must wait until the executing thread or
process exits the critical section. The purpose of synchronization
mechanisms is to ensure that only one thread or process can execute
the critical section at a time.
2. The concept of a critical section is central to synchronization in
computer systems, as it is necessary to ensure that multiple threads or
processes can execute concurrently without interfering with each other.
Various synchronization mechanisms such as semaphores, mutexes,
monitors, and condition variables are used to implement critical
sections and ensure that shared resources are accessed in a mutually
exclusive manner.
The use of critical sections in synchronization can be advantageous in improving
the performance of concurrent systems, as it allows multiple threads or
processes to work together without interfering with each other. However, care
must be taken in designing and implementing critical sections, as incorrect
synchronization can lead to race conditions, deadlocks.
Critical Section:_
When more than one processes try to access the same code segment that
segment is known as the critical section. The critical section contains shared
variables or resources which are needed to be synchronized to maintain the
consistency of data variables.
In simple terms, a critical section is a group of instructions/statements or
regions of code that need to be executed atomically (read this post for atomicity),
such as accessing a resource (file, input or output port, global data, etc.)
In concurrent programming, if one thread tries to change the value of shared
data at the same time as another thread tries to read the value (i.e, data race
across threads), the result is unpredictable. The access to such shared variables
(shared memory, shared files, shared port, etc.) is to be synchronized.
Few programming languages have built-in support for synchronization. It is
critical to understand the importance of race conditions while writing kernel-
mode programming (a device driver, kernel thread, etc.) since the programmer
can directly access and modify kernel data structures
race condition:-

A race condition is an undesirable situation that occurs when a device or system


attempts to perform two or more operations at the same time, but because of the
nature of the device or system, the operations must be done in the proper
sequence to be done correctly.

54
Race conditions are most commonly associated with computer science and
programming. They occur when two computer program processes, or threads,
attempt to access the same resource at the same time and cause problems in the
system.

What are the types of race conditions?

There are a few types of race conditions. Two categories that define the impact of
the race condition on a system are referred to as critical and noncritical:

• A critical race condition will cause the end state of the device, system or
program to change. For example, if flipping two light switches connected
to a common light at the same time blows the circuit, it is considered a
critical race condition. In software, a critical race condition is when a
situation results in a bug with unpredictable or undefined behavior.

• A noncritical race condition does not directly affect the end state of the
system, device or program. In the light example, if the light is off and
flipping both switches simultaneously turns the light on and has the
same effect as flipping one switch, then it is a noncritical race condition.
In software, a noncritical race condition does not result in a bug.

Critical and noncritical race conditions aren't limited to electronics or


programming. They can occur in many types of systems where race conditions
happen.

In programming, two main types of race conditions occur in a critical section of


code, which is a section of code executed by multiple threads. When multiple
threads try to read a variable and then each acts on it, one of the following
situations can occur:

• Read-modify-write. This kind of race condition happens when two


processes read a value in a program and write back a new value. It often
causes a software bug. Like the example above, the expectation is that
the two processes will happen sequentially -- the first process produces
its value and then the second process reads that value and returns a
different one.

For example, if checks against a checking account are processed


sequentially, the system will make sure there are enough funds in the

55
account to process check A first and then look again to see if there are
enough funds to process check B after processing check A. However, if
the two checks are processed at the same time, the system may read the
same account balance value for both processes and give an incorrect
account balance value, causing the account to be overdrawn.

Check-then-act. This race condition happens when two processes check


a value on which they will take each take an external action. The
processes both check the value, but only one process can take the value
with it. The later-occurring process will read the value as null. This
results in a potentially out-of-date or unavailable observation being used
to determine what the program will do next. For example, if a map
application runs two processes simultaneously that require the same
location data, one will take the value first so the other can't use it. The
later process reads the data as null.

Classical problems:-
in this article, we will see number of classical problems of synchronization as
examples of a large class of concurrency-control problems. In our solutions to
the problems, we use semaphores for synchronization, since that is the
traditional way to present such solutions. However, actual implementations of
these solutions could use mutex locks in place of binary semaphores.
These problems are used for testing nearly every newly proposed
synchronization scheme. The following problems of synchronization are
considered as classical problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
4. Sleeping Barber Problem
These are summarized, for detailed explanation, you can view the linked articles
for each.
• Bounded-buffer (or Producer-Consumer) Problem:
Bounded Buffer problem is also called producer consumer problem.
This problem is generalized in terms of the Producer-Consumer
problem. Solution to this problem is, creating two counting semaphores
“full” and “empty” to keep track of the current number of full and empty
buffers respectively. Producers produce a product and consumers
consume the product, but both use of one of the containers each time.

• Dining-Philosophers Problem:
The Dining Philosopher Problem states that K philosophers seated
around a circular table with one chopstick between each pair of
philosophers. There is one chopstick between each philosopher. A
philosopher may eat if he can pickup the two chopsticks adjacent to
him. One chopstick may be picked up by any one of its adjacent

56
followers but not both. This problem involves the allocation of limited
resources to a group of processes in a deadlock-free and starvation-free
manner.

• Readers and Writers Problem:


Suppose that a database is to be shared among several concurrent
processes. Some of these processes may want only to read the database,
whereas others may want to update (that is, to read and write) the
database. We distinguish between these two types of processes by
referring to the former as readers and to the latter as writers. Precisely
in OS we call this situation as the readers-writers problem. Problem
parameters:
• One set of data is shared among a number of processes.

• Once a writer is ready, it performs its write. Only one writer


may write at a time.

• If a process is writing, no other process can read it.

• If at least one reader is reading, no other process can write.

• Readers may not write and only read.

• Sleeping Barber Problem:


Barber shop with one barber, one barber chair and N chairs to wait in.
When no customers the barber goes to sleep in barber chair and must
be woken when a customer comes in. When barber is cutting hair new
customers take empty seats to wait, or leave if no vacancy.
Classical problems of Synchronization with Semaphore Solution:-
In this article, we will see number of classical problems of synchronization as
examples of a large class of concurrency-control problems. In our solutions to
the problems, we use semaphores for synchronization, since that is the
traditional way to present such solutions. However, actual implementations of
these solutions could use mutex locks in place of binary semaphores.
These problems are used for testing nearly every newly proposed
synchronization scheme. The following problems of synchronization are
considered as classical problems:

57
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
4. Sleeping Barber Problem
These are summarized, for detailed explanation, you can view the linked articles
for each.
• Bounded-buffer (or Producer-Consumer) Problem:
Bounded Buffer problem is also called producer consumer problem.
This problem is generalized in terms of the Producer-Consumer
problem. Solution to this problem is, creating two counting semaphores
“full” and “empty” to keep track of the current number of full and empty
buffers respectively. Producers produce a product and consumers
consume the product, but both use of one of the containers each time.

• Dining-Philosophers Problem:
The Dining Philosopher Problem states that K philosophers seated
around a circular table with one chopstick between each pair of
philosophers. There is one chopstick between each philosopher. A
philosopher may eat if he can pickup the two chopsticks adjacent to
him. One chopstick may be picked up by any one of its adjacent
followers but not both. This problem involves the allocation of limited
resources to a group of processes in a deadlock-free and starvation-free
manner.

• Readers and Writers Problem:


Suppose that a database is to be shared among several concurrent
processes. Some of these processes may want only to read the database,
whereas others may want to update (that is, to read and write) the
database. We distinguish between these two types of processes by
referring to the former as readers and to the latter as writers. Precisely
in OS we call this situation as the readers-writers problem. Problem
parameters:
• One set of data is shared among a number of processes.

• Once a writer is ready, it performs its write. Only one writer


may write at a time.

58
• If a process is writing, no other process can read it.

• If at least one reader is reading, no other process can write.

• Readers may not write and only read.

• Sleeping Barber Problem:


Barber shop with one barber, one barber chair and N chairs to wait in.
When no customers the barber goes to sleep in barber chair and must
be woken when a customer comes in. When barber is cutting hair new
customers take empty seats to wait, or leave if no vacancy.

Semaphores :-
Semaphores are integer variables that are used to solve the critical section
problem by using two atomic operations, wait and signal that are used for process
synchronization.
The definitions of wait and signal are as follows −

• Wait
The wait operation decrements the value of its argument S, if it is
positive. If S is negative or zero, then no operation is performed.
wait(S)
{
while (S<=0);

S--;
}

• Signal
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}

59
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary
semaphores. Details about these are given as follows −

• Counting Semaphores
These are integer value semaphores and have an unrestricted value
domain. These semaphores are used to coordinate the resource access,
where the semaphore count is the number of available resources. If
the resources are added, semaphore count automatically incremented
and if the resources are removed, the count is decremented.
• Binary Semaphores
The binary semaphores are like counting semaphores but their value
is restricted to 0 and 1. The wait operation only works when the
semaphore is 1 and the signal operation succeeds when semaphore is
0. It is sometimes easier to implement binary semaphores than
counting semaphores.
Advantages of Semaphores
Some of the advantages of semaphores are as follows −

• Semaphores allow only one process into the critical section. They
follow the mutual exclusion principle strictly and are much more
efficient than some other methods of synchronization.
• There is no resource wastage because of busy waiting in semaphores
as processor time is not wasted unnecessarily to check if a condition
is fulfilled to allow a process to access the critical section.
• Semaphores are implemented in the machine independent code of the
microkernel. So they are machine independent.
Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −

• Semaphores are complicated so the wait and signal operations must


be implemented in the correct order to prevent deadlocks.
• Semaphores are impractical for last scale use as their use leads to loss
of modularity. This happens because the wait and signal operations
prevent the creation of a structured layout for the system.
• Semaphores may lead to a priority inversion where low priority
processes may access the critical section first and high priority
processes later
message passing:-
Message Passing provides a mechanism to allow processes to communicate and
to synchronize their actions without sharing the same address space.
For example − chat programs on World Wide Web.
Now let us discuss the message passing step by step.
Step 1 − Message passing provides two operations which are as follows −
• Send message
• Receive message
Messages sent by a process can be either fixed or variable size.

60
Step 2 − For fixed size messages the system level implementation is straight
forward. It makes the task of programming more difficult.
Step 3 − The variable sized messages require a more system level implementation
but the programming task becomes simpler.
Step 4 − If process P1 and P2 want to communicate they need to send a message
to and receive a message from each other that means here a communication link
exists between them.
Step 5 − Methods for logically implementing a link and the send() and receive()
operations.
Given below is the structure of message passing technique −

Characteristics
The characteristics of Message passing model are as follows −
• Mainly the message passing is used for communication.
• It is used in distributed environments where the communicating
processes are present on remote machines which are connected with
the help of a network.
• Here no code is required because the message passing facility provides
a mechanism for communication and synchronization of actions that
are performed by the communicating processes.
• Message passing is a time consuming process because it is
implemented through kernel (system calls).
• It is useful for sharing small amounts of data so that conflicts need
not occur.
• In message passing the communication is slower when compared to
shared memory technique.
Deadlock:-
A process in operating system uses resources in the following way.
1. Requests a resource
2. Use the resource
3. Releases the resource
61
A deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired by some
other process.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in
front of each other. A similar situation occurs in operating systems when there
are two or more processes that hold some resources and wait for resources held
by other(s). For example, in the below diagram, Process 1 is holding Resource 1
and waiting for resource 2 which is acquired by process 2, and process 2 is
waiting for resource 1.

Examples Of Deadlock
1. The system has 2 tape drives. P1 and P2 each hold one tape drive and
each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as
follows:
• P0 executes wait(A) and preempts.
• P1 executes wait(B).
• Now P0 and P1 enter in deadlock.

P0 P1

wait(A); wait(B)

wait(B); wait(A)

3. Assume the space is available for allocation of 200K bytes, and the
following sequence of events occurs.

P0 P1

62
Request Request
80KB; 70KB;

Request 60KB; Request 80KB;

Deadlock occurs if both processes progress to their second request.


Deadlock can arise if the following four conditions hold simultaneously
(Necessary Conditions)
Mutual Exclusion: Two or more resources are non-shareable (Only one process
can use at a time)
Hold and Wait: A process is holding at least one resource and waiting for
resources.
No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
Circular Wait: A set of processes waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance:
Prevention:
The idea is to not let the system into a deadlock state. This system will make
sure that above mentioned four conditions will not arise. These techniques are
very costly so we use this in cases where our priority is making a system
deadlock-free.
One can zoom into each category individually, Prevention is done by negating
one of the above-mentioned necessary conditions for deadlock. Prevention can
be done in four different ways:
1. Eliminate mutual exclusion 3. Allow preemption
2. Solve hold and Wait 4. Circular wait
Solution
Avoidance:
Avoidance is kind of futuristic. By using the strategy of “Avoidance”, we have to
make an assumption. We need to ensure that all information about resources that
the process will need is known to us before the execution of the process. We use
Banker’s algorithm (Which is in turn a gift from Dijkstra) to avoid deadlock.
In prevention and avoidance, we get the correctness of data but performance
decreases.
2) Deadlock detection and recovery: If Deadlock prevention or avoidance is not
applied to the software then we can handle this by deadlock detection and
recovery. which consist of two phases:
1. In the first phase, we examine the state of the process and check
whether there is a deadlock or not in the system.
2. If found deadlock in the first phase then we apply the algorithm for
recovery of the deadlock.
In Deadlock detection and recovery, we get the correctness of data but
performance decreases.
3) Deadlock ignorance: If a deadlock is very rare, then let it happen and reboot
the system. This is the approach that both Windows and UNIX take. we use the
ostrich algorithm for deadlock ignorance.
In Deadlock, ignorance performance is better than the above two methods but
the correctness of data.

63
Conditions for Deadlock in Operating System:-
Deadlock is a situation which involves the interaction of more than one resources
and processes with each other. We can visualise the occurrence of deadlock as a
situation where there are two people on a staircase. One is ascending the
staircase while the other is descending. The staircase is so narrow that it can
only fit one person at a time. As a result, one has to retreat while the other moves
on and uses the staircase. Once that person has finished, the other one can use
that staircase. But here, neither person is willing to retreat and waits for the other
to retreat. None of them are able to use the staircase. The people here are the
processes, and the staircase is the resource. When a process requests for the
resource that is been held another process which needs another resource to
continue, but is been held by the first process, then it is called

a deadlock. There
are four conditions necessary for the occurrence of a deadlock. They can be
understood with the help of the above illustrated example of staircase:
1. Mutual Exclusion: When two people meet in the landings, they can’t just
walk through because there is space only for one person. This condition
allows only one person (or process) to use the step between them (or
the resource) is the first condition necessary for the occurrence of the
deadlock.
2. Hold and Wait: When the two people refuse to retreat and hold their
ground, it is called holding. This is the next necessary condition for
deadlock.
3. No Preemption: For resolving the deadlock one can simply cancel one
of the processes for other to continue. But the Operating System doesn’t
do so. It allocates the resources to the processors for as much time as
is needed until the task is completed. Hence, there is no temporary
reallocation of the resources. It is the third condition for deadlock.
4. Circular Wait: When the two people refuse to retreat and wait for each
other to retreat so that they can complete their task, it is called circular
wait. It is the last condition for deadlock to occur.

Resource Allocation Graph (RAG) in Operating System:-

64
As Banker’s algorithm using some kind of table like allocation, request, available
all that thing to understand what is the state of the system. Similarly, if you want
to understand the state of the system instead of using those table, actually tables
are very easy to represent and understand it, but then still you could even
represent the same information in the graph. That graph is called Resource
Allocation Graph (RAG).
So, resource allocation graph is explained to us what is the state of the system
in terms of processes and resources. Like how many resources are available,
how many are allocated and what is the request of each process. Everything can
be represented in terms of the diagram. One of the advantages of having a
diagram is, sometimes it is possible to see a deadlock directly by using RAG, but
then you might not be able to know that by looking at the table. But the tables
are better if the system contains lots of process and resource and Graph is better
if the system contains less number of process and resource.
We know that any graph contains vertices and edges. So RAG also contains
vertices and edges. In RAG vertices are two type –
1. Process vertex – Every process will be represented as a process
vertex.Generally, the process will be represented with a circle.
2. Resource vertex – Every resource will be represented as a resource vertex. It
is also two type –
• Single instance type resource – It represents as a box, inside the box,
there will be one dot.So the number of dots indicate how many instances
are present of each resource type.
• Multi-resource instance type resource – It also represents as a box,
inside the box, there will be many dots present.

Now coming to the edges of RAG.There are two types of edges in RAG –
1. Assign Edge – If you already assign a resource to a process then it is called
Assign edge.
2. Request Edge – It means in future the process might want some resource to
complete the execution, that is called request edge.

65
So, if a process is using a resource, an arrow is drawn from the resource node to
the process node. If a process is requesting a resource, an arrow is drawn from
the process node to the resource node.
Example 1 (Single instances RAG) –

If there is a cycle in the Resource Allocation Graph and each resource in the cycle
provides only one instance, then the processes will be in deadlock. For example,
if process P1 holds resource R1, process P2 holds resource R2 and process P1 is
waiting for R2 and process P2 is waiting for R1, then process P1 and process P2
will be in deadlock.

66
Here’s another example, that shows Processes P1 and P2 acquiring resources R1
and R2 while process P3 is waiting to acquire both resources. In this example,
there is no deadlock because there is no circular dependency.
So cycle in single-instance resource type is the sufficient condition for deadlock.
Example 2 (Multi-instances RAG) –

From the above example, it is not possible to say the RAG is in a safe state or in
an unsafe state.So to see the state of this RAG, let’s construct the allocation
matrix and request matrix.

67
• The total number of processes are three; P1, P2 & P3 and the total
number of resources are two; R1 & R2.
Allocation matrix –
• For constructing the allocation matrix, just go to the resources and see
to which process it is allocated.
• R1 is allocated to P1, therefore write 1 in allocation matrix and similarly,
R2 is allocated to P2 as well as P3 and for the remaining element just
write 0.
Request matrix –
• In order to find out the request matrix, you have to go to the process
and see the outgoing edges.
• P1 is requesting resource R2, so write 1 in the matrix and similarly, P2
requesting R1 and for the remaining element write 0.
So now available resource is = (0, 0).
Checking deadlock (safe or not) –

So, there is no deadlock in this RAG.Even though there is a cycle, still there
is no deadlock.Therefore in multi-instance resource cycle is not sufficient
condition for deadlock.

68
Above example is the same as the previous example except that, the
process P3 requesting for resource R1.
So the table becomes as shown in below.

So,the Available resource is = (0, 0), but requirement are (0, 1), (1, 0) and
(1, 0).So you can’t fulfill any one requirement.Therefore, it is in deadlock.
Therefore, every cycle in a multi-instance resource type graph is not a
deadlock, if there has to be a deadlock, there has to be a cycle.So, in case
of RAG with multi-instance resource type, the cycle is a necessary
condition for deadlock, but not sufficient.

Handling Deadlocks:-
Deadlock is a situation where a process or a set of processes is blocked, waiting
for some other resource that is held by some other waiting process. It is an
undesirable state of the system. The following are the four conditions that must
hold simultaneously for a deadlock to occur.

69
1. Mutual Exclusion – A resource can be used by only one process at a
time. If another process requests for that resource then the requesting
process must be delayed until the resource has been released.
2. Hold and wait – Some processes must be holding some resources in the
non-shareable mode and at the same time must be waiting to acquire
some more resources, which are currently held by other processes in
the non-shareable mode.
3. No pre-emption – Resources granted to a process can be released back
to the system only as a result of voluntary action of that process after
the process has completed its task.
4. Circular wait – Deadlocked processes are involved in a circular chain
such that each process holds one or more resources being requested by
the next process in the chain.
Methods of handling deadlocks: There are four approaches to dealing with
deadlocks.
1. Deadlock Prevention
2. Deadlock avoidance (Banker's Algorithm)
3. Deadlock detection & recovery
4. Deadlock Ignorance (Ostrich Method)
These are explained below.
1. Deadlock Prevention: The strategy of deadlock prevention is to design the
system in such a way that the possibility of deadlock is excluded. The indirect
methods prevent the occurrence of one of three necessary conditions of
deadlock i.e., mutual exclusion, no pre-emption, and hold and wait. The direct
method prevents the occurrence of circular wait. Prevention techniques
– Mutual exclusion – are supported by the OS. Hold and Wait – the condition can
be prevented by requiring that a process requests all its required resources at
one time and blocking the process until all of its requests can be granted at the
same time simultaneously. But this prevention does not yield good results
because:
• long waiting time required
• inefficient use of allocated resource
• A process may not know all the required resources in advance
No pre-emption – techniques for ‘no pre-emption are’
• If a process that is holding some resource, requests another resource
that can not be immediately allocated to it, all resources currently being
held are released and if necessary, request again together with the
additional resource.
• If a process requests a resource that is currently held by another
process, the OS may pre-empt the second process and require it to
release its resources. This works only if both processes do not have the
same priority.
Circular wait One way to ensure that this condition never holds is to impose a
total ordering of all resource types and to require that each process requests
resources in increasing order of enumeration, i.e., if a process has been allocated
resources of type R, then it may subsequently request only those resources of
types following R in ordering.
2. Deadlock Avoidance: The deadlock avoidance Algorithm works by
proactively looking for potential deadlock situations before they occur. It does
this by tracking the resource usage of each process and identifying conflicts that
could potentially lead to a deadlock. If a potential deadlock is identified, the
algorithm will take steps to resolve the conflict, such as rolling back one of the
processes or pre-emptively allocating resources to other processes. The

70
Deadlock Avoidance Algorithm is designed to minimize the chances of a
deadlock occurring, although it cannot guarantee that a deadlock will never
occur. This approach allows the three necessary conditions of deadlock but
makes judicious choices to assure that the deadlock point is never reached. It
allows more concurrency than avoidance detection A decision is made
dynamically whether the current resource allocation request will, if granted,
potentially lead to deadlock. It requires knowledge of future process requests.
Two techniques to avoid deadlock :
1. Process initiation denial
2. Resource allocation denial
Advantages of deadlock avoidance techniques:
• Not necessary to pre-empt and rollback processes
• Less restrictive than deadlock prevention
Disadvantages :
• Future resource requirements must be known in advance
• Processes can be blocked for long periods
• Exists a fixed number of resources for allocation

Banker’s Algorithm:
The Banker’s Algorithm is based on the concept of resource allocation graphs. A
resource allocation graph is a directed graph where each node represents a
process, and each edge represents a resource. The state of the system is
represented by the current allocation of resources between processes. For
example, if the system has three processes, each of which is using two resources,
the resource allocation graph would look like this:
Processes A, B, and C would be the nodes, and the resources they are using would
be the edges connecting them. The Banker’s Algorithm works by analyzing the
state of the system and determining if it is in a safe state or at risk of entering a
deadlock.
To determine if a system is in a safe state, the Banker’s Algorithm uses two
matrices: the available matrix and the need matrix. The available matrix contains
the amount of each resource currently available. The need matrix contains the
amount of each resource required by each process.
The Banker’s Algorithm then checks to see if a process can be completed without
overloading the system. It does this by subtracting the amount of each resource
used by the process from the available matrix and adding it to the need matrix.
If the result is in a safe state, the process is allowed to proceed, otherwise, it is
blocked until more resources become available.
The Banker’s Algorithm is an effective way to prevent deadlocks in
multiprogramming systems. It is used in many operating systems, including
Windows and Linux. In addition, it is used in many other types of systems, such
as manufacturing systems and banking systems.
The Banker’s Algorithm is a powerful tool for resource allocation problems, but
it is not foolproof. It can be fooled by processes that consume more resources
than they need, or by processes that produce more resources than they need.
Also, it can be fooled by processes that consume resources in an unpredictable
manner. To prevent these types of problems, it is important to carefully monitor
the system to ensure that it is in a safe state.
3. Deadlock Detection: Deadlock detection is used by employing an algorithm
that tracks the circular waiting and kills one or more processes so that the
deadlock is removed. The system state is examined periodically to determine if

71
a set of processes is deadlocked. A deadlock is resolved by aborting and
restarting a process, relinquishing all the resources that the process held.
• This technique does not limit resource access or restrict process action.
• Requested resources are granted to processes whenever possible.
• It never delays the process initiation and facilitates online handling.
• The disadvantage is the inherent pre-emption losses.
4. Deadlock Ignorance: In the Deadlock ignorance method the OS acts like the
deadlock never occurs and completely ignores it even if the deadlock occurs.
This method only applies if the deadlock occurs very rarely. The algorithm is
very simple. It says ” if the deadlock occurs, simply reboot the system and act
like the deadlock never occurred.” That’s why the algorithm is called the Ostrich
Algorithm.
Advantages:
• Ostrich Algorithm is relatively easy to implement and is effective in
most cases.
• It helps in avoiding the deadlock situation by ignoring the presence of
deadlocks.
Disadvantages:
• Ostrich Algorithm does not provide any information about the deadlock
situation.
• It can lead to reduced performance of the system as the system may be
blocked for a long time.
• It can lead to a resource leak, as resources are not released when the
system is blocked due to deadlock.

Deadlock Prevention:-

If we simulate deadlock with a table which is standing on its four legs then we can
also simulate four legs with the four conditions which when occurs
simultaneously, cause the deadlock.However, if we break one of the legs of the
table then the table will fall definitely. The same happens with deadlock, if we can
be able to violate one of the four necessary conditions and don't let them occur
together then we can prevent the deadlock.

Let's see how we can prevent each of the conditions.

1. Mutual Exclusion

Mutual section from the resource point of view is the fact that a resource can never
be used by more than one process simultaneously which is fair enough but that is
the main reason behind the deadlock. If a resource could have been used by more
than one process at the same time then the process would have never been waiting
for any resource.

PlayNext
Unmute

Current Time 0:00

72
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s

However, if we can be able to violate resources behaving in the mutually exclusive


manner then the deadlock can be prevented.

Spooling

For a device like printer, spooling can work. There is a memory associated with
the printer which stores jobs from each of the process into it. Later, Printer
collects all the jobs and print each one of them according to FCFS. By using this
mechanism, the process doesn't have to wait for the printer and it can continue
whatever it was doing. Later, it collects the output when it is produced.

Although, Spooling can be an effective approach to violate mutual exclusion but


it suffers from two kinds of problems.

1. This cannot be applied to every resource.


2. After some point of time, there may arise a race condition between the
processes to get space in that spool.

We cannot force a resource to be used by more than one process at the same time
since it will not be fair enough and some serious problems may arise in the
performance. Therefore, we cannot violate mutual exclusion for a process
practically.

2. Hold and Wait

Hold and wait condition lies when a process holds a resource and waiting for some
other resource to complete its task. Deadlock occurs because there can be more

73
than one process which are holding one resource and waiting for other in the
cyclic order.

However, we have to find out some mechanism by which a process either doesn't
hold any resource or doesn't wait. That means, a process must be assigned all the
necessary resources before the execution starts. A process must not wait for any
resource once the execution has been started.

!(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't
hold or you don't wait)

This can be implemented practically if a process declares all the resources


initially. However, this sounds very practical but can't be done in the computer
system because a process can't determine necessary resources initially.

Process is the set of instructions which are executed by the CPU. Each of the
instruction may demand multiple resources at the multiple times. The need
cannot be fixed by the OS.

The problem with the approach is:

1. Practically not possible.


2. Possibility of getting starved will be increases due to the fact that some
process may hold a resource for a very long time.

3. No Preemption

Deadlock arises due to the fact that a process can't be stopped once it starts.
However, if we take the resource away from the process which is causing deadlock
then we can prevent deadlock.

This is not a good approach at all since if we take a resource away which is being
used by the process then all the work which it has done till now can become
inconsistent.

Consider a printer is being used by any process. If we take the printer away from
that process and assign it to some other process then all the data which has been
printed can become inconsistent and ineffective and also the fact that the process
can't start printing again from where it has left which causes performance
inefficiency.

4. Circular Wait

To violate circular wait, we can assign a priority number to each of the resource.
A process can't request for a lesser priority resource. This ensures that not a single
process can request a resource which is being utilized by some other process and
no cycle will be formed.

74
Among all the methods, violating Circular wait is the only approach that can be
implemented practically.

Deadlock avoidance:-

In deadlock avoidance, the request for any resource will be granted if the resulting
state of the system doesn't cause deadlock in the system. The state of the system
will continuously be checked for safe and unsafe states.

In order to avoid deadlocks, the process must tell OS, the maximum number of
resources a process can request to complete its execution.

The simplest and most useful approach states that the process should declare the
maximum number of resources of each type it may ever need. The Deadlock
avoidance algorithm examines the resource allocations so that there can never be
a circular wait condition.

Safe and Unsafe States

PlayNext
Unmute

Current Time 0:00

Duration 18:10
Loaded: 0.37%
Â
Fullscreen

75
Backward Skip 10sPlay VideoForward Skip 10s

The resource allocation state of a system can be defined by the instances of


available and allocated resources, and the maximum instance of the resources
demanded by the processes.

A state of a system recorded at some random time is shown below.

Resources Assigned

Process Type 1 Type 2 Type 3 Type 4

A 3 0 2 2

B 0 0 1 1

C 1 1 1 0

D 2 1 4 0

Resources still needed

Process Type 1 Type 2 Type 3 Type 4

A 1 1 0 0

B 0 1 1 2

C 1 2 1 0

D 2 1 1 2

1. E = (7 6 8 4)
2. P = (6 2 8 3)
3. A = (1 4 0 1)

Above tables and vector E, P and A describes the resource allocation state of a
system. There are 4 processes and 4 types of the resources in a system. Table 1
shows the instances of each resource assigned to each process.

Table 2 shows the instances of the resources, each process still needs. Vector E is
the representation of total instances of each resource in the system.

Vector P represents the instances of resources that have been assigned to


processes. Vector A represents the number of resources that are not in use.

76
A state of the system is called safe if the system can allocate all the resources
requested by all the processes without entering into deadlock.

If the system cannot fulfill the request of all processes then the state of the system
is called unsafe.

The key of Deadlock avoidance approach is when the request is made for
resources then the request must only be approved in the case if the resulting state
is also a safe state.

Deadlock Detection :
1. If resources have a single instance –
In this case for Deadlock detection, we can run an algorithm to check for the
cycle in the Resource Allocation Graph. The presence of a cycle in the graph is a
sufficient condition for deadlock.

In the above diagram, resource 1 and resource 2 have single instances. There is
a cycle R1 → P1 → R2 → P2. So, Deadlock is Confirmed.

2. If there are multiple instances of resources –


Detection of the cycle is necessary but not a sufficient condition for deadlock
detection, in this case, the system may or may not be in deadlock varies
according to different situations.
3. Wait-For Graph Algorithm –
The Wait-For Graph Algorithm is a deadlock detection algorithm used to detect
deadlocks in a system where resources can have multiple instances. The
algorithm works by constructing a Wait-For Graph, which is a directed graph that
represents the dependencies between processes and resources
Recovery from Deadlock in Operating System:-

. Process Termination:
To eliminate the deadlock, we can simply kill one or more processes. For this,
we use two methods:

77
• (a). Abort all the Deadlocked Processes:
Aborting all the processes will certainly break the deadlock, but with a
great expense. The deadlocked processes may have computed for a long
time and the result of those partial computations must be discarded and
there is a probability to recalculate them later.

• (b). Abort one process at a time until deadlock is eliminated:


Abort one deadlocked process at a time, until deadlock cycle is
eliminated from the system. Due to this method, there may be
considerable overhead, because after aborting each process, we have to
run deadlock detection algorithm to check whether any processes are
still deadlocked.

2. Resource Preemption:
To eliminate deadlocks using resource preemption, we preempt some resources
from processes and give those resources to other processes. This method will
raise three issues –
• (a). Selecting a victim:
We must determine which resources and which processes are to be
preempted and also the order to minimize the cost.

• (b). Rollback:
We must determine what should be done with the process from which
resources are preempted. One simple idea is total rollback. That means
abort the process and restart it.

• (c). Starvation:
In a system, it may happen that same process is always picked as a
victim. As a result, that process will never complete its designated task.
This situation is called Starvation and must be avoided. One solution is
that a process must be picked as a victim only a finite number of times.

Memory Management :-
The term Memory can be defined as a collection of data in a specific format. It is
used to store instructions and process data. The memory comprises a large array
or group of words or bytes, each with its own location. The primary motive of a
computer system is to execute programs. These programs, along with the
information they access, should be in the main memory during execution. The
CPU fetches instructions from memory according to the value of the program
counter.
To achieve a degree of multiprogramming and proper utilization of memory,
memory management is important. Many memory management methods exist,
reflecting various approaches, and the effectiveness of each algorithm depends
on the situation.
In a multiprogramming computer, the operating system resides in a part of
memory and the rest is used by multiple processes. The task of subdividing
the memory among different processes is called memory management.
Memory management is a method in the operating system to manage
operations between main memory and disk during process execution. The
main aim of memory management is to achieve efficient utilization of
memory.

78
Why Memory Management is required:

• Allocate and de-allocate memory before and after process execution.


• To keep track of used memory space by processes.
• To minimize fragmentation issues.
• To proper utilization of main memory.
• To maintain data integrity while executing of process.

Virtual Memory :-
Virtual Memory is a storage allocation scheme in which secondary memory can
be addressed as though it were part of the main memory. The addresses a
program may use to reference memory are distinguished from the addresses the
memory system uses to identify physical storage sites, and program-generated
addresses are translated automatically to the corresponding machine addresses.
The size of virtual storage is limited by the addressing scheme of the computer
system and the amount of secondary memory is available not by the actual
number of the main storage locations.
It is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical
addresses in computer memory.
1. All memory references within a process are logical addresses that are
dynamically translated into physical addresses at run time. This means
that a process can be swapped in and out of the main memory such that
it occupies different places in the main memory at different times
during the course of execution.
2. A process may be broken into a number of pieces and these pieces need
not be continuously located in the main memory during execution. The
combination of dynamic run-time address translation and use of page
or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or
segments are present in the main memory during execution. This means that the
required pages need to be loaded into memory whenever required. Virtual
memory is implemented using Demand Paging or Demand Segmentation
Memory management scheme:-
Memory management is the functionality of an operating system which
handles or manages primary memory and moves processes back and forth
between main memory and disk during execution. Memory management keeps
track of each and every memory location, regardless of either it is allocated
to some process or it is free. It checks how much memory is to be allocated to
processes. It decides which process will get memory at what time. It tracks
whenever some memory gets freed or unallocated and correspondingly it
updates the status.
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of
main memory (or move) to secondary storage (disk) and make that memory
available to other processes. At some later time, the system swaps back the
process from the secondary storage to main memory.
Though performance is usually affected by swapping process but it helps in
running multiple and big processes in parallel and that's the reason Swapping is
also known as a technique for memory compaction.

79
The total time taken by swapping process includes the time it takes to move the
entire process to a secondary disk and then to copy the process back to memory,
as well as the time the process takes to regain main memory.
Let us assume that the user process is of size 2048KB and on a standard hard disk
where swapping will take place has a data transfer rate around 1 MB per second.
The actual transfer of the 1000K process to or from memory will take
2048KB / 1024KB per second
= 2 seconds
= 2000 milliseconds
Now considering in and out time, it will take complete 4000 milliseconds plus
other overhead where the process competes to regain main memory.
Memory Allocation
Main memory usually has two partitions −
• Low Memory − Operating system resides in this memory.
• High Memory − User processes are held in high memory.
Operating system uses the following memory allocation mechanism.

80
S.N. Memory Allocation & Description

1 Single-partition allocation
In this type of allocation, relocation-register scheme is used to
protect user processes from each other, and from changing
operating-system code and data. Relocation register contains
value of smallest physical address whereas limit register contains
range of logical addresses. Each logical address must be less than
the limit register.

2 Multiple-partition allocation
In this type of allocation, main memory is divided into a number
of fixed-sized partitions where each partition should contain only
one process. When a partition is free, a process is selected from
the input queue and is loaded into the free partition. When the
process terminates, the partition becomes available for another
process.

Fragmentation
As processes are loaded and removed from memory, the free memory space is
broken into little pieces. It happens after sometimes that processes cannot be
allocated to memory blocks considering their small size and memory blocks
remains unused. This problem is known as Fragmentation.
Fragmentation is of two types −

S.N. Fragmentation & Description

1 External fragmentation
Total memory space is enough to satisfy a request or to reside a
process in it, but it is not contiguous, so it cannot be used.

2 Internal fragmentation
Memory block assigned to process is bigger. Some portion of
memory is left unused, as it cannot be used by another process.

The following diagram shows how fragmentation can cause waste of memory and
a compaction technique can be used to create more free memory out of
fragmented memory −

81
External fragmentation can be reduced by compaction or shuffle memory contents
to place all free memory together in one large block. To make compaction feasible,
relocation should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest
partition but large enough for the process.
Paging
A computer can address more memory than the amount physically installed on
the system. This extra memory is actually called virtual memory and it is a section
of a hard that's set up to emulate the computer's RAM. Paging technique plays an
important role in implementing virtual memory.
Paging is a memory management technique in which process address space is
broken into blocks of the same size called pages (size is power of 2, between 512
bytes and 8192 bytes). The size of the process is measured in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical)
memory called frames and the size of a frame is kept the same as that of a page
to have optimum utilization of the main memory and to avoid external
fragmentation.

82
Address Translation
Page address is called logical address and represented by page number and
the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame
number and the offset.
Physical Address = Frame number + page offset
A data structure called page map table is used to keep track of the relation
between a page of a process to a frame in physical memory.

83
When the system allocates a frame to any page, it translates this logical address
into a physical address and create entry into the page table to be used throughout
execution of the program.
When a process is to be executed, its corresponding pages are loaded into any
available memory frames. Suppose you have a program of 8Kb but your memory
can accommodate only 5Kb at a given point in time, then the paging concept will
come into picture. When a computer runs out of RAM, the operating system (OS)
will move idle or unwanted pages of memory to secondary memory to free up RAM
for other processes and brings them back when needed by the program.
This process continues during the whole execution of the program where the OS
keeps removing idle pages from the main memory and write them onto the
secondary memory and bring them back when required by the program.
Advantages and Disadvantages of Paging
Here is a list of advantages and disadvantages of paging −
• Paging reduces external fragmentation, but still suffer from internal
fragmentation.
• Paging is simple to implement and assumed as an efficient memory
management technique.
• Due to equal size of the pages and frames, swapping becomes very
easy.
• Page table requires extra memory space, so may not be good for a
system having small RAM.
Segmentation
Segmentation is a memory management technique in which each job is divided
into several segments of different sizes, one for each module that contains pieces
that perform related functions. Each segment is actually a different logical address
space of the program.
When a process is to be executed, its corresponding segmentation are loaded into
non-contiguous memory though every segment is loaded into a contiguous block
of available memory.

84
Segmentation memory management works very similar to paging but here
segments are of variable-length where as in paging pages are of fixed size.
A program segment contains the program's main function, utility functions, data
structures, and so on. The operating system maintains a segment map table for
every process and a list of free memory blocks along with segment numbers, their
size and corresponding memory locations in main memory. For each segment, the
table stores the starting address of the segment and the length of the segment. A
reference to a memory location includes a value that identifies a segment and an
offset.

Sharing Pages in OS:-

The paging system also has a problem with sharing as one of its design issues.

On a large computer system that is capable of running multiple programs at once,


it is not uncommon for multiple users to be occupied with the same program at
the same time.

Now, simply share the pages in order to prevent having two distinct copies of the
same page stored in your memory at the same time. The fact that not all of the
pages can be shared presents a challenge, however.

Pages that can only be read are generally shareable, such as the text of a program;
however, data pages are not.

85
When two or more than two processes (referred to collectively as multiple
processes) share some code, it can result in a problem with shared pages.

Let's say that process X and process Y are both running the editor and sharing its
pages. What would happen?

If the scheduler makes the decision to remove process X from memory, evicting
all of its pages and filling the empty page frames with the other program will cause
process Y to generate a large number of page faults in order to restore them. If
the scheduler makes the decision to remove process Y from memory, evicting all
of its pages and filling the empty page frames with the other program.

In a similar fashion, whenever the process X comes to an end, it is essential to be


able to discover that the pages are still in use. This ensures that the disc space
associated with those pages is not accidentally freed.

Protection Paging in Operating System:-


Paging is a memory management scheme that eliminates the need for contiguous
allocation of physical memory. The process of retrieving processes in the form of
pages from the secondary storage into the main memory is known as paging. The
basic purpose of paging is to separate each procedure into pages. Additionally,
frames will be used to split the main memory.This scheme permits the physical
address space of a process to be non – contiguous.
Let us look some important terminologies:
• Logical Address or Virtual Address (represented in bits): An address
generated by the CPU
• Logical Address Space or Virtual Address Space( represented in words or
bytes): The set of all logical addresses generated by a program
• Physical Address (represented in bits): An address actually available on
memory unit
• Physical Address Space (represented in words or bytes): The set of all
physical addresses corresponding to the logical addresses
Features of paging:
1. Mapping logical address to physical address.
2. Page size is equal to frame size.
3. Number of entries in a page table is equal to number of pages in logical
address space.
4. The page table entry contains the frame number.
5. All the page table of the processes are placed in main memory.
Example:
• If Logical Address = 31 bit, then Logical Address Space = 231 words = 2 G
words (1 G = 230)
• If Logical Address Space = 128 M words = 27 * 220 words, then Logical
Address = log2 227 = 27 bits
• If Physical Address = 22 bit, then Physical Address Space = 222 words = 4
M words (1 M = 220)

86
• If Physical Address Space = 16 M words = 24 * 220 words, then Physical
Address = log2 224 = 24 bits
The mapping from virtual to physical address is done by the memory management
unit (MMU) which is a hardware device and this mapping is known as paging
technique.
• The Physical Address Space is conceptually divided into a number of
fixed-size blocks, called frames.
• The Logical address Space is also splitted into fixed-size blocks,
called pages.
• Page Size = Frame Size
Let us consider an example:
• Physical Address = 12 bits, then Physical Address Space = 4 K words
• Logical Address = 13 bits, then Logical Address Space = 8 K words
• Page size = frame size = 1 K words (assumption)

Addr
ess generated by CPU is divided into
• Page number(p): Number of bits required to represent the pages in
Logical Address Space or Page number
• Page offset(d): Number of bits required to represent particular word in a
page or page size of Logical Address Space or word number of a page or
page offset.
Physical Address is divided into
• Frame number(f): Number of bits required to represent the frame of
Physical Address Space or Frame number.
• Frame offset(d): Number of bits required to represent particular word in
a frame or frame size of Physical Address Space or word number of a
frame or frame offset.
The hardware implementation of page table can be done by using dedicated
registers. But the usage of register for the page table is satisfactory only if page
table is small. If page table contain large number of entries then we can use
TLB(translation Look-aside buffer), a special, small, fast look up hardware cache.
• The TLB is associative, high speed memory.
• Each entry in TLB consists of two parts: a tag and a value.

87
• When this memory is used, then an item is compared with all tags
simultaneously.If the item is found, then corresponding value is
returned.

Sharing and protection Segmentation:-


A process is divided into Segments. The chunks that a program is divided into
which are not necessarily all of the same sizes are called segments. Segmentation
gives the user’s view of the process which paging does not give. Here the user’s
view is mapped to physical memory. There are types of segmentation:
1. Virtual memory segmentation – Each process is divided into a number
of segments, not all of which are resident at any one point in time.
2. Simple segmentation – Each process is divided into a number of
segments, all of which are loaded into memory at run time, though not
necessarily contiguously.
There is no simple relationship between logical addresses and physical
addresses in segmentation. A table stores the information about all such
segments and is called Segment Table.
Segment Table – It maps two-dimensional Logical address into one-dimensional
Physical address. It’s each table entry has:
• Base Address: It contains the starting physical address where the
segments reside in memory.
• Limit: It specifies the length of the segment.

Translation of Two dimensional Logical Address to dimensional Physical


Address.

88
Addre
ss generated by the CPU is divided into:
• Segment number (s): Number of bits required to represent the segment.
• Segment offset (d): Number of bits required to represent the size of the
segment.
Advantages of Segmentation –
• No Internal fragmentation.
Segment Table consumes less space in comparison to Page table in paging.
• As a complete module is loaded all at once, segmentation improves CPU
utilization.
• The user’s perception of physical memory is quite similar to
segmentation. Users can divide user programs into modules via
segmentation. These modules are nothing more than the separate
processes’ codes.
• The user specifies the segment size, whereas in paging, the hardware
determines the page size.
• Segmentation is a method that can be used to segregate data from
security operations.
• Flexibility: Segmentation provides a higher degree of flexibility than
paging. Segments can be of variable size, and processes can be designed
to have multiple segments, allowing for more fine-grained memory
allocation.
• Sharing: Segmentation allows for sharing of memory segments between
processes. This can be useful for inter-process communication or for
sharing code libraries.
• Protection: Segmentation provides a level of protection between
segments, preventing one process from accessing or modifying another
process’s memory segment. This can help increase the security and
stability of the system.
Disadvantage of Segmentation –
• As processes are loaded and removed from the memory, the free
memory space is broken into little pieces, causing External
fragmentation.
• Overhead is associated with keeping a segment table for each activity.
• Due to the need for two memory accesses, one for the segment table
and the other for main memory, access time to retrieve the instruction
increases.

89
• Fragmentation: As mentioned, segmentation can lead to external
fragmentation as memory becomes divided into smaller segments. This
can lead to wasted memory and decreased performance.
• Overhead: The use of a segment table can increase overhead and reduce
performance. Each segment table entry requires additional memory, and
accessing the table to retrieve memory locations can increase the time
needed for memory operations.
• Complexity: Segmentation can be more complex to implement and
manage than paging. In particular, managing multiple segments per
process can be challenging, and the potential for segmentation faults
can increase as a result

Virtual Memory:-
virtual Memory is a storage allocation scheme in which secondary memory can
be addressed as though it were part of the main memory. The addresses a
program may use to reference memory are distinguished from the addresses the
memory system uses to identify physical storage sites, and program-generated
addresses are translated automatically to the corresponding machine addresses.
The size of virtual storage is limited by the addressing scheme of the computer
system and the amount of secondary memory is available not by the actual
number of the main storage locations.
It is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical
addresses in computer memory.
1. All memory references within a process are logical addresses that are
dynamically translated into physical addresses at run time. This means
that a process can be swapped in and out of the main memory such that
it occupies different places in the main memory at different times
during the course of execution.
2. A process may be broken into a number of pieces and these pieces need
not be continuously located in the main memory during execution. The
combination of dynamic run-time address translation and use of page
or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or
segments are present in the main memory during execution. This means that the
required pages need to be loaded into memory whenever required. Virtual
memory is implemented using Demand Paging or Demand Segmentation.
Demand Paging :
The process of loading the page into memory on demand (whenever page fault
occurs) is known as demand paging.
The process includes the following steps :

90
1. If the CPU tries to refer to a page that is currently not available in the
main memory, it generates an interrupt indicating a memory access
fault.
2. The OS puts the interrupted process in a blocking state. For the
execution to proceed the OS must bring the required page into the
memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical
address space. The page replacement algorithms are used for the
decision-making of replacing the page in physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution
and it will place the process back into the ready state.
Hence whenever a page fault occurs these steps are followed by the operating
system and the required page is brought into memory.
Advantages :
• More processes may be maintained in the main memory: Because we are
going to load only some of the pages of any particular process, there is
room for more processes. This leads to more efficient utilization of the
processor because it is more likely that at least one of the more
numerous processes will be in the ready state at any particular time.
• A process may be larger than all of the main memory: One of the most
fundamental restrictions in programming is lifted. A process larger than
the main memory can be executed because of demand paging. The OS
itself loads pages of a process in the main memory as required.
• It allows greater multiprogramming levels by using less of the available
(primary) memory for each process.
Page Fault Service Time :
The time taken to service the page fault is called page fault service time. The
page fault service time includes the time taken to perform all the above six
steps.
Let Main memory access time is: m
Page fault service time is: s
91
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
Swapping:
Swapping a process out means removing all of its pages from memory, or
marking them so that they will be removed by the normal page replacement
process. Suspending a process ensures that it is not runnable while it is swapped
out. At some later time, the system swaps back the process from the secondary
storage to the main memory. When a process is busy swapping pages in and out
then this situation is called thrashing.

Thrashing :

92
At any given time, only a few pages of any process are in the main memory and
therefore more processes can be maintained in memory. Furthermore, time is
saved because unused pages are not swapped in and out of memory. However,
the OS must be clever about how it manages this scheme. In the steady-state
practically, all of the main memory will be occupied with process pages, so that
the processor and OS have direct access to as many processes as possible. Thus
when the OS brings one page in, it must throw another out. If it throws out a
page just before it is used, then it will just have to get that page again almost
immediately. Too much of this leads to a condition called Thrashing. The system
spends most of its time swapping pages rather than executing instructions. So a
good page replacement algorithm is required.
In the given diagram, the initial degree of multiprogramming up to some extent
of point(lambda), the CPU utilization is very high and the system resources are
utilized 100%. But if we further increase the degree of multiprogramming the
CPU utilization will drastically fall down and the system will spend more time
only on the page replacement and the time is taken to complete the execution of
the process will increase. This situation in the system is called thrashing.
Causes of Thrashing :
1. High degree of multiprogramming : If the number of processes keeps
on increasing in the memory then the number of frames allocated to
each process will be decreased. So, fewer frames will be available for
each process. Due to this, a page fault will occur more frequently and
more CPU time will be wasted in just swapping in and out of pages and
the utilization will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is
increased, frames per process are decreased. Hence CPU time will be
consumed in just swapping pages.

2. Lacks of Frames: If a process has fewer frames then fewer pages of that
process will be able to reside in memory and hence more frequent
swapping in and out will be required. This may lead to thrashing. Hence
sufficient amount of frames must be allocated to each process in order
to prevent thrashing.
Recovery of Thrashing :
• Do not allow the system to go into thrashing by instructing the long-
term scheduler not to bring the processes into memory after the
threshold.
• If the system is already thrashing then instruct the mid-term scheduler
to suspend some of the processes so that we can recover the system
from thrashing.
Performance in Virtual Memory:
• Let p be the page fault rate( 0 <= p <= 1).
• if p = 0 no page faults
• if p =1, every reference is a fault.
Effective access time (EAT) = (1 -p)* Memory Access Time + p * Page fault time.
Note: Page fault time = page fault overhead + swap out + swap in +restart
overhead

93
The performance of a virtual memory management system depends on the total
number of page faults, which depend on “paging policies” and “frame allocation”
Frame Allocation:
Number of frames allocating to each process in either static or dynamic.
• Static Allocation: The number of frame allocation to a process is fixed.
• Dynamic Allocation: The number of frames allocated to a process
changes.
Paging Policies:
• Fetch Policy: It decides when a page should be loaded into memory.
• Replacement Policy: It decides which page in memory should be
replaced.
• Placement Policy: It decides where in memory should a page be loaded.

Demand Paging:-

According to the concept of Virtual Memory, in order to execute some process,


only a part of the process needs to be present in the main memory which means
that only a few pages will only be present in the main memory at any time.

However, deciding, which pages need to be kept in the main memory and which
need to be kept in the secondary memory, is going to be difficult because we
cannot say in advance that a process will require a particular page at particular
time.

Therefore, to overcome this problem, there is a concept called Demand Paging is


introduced. It suggests keeping all pages of the frames in the secondary memory
until they are required. In other words, it says that do not load any page in the
main memory until it is required.

Whenever any page is referred for the first time in the main memory, then that
page will be found in the secondary memory.

After that, it may or may not be present in the main memory depending upon the
page replacement algorithm which will be covered later in this tutorial.

Page Replacement Algorithms in Operating Systems:-


In an operating system that uses paging for memory management, a page
replacement algorithm is needed to decide which page needs to be replaced when
a new page comes in.
Page Fault: A page fault happens when a running program accesses a memory
page that is mapped into the virtual address space but not loaded in physical
memory. Since actual physical memory is much smaller than virtual memory,
page faults happen. In case of a page fault, Operating System might have to
replace one of the existing pages with the newly needed page. Different page
replacement algorithms suggest different ways to decide which page to replace.
The target for all algorithms is to reduce the number of page faults.

Page Replacement Algorithms:

1. First In First Out (FIFO): This is the simplest page replacement algorithm. In
this algorithm, the operating system keeps track of all pages in the memory in a

94
queue, the oldest page is in the front of the queue. When a page needs to be
replaced page in the front of the queue is selected for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page
frames.Find the number of page faults.

Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty
slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is
not available in memory so it replaces the oldest page slot i.e 1. —>1 Page
Fault. 6 comes, it is also not available in memory so it replaces the oldest page
slot i.e 3 —>1 Page Fault. Finally, when 3 come it is not available so it replaces
0 1 page fault.

Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO)
page replacement algorithm. For example, if we consider reference strings 3, 2,
1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if we increase
slots to 4, we get 10-page faults.
2. Optimal Page replacement: In this algorithm, pages are replaced which would
not be used for the longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with
4 page frame. Find number of page fault.

95
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —
>4 Page faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7
because it is not used for the longest duration of time in the future.—>1 Page
fault. 0 is already there so —> 0 Page fault. 4 will takes place of 1 —> 1 Page
Fault.
Now for the further page reference string —> 0 Page fault because they are
already available in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating
system cannot know future requests. The use of Optimal Page replacement is to
set up a benchmark so that other replacement algorithms can be analyzed against
it.
3. Least Recently Used: In this algorithm, page will be replaced which is least
recently used.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2,
3 with 4 page frames. Find number of page faults.

Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —
>4 Page faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7
because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault

96
Now for the further page reference string —> 0 Page fault because they are
already available in the memory.
4. Most Recently Used (MRU): In this algorithm, page will be replaced which has
been used recently. Belady’s anomaly can occur in this algorithm.

Trashing:-

In computer science, thrashing occurs when a computer's virtual memory


resources are overused, leading to a constant state of paging and page faults,
inhibiting most application-level processing. It causes the performance of the
computer to degrade or collapse. The situation can continue indefinitely until the
user closes some running applications or the active processes free up additional
virtual memory resources.

To know more clearly about thrashing, first, we need to know about page fault
and swapping.

o Page fault: We know every program is divided into some pages. A page fault
occurs when a program attempts to access data or code in its address space
but is not currently located in the system RAM.
o Swapping: Whenever a page fault happens, the operating system will try to
fetch that page from secondary memory and try to swap it with one of the
pages in RAM. This process is called swapping.

Thrashing is when the page fault and swapping happens very frequently at a
higher rate, and then the operating system has to spend more time swapping these
pages. This state in the operating system is known as thrashing. Because of
thrashing, the CPU utilization is going to be reduced or negligible.s

The basic concept involved is that if a process is allocated too few frames, then
there will be too many and too frequent page faults. As a result, no valuable work
would be done by the CPU, and the CPU utilization would fall drastically.

97
The long-term scheduler would then try to improve the CPU utilization by loading
some more processes into the memory, thereby increasing the degree of
multiprogramming. Unfortunately, this would result in a further decrease in the
CPU utilization, triggering a chained reaction of higher page faults followed by an
increase in the degree of multiprogramming, called thrashing.

Thrashing is a condition or a situation when the system is spending a major


portion of its time servicing the page faults, but the actual processing done is very
negligible.
Causes of thrashing:
1. High degree of multiprogramming.
2. Lack of frames.
3. Page replacement policy.
Thrashing’s Causes
Thrashing has an impact on the operating system’s execution performance.
Thrashing also causes serious performance issues with the operating system.
When the CPU’s usage is low, the process scheduling mechanism tries to load
multiple processes into memory at the same time, increasing the degree of Multi
programming.
In this case, the number of processes in the memory exceeds the number of
frames available in the memory. Each process is given a set number of frames to
work with.
If a high-priority process arrives in memory and the frame is not vacant at the
moment, the other process occupying the frame will be moved to secondary
storage, and the free frame will be allotted to a higher-priority process.
We may also argue that as soon as the memory is full, the procedure begins to
take a long time to swap in the required pages. Because most of the processes are
waiting for pages, the CPU utilization drops again.
As a result, a high level of multi programming and a lack of frames are two of the
most common reasons for thrashing in the operating system.

98
The basic concept involved is that if a process is allocated to few frames, then
there will be too many and too frequent page faults. As a result, no useful work
would be done by the CPU and the CPU utilization would fall drastically. The long-
term scheduler would then try to improve the CPU utilization by loading some
more processes into the memory thereby increasing the degree of multi
programming. This would result in a further decrease in the CPU utilization
triggering a chained reaction of higher page faults followed by an increase in the
degree of multi programming, called Thrashing.
Locality Model –
A locality is a set of pages that are actively used together. The locality model states
that as a process executes, it moves from one locality to another. A program is
generally composed of several different localities which may overlap.
For example, when a function is called, it defines a new locality where memory
references are made to the instructions of the function call, it’s local and global
variables, etc. Similarly, when the function is exited, the process leaves this
locality.
Techniques to handle:
1. Working Set Model –
This model is based on the above-stated concept of the Locality Model.
The basic principle states that if we allocate enough frames to a process to
accommodate its current locality, it will only fault whenever it moves to some new
locality. But if the allocated frames are lesser than the size of the current locality,
the process is bound to thrash.
According to this model, based on parameter A, the working set is defined as the
set of pages in the most recent ‘A’ page references. Hence, all the actively used
pages would always end up being a part of the working set.
The accuracy of the working set is dependent on the value of parameter A. If A is
too large, then working sets may overlap. On the other hand, for smaller values of
A, the locality might not be covered entirely.

If D is the total demand for frames and is the working set size for process
i,

Now, if ‘m’ is the number of frames available in the memory, there are 2
possibilities:
• (i) D>m i.e. total demand exceeds the number of frames, then thrashing
will occur as some processes would not get enough frames.
• (ii) D<=m, then there would be no thrashing.
2. Page Fault Frequency –
A more direct approach to handling thrashing is the one that uses the Page-Fault
Frequency concept.

99
The problem associated with Thrashing is the high page fault rate and thus, the
concept here is to control the page fault rate.
If the page fault rate is too high, it indicates that the process has too few frames
allocated to it. On the contrary, a low page fault rate indicates that the process
has too many frames.
Upper and lower limits can be established on the desired page fault rate as shown
in the diagram.
If the page fault rate falls below the lower limit, frames can be removed from the
process. Similarly, if the page fault rate exceeds the upper limit, more frames can
be allocated to the process.
In other words, the graphical state of the system should be kept limited to the
rectangular region formed in the given diagram.
Here too, if the page fault rate is high with no free frames, then some of the
processes can be suspended and frames allocated to them can be reallocated to
other processes. The suspended processes can then be restarted later.
62

100

You might also like