0% found this document useful (0 votes)
102 views102 pages

Operating System Notes For Interview Preparation

These notes provide a comprehensive overview of Operating Systems (OS) for interview preparation, covering essential topics such as introduction, process management, memory management, storage management, and security. The document includes definitions, goals, functions, features, advantages, and disadvantages of OS, as well as various types and generations of operating systems. Additionally, it outlines services provided by OS and includes an index for easy navigation through the material.

Uploaded by

Himanshu Gupta
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)
102 views102 pages

Operating System Notes For Interview Preparation

These notes provide a comprehensive overview of Operating Systems (OS) for interview preparation, covering essential topics such as introduction, process management, memory management, storage management, and security. The document includes definitions, goals, functions, features, advantages, and disadvantages of OS, as well as various types and generations of operating systems. Additionally, it outlines services provided by OS and includes an index for easy navigation through the material.

Uploaded by

Himanshu Gupta
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/ 102

Operating System Notes for

Interview Preparation
These are notes for the Operating System which are specially designed for
interview preparation. In this, you will get to know every aspect of the
operating system from its introduction to security.

These notes are divided into 5 modules along with a lot of sub-topics.

Let’s dive into it without any further due.


Here is an index that helps you to track the topics of this domain of OS
because operating system is a subject that makes you go into the depth of
a single topic (if you find interest).

INDEX

Sr. No. Modules covered Subtopics

1 Introduction to Operating System ●​ Introduction


●​ Some Important Definitions
●​ Goals of OS
●​ Functions of OS
●​ Features of OS
●​ Advantages of OS
●​ Disadvantages of OS
●​ Knowledge With Fun
●​ Generation of OS
●​ Types of OS
●​ Services provided by OS
●​ System Calls
●​ Types of System Call
●​ System Boot

2 Process Management
3 Memory Management

4 Storage Management

5 Protection and Security



Module 1: Introduction to Operating System

Introduction

An Operating System is a piece of software that manages all the resources


of a computer system, both hardware, and software, and provides an
environment in which the user can execute his/her programs in a
convenient and efficient manner.

●​ Operating systems are an essential part of any computer system.


●​ It is a software that manages the computer hardware, input/output
operations, and memory.
●​ Personal Computer (PCs) OS supports complex games, business
applications, and everything like that.
●​ Thus, some OS are designed to be convenient, others to be efficient
and others some combination of both convenient and efficient.

Some Important Definitions are:


●​ Computer Hardware: It is the collection of all the physical
components (such as ram, processor, fan, graphic card, and
many more) of the computer system.
●​ Computer Software: It is the collection of all the programs
stored in and executed by a computer system.
●​ System Software: It operates and controls the computer system
and provides a platform to run applications software. So, this
includes OS like Windows, Linux, Android, and MacOS.
●​ Application Software: It performs a specific task or a function for
the user.
For example: Microsoft Word, Excel, PowerPoint, Calculator,
and many more.
Figure 1: Abstract View of the components of a computer system

Now, we will discuss about the goals, functions, and features of an


operating system:

Goals of Operating System:


For better understanding and learning purpose, we have divided the goals
in two parts mainly i.e. User Goals and System Goals.

User Goals System Goals


●​ Convenient to use. ●​ Easy to design and create.
●​ Easy to learn and operate. ●​ Easy to maintain and operate.
●​ Reliable. ●​ Efficiency i.e. proper
●​ Safe. utilization of resources.
●​ Fast. ●​ Energy Conservation.
●​ Minimal User Interface. ●​ Maximum CPU utilization.
●​ High performance. ●​ High performance.
●​ Faster Job Execution. ●​ Less Process Starvation and
high priority Job Execution.
Functions of Operating System:
●​ It is an interface between the user and hardware (as you can see in
figure 1).
●​ Resource Allocator means that the OS decides how to allocate
system resources (like CPU, memory, and I/O devices) to specific
programs and users so that it can operate the computer system
efficiently and fairly.
●​ Process Management
●​ File Management.
●​ Device Management.
●​ Input/Output System Management.
●​ Control Program refers to and is especially concerned with the
operations and control of I/O devices.
●​ Ability to evolve means 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 the
present services.
●​ Security means the OS uses password protection to protect user
data and unauthorized access.
●​ Error detecting aids means that the OS is constantly looking at the
system to detect errors and avoid the malfunctioning of a system.
●​ Job Accounting means that the OS keeps track of time and
resources used by the various jobs and users.
●​ Control over the system performance means that it monitors the
overall system situation to help in improving its performance.
●​ Coordination means that the OS coordinates and assigns compilers
and other software to various users of computer systems.
NOTE: Process, File, Device, and I/O System Management covered in
other modules of the notes.

Features of Operating System:


●​ Protected and supervisor mode.
●​ Allows disk access and file system device.
●​ Driver Networking Security.
●​ Program Execution.
●​ Memory Management Virtual Memory.
●​ Multitasking.
●​ Handling I/O Operations.
●​ Manipulation of the file system.
●​ Error Detection and handling.
●​ Resource Allocation.
●​ Information and resource protection.

Advantages of Operating System:


●​ Allows users to hide the details of hardware by creating an
abstraction layer (same as abstraction in OOPs interview
preparation sheet).
●​ Easy to use with a GUI (Graphical User Interface).
●​ Offers an environment in which a user may execute programs/
applications.
●​ The OS must make sure that the computer system is
convenient to use.
●​ OS acts as an intermediary between applications and hardware
components.
●​ It provides the computer system resources with an easy to use
format.
●​ Acts as an intermediary between all hardwares and softwares
of the system.

Disadvantages of Operating System:


●​ If any issue occurs in the OS, you may lose all the contents
which have been stored in your system.
●​ OS software is quite expensive for small size organizations
which adds a burden on them.
Example: Windows, Linux, Macintosh.

●​ It is never entirely secure as a threat can occur at any time.


Knowledge With Fun
●​ The basic unit of computer storage is the ‘bit’. A bit contains one of
two values 0 and 1. All other storage in a computer is based on a
collection of bits. A ‘byte’ is 8 bits, a ‘Kilobyte(KB)’ is 1024 bytes, a
‘Megabyte(MB)’ is 1024 KB, a ‘GigaByte(GB)’ is 1024 MB, a
‘TeraByte(TB)’ is 1024 GB, a ‘PetaByte(PB)’ is 1024 TB and so on.
●​ First Operating System used for real world tasks was: GM-NAA I/O,
produced in 1956 by the General Motors Research division for its IBM
704.
○​ The Full Form of GM-NAA I/O is Input/Output system of
General Motors and North American Aviation.
●​ The Atlas Computer was one of the world's first supercomputers, in
use from 1962 (when it was claimed to be the most powerful
computer in the world) to 1972.

Now, we will understand the difference between the generation and types
of operating systems:
Generation refers to the ‘evolution’ in any particular thing but ‘types’ refers
to the different types of that thing in a particular generation.

Generation of Operating System:


There are simply 4 levels of generation are:

Generation Generation Electronic Devices Types of OS


Year Used Devices
First 1945-55 Vacuum Tubes Plugboards
Second 1955-65 Transistors Batch Systems

Third 1965-80 Integrated Circuits Multiprogramming


Fourth Since 1980 Personal Computers

Types of Operating Systems: The types of operating system are as


follows:
1.​ Batch Operating System.
2.​ Multiprogramming Operating System.
3.​ Time Sharing Operating System.
4.​ Real Time Operating System.
5.​ Mobile Operating System.

1.​ Batch Operating System:


●​ This type of operating system does not interact with the
computer directly.
●​ There is an operator which takes similar jobs having the same
requirement and groups them into batches.
●​ It is the responsibility of the operator to sort the jobs with similar
needs.
●​ In this type of OS, every user prepares their task/job on an
offline device called a punch card and submit it to the computer
operator.

​ Advantages:
1.​ It is easy to manage large work repeatedly in batch systems.
2.​ Multiple users can share the batch systems.
3.​ The idle time for the batch system is very less.

​ Disadvantages:
1.​ It is sometimes costly.
2.​ The computer operator should be well known with batch systems.
3.​ These systems are hard to debug.
4.​ The other jobs will have to wait for an unknown time if any job fails.

​ Examples: Payroll System, Bank Statements.

2.​ Multiprogramming Operating System:


●​ Remember, multiprogramming is an ability that processes or
executes more than one task.
●​ Multiprogramming OS are those which execute more than one
program using a single processor machine.
●​ The idea of multiprogramming came so that tasks efficiently use
the CPU.

​ Advantages:
1.​ Utilization of the CPU is increased because the CPU never
goes to an idle state.
2.​ Less response time which further increases performance.
3.​ It helps to improve the turnaround time for short time jobs.

​ Disadvantages:
1.​ Every management is required like memory, file kind of
management.
2.​ Scheduling is required.
3.​ It is a highly complex task.

​ Examples: Windows OS, Micro-Computer - XENIX.

3.​ Time Sharing Operating System:


●​ It requires an interactive computer system.
●​ It is a logical extension of multiprogramming.
●​ It is also known as Multitasking.
●​ In time sharing systems, the CPU system executes multiple
jobs by switching among them, but the switches occur so
frequently that the users can interact with each program while it
is running.
●​ This type of OS allows many users to share the computer
simultaneously.
●​ This type of OS uses CPU scheduling and multiprogramming.
●​ Actually, it needs an alarm to send an interrupt signal to the
CPU after every interval of time for context switching from one
task to another.
●​ The CPU executes every process or task for a specific period of
time quantum, so that every process gets an equal opportunity
for getting the CPU’s attention for execution which generally
reduces the problem of starvation.

​ Advantages:
1.​ Each task gets an equal opportunity.
2.​ Fewer chances of duplication.
3.​ CPU idle time can be reduced.
4.​ Idle time reduces means the CPU always processes
multiple tasks that means every task gets a response for
further processing i.e. response time increases.

​ Disadvantages:
1.​ A specially appointed person always needs to take care
of security.
2.​ Data communication issues.
3.​ Reliability issues.

​ Examples: Unix, Multics, etc

4.​ Real Time Operating System:


●​ This type of operating system serves real-time systems.
●​ The time interval required to process and respond to inputs is
very small and this interval of time is known as Response time.
●​ Real-time systems are used when there are very strict time
requirements like missile systems, air traffic control systems,
robots, etc.
●​ The operating system is meant for applications where time
constraints are very strict and even the shortest possible delay
is not acceptable, this operating system is known as Hard
Real-time operating system.
●​ And where there are time constraints are less strict, that
operating system known as Soft Real time operating system.

​ Advantages:
1.​ Maximum utilization of devices and systems.
2.​ The time assigned for shifting tasks in these systems is very
less.
3.​ These types of systems are error free.

​ Disadvantages:
1.​ The system’s resources are expensive.
2.​ The algorithms in these operating systems are very complex
because it is necessary to make this system error-free so this
error freeness makes it complex.
3.​ These systems are programmed in such a way that they don’t
prioritize their threads because these systems are very less
prone to task switching.

​ Examples:
●​ Scientific experiments, Medical Imaging Systems, Weapons
systems

5.​ Mobile Operating System:


●​ This operating system is specially designed for mobile phones
especially smartphones so that these phones make the daily
task easy.
●​ Easily sorts the tasks of their owner.
●​ These operating systems not only provide the functionality of
basic cell phones like calling, and basic calculations but even
extend their features and provides a variety of useful features
that cell phones don’t provide.

​ Advantages:
1.​ Millions of applications are available for users.
2.​ Android is open source.
3.​ Sharing things is very easy as compared to other OS.

​ Disadvantages:
1.​ This OS requires more battery for processing of various tasks in
the background which makes the smartphone's battery drained
after a regular interval of time.
2.​ Virus protection is not better.
3.​ Low specifications devices run slowly.

​ Examples: ANDROID
Services provided by Operating System:
●​ An operating system provides an environment for the execution of the
programs.
●​ It provides certain services to programs and users of those programs.
●​ Here are the OS Services:
○​ User Interface
○​ Program Execution
○​ I/O Operations
○​ File System Manipulation
○​ Communications
○​ Error Detection
○​ Resource Allocation
○​ Accounting
○​ Protection and Security
1.​ User Interface:
In simple words, operating systems have a user interface that is
commonly of two types: CLI (Command Line Interface) and GUI
(Graphical User Interface).
Command Line Interface:
●​ On systems with multiple command interpreters to choose from,
the interpreters are known as Shells.
○​ For example, on UNIX and Linux systems, a user can
choose among several different shells, including the
Bourne Shell, C-Shell, Bourne-Again Shell, Korn
Shell, and others. Third-party shells and free user-written
shells are also available.
●​ It needs commands(input/actions) for further work/task.
●​ Rather than having a User-friendly nature, it has developer
friendly nature. (Context for saying, it is not user friendly
because it needs a knowledge of programming and commands.
●​ All the commands are written by the developer in the
environment, commonly known as ‘Terminal’.
​ Graphical User Interface:
●​ It is made by the developers having in mind that all types of
users will be able to use computers whether they know about
programming or commands related stuff or not.
●​ It has a very beautiful environment for implementing the daily
tasks of users.
●​ It is the best example of Abstraction.

2.​ Program Execution:


Remember, the system must be able to load a program into the
memory and run that program and that program must be able to end
its execution either normally or abnormally.

3.​ I/O Operations:


●​ A process in execution may require I/O, which may involve a file
or an I/O device.
●​ For optimization and protection, users usually can’t control the
I/O devices directly.
●​ Therefore, the OS must provide a means to do I/O.

4.​ File System Manipulators:


In simple words, a program requires to read, write the files and
directories. They also need to create, delete, and search according to
given input. OS does permission management for these files i.e .,
allowing or denying access to these files and directories based on file
ownership.

5.​ Communications:
In simple words, there usually occurs when one program needs to
exchange information with another process. So, here the operating
system implements the methods called “Shared Message” or
“Message Passing” in which information is exchanged between
processes by OS.

6.​ Error Detection:


Errors can occur anywhere so the OS is required to be constantly
conscious of possible errors. So for each type of error, the OS should
take appropriate action to ensure correct and error-free computing.

7.​ Resource Allocation:


In simple words, the OS has access to limited resources so when
multiple jobs are running simultaneously, the OS needs to allocate
the resources consciously so that maximum output comes from less
resource consumption and remember this is one of the main goals of
the OS.

8.​ Accounting:
Simply, in order to improve anything, take out the statistics and
accounting data. It is very eminent which tracks which task or user
uses the most resources of the computer. Through this, computer
scientists are able to improve the services.

9.​ Protection and Security:


Everybody wants that all the information must be protected and
secured from unauthorized users. So, OS also plays this part and
provides a valuable service of Password either in form of PIN Code or
Biometric Locks.
This ensures that no unauthorized user accesses the data or control
to resources.
System Calls: System Calls provide an interface to the services made
available by the operating system.

●​ System Calls are the programmatic way in which a computer program


requests a service from the kernel of an OS.
●​ System call offers the services of the operating system to the user
programs via API (Application Programming Interface).
●​ These calls are generally available as routines written in C and C++.
●​ In simple words, System Calls provide an interface to the application
running in user space so that this stuff in user space can be in
contact with kernel space because kernel space has all the access to
the hardware and to communicate with hardware, system calls play
an important role for user space.
●​ Take a look in the following sequence and you will get to know the
hierarchy of system calls or transfer from Kernel mode to User Mode
and vice-versa.

Step 1: Firstly, the system is in user mode and the user executes the
process.
Step 2: After executing the process, a software interrupt is called.
Step 3: This call gives a high-priority task of switching from user mode to
kernel mode or vice-versa with the help of a system call.

Example 1: The user has to copy the file from one place to another place.
So, the following hierarchy is followed.
●​ User copy file from source place to destination place.
●​ User executes the process and here a software interrupt is called.
●​ This software interrupt tells the OS that one more high-priority task
comes for switching to kernel mode and then System Call Interface
(SCI) comes into picture.
●​ Then SCI switches the user mode to kernel mode.
●​ After this, it goes towards the required management like process
management, and file management for the implementation part and
completes the required sequence with the help of kernel and
hardware like:
●​ And as soon as the process completes, this kernel mode
switches to user mode and you will see the file at the
destination place.

Example 2: If you understand example 1 then this example is very easy to


understand and if you do not understand example 1 then after
understanding this example you will surely get example 1.
Figure 2: The handling of a user application invoking the open() system call

Types of System Call: Following are the types of system calls:


●​ Process Control
○​ end, abort
○​ load, execute
○​ create process, terminate process
○​ get process attributes, set process attributes
○​ wait for time
○​ wait event, signal event
○​ allocate and free memory

●​ File Management
○​ create file, delete file
○​ open, close
○​ read, write, reposition
○​ get file attributes, set file attributes

●​ Device Management
○​ request device, release device
○​ read, write, reposition
○​ get device attributes, set device attributes
○​ logically attach or detach devices

●​ Information Maintenance
○​ get time or date, set time or date
○​ get system data, set system data
○​ get process, file or device attributes
○​ set process, file or device attributes

●​ Communications
○​ create, delete communication connection
○​ send, receive messages
○​ transfer status information
○​ attach or detach remote devices

Example of System Call for Windows and Unix:


Types of Windows Unix
System Call
Process ●​ CreateProcess() ●​ fork()
Control ●​ ExitProcess() ●​ exit()
●​ WaitForSingleObject() ●​ wait()
File ●​ CreateFile() ●​ open()
Management ●​ ReadFile() ●​ read()
●​ WriteFile() ●​ write()
●​ CloseHandle() ●​ close()
Device ●​ SetConsoleMode() ●​ ioctl()
Management ●​ ReadConsole() ●​ read()
●​ WriteConsole() ●​ write()
Information ●​ GetCurrentProcessID() ●​ getpid()
Maintenance ●​ SetTimer() ●​ alarm()
●​ Sleep() ●​ sleep()
Communication ●​ CreatePipe() ●​ pipe()
s ●​ CreateFileMapping() ●​ shm_open()
●​ MapViewOfFile() ●​ mmap()
Protection ●​ SetFileSecurity() ●​ chmod()
●​ InitializeSecurityDescriptor() ●​ umask()
●​ SetSecurityDescriptorGroup() ●​ chown()
System Boot:
The process of starting the computer by loading the kernel is known as
Booting of the system.
On many computer systems, a small piece of code known as the
bootstrap program or bootstrap loader locates the kernel, loads it into
the main memory, and starts the further execution.

Here is the step-wise process of booting the OS is:


1.​ As you switch on the power
-​ power goes to the power supply of the CPU box then power goes to the various
other components of the CPU box.

2.​ Now, the CPU loads the BIOS by fetching the sequence of
instructions into the RAM from the BIOS(which is originally stored in
ROM).

3.​ Now, BIOS runs a test (which is commonly known as POST- Power
On Self Test) and initializes the hardware.
-​ Basically, start all the attached devices to make sure that the computer is working
properly or not (basically, it looks for RAM), if not working properly then produces
a beep sound.

4.​ Now, after performing the test, it will load some settings from a
memory area(which is backed by CMOS - Complementary Metal
Oxide Semiconductor).

5.​ After this, it will begin the search for MBR (Master Boot Record)
which exists on the zeroth index of a disk.

6.​ After reaching MBR, BIOS loads the program called Boot loader.
-​ Boot loader is also known as a Boot device where actually the operating system
boot’s instructions are present.

7.​ And after loading the boot loader, finally it loads the actual operating
system.
Above is the process followed by the CPU box and its components after
switching on the power button.

Here, is the ending of Module 1, now, We recommend you to go through


the following questions:

Question: What is Kernel?


Answer: 1. It is the heart of the Operating System.
2. It interacts directly with hardware.
3. It is part between the OS and the hardware.
4. It is the first part of the OS to load directly on startup.
5. FiveTypes of Kernel are:
a.​ Monolithic Kernel
b.​ Micro Kernel
c.​ Hybrid Kernel
d.​ Exo Kernel
e.​ Nanokernel

Question: What are Monolithic and Micro Kernel?


Answer: You can understand this question better if you relate it to address
spaces of user and kernel.

Monolithic Kernel: In this, an operating system is processed in kernel


mode and all the facilities are centralized in one place.

Micro Kernel: In this, it has different address spaces allotted for different
facilities like management, scheduling, services, etc.
Sr. No. Monolithic Kernel Micro Kernel
1. It is larger in size It is smaller in size
2. It is faster. It is slower.
3. Less reliable More reliable
4. Communication is faster because of Communication is not
the centralisation of the modules. performing well
because of context
switching, it makes
overhead between
user mode and kernel.

Question: Define Interrupt and System Call?


Answer: Interrupt, in simple words, temporarily stops the current tasks
and provides an urgent piece of task to the CPU for process, actually it
makes that task priority very high that is why the CPU needs to stop the
current task and process the task given by interrupt whereas System Calls
provides an interface to the services made available by the operating
system.

Question: What is UEFI?


Answer: UEFI stands for Unified Extensible Firmware Interface. It is an
upgraded version of BIOS(Basic Input Output System).
Nowadays, UEFI is in use because it includes more security features than
the legacy BIOS mode.

Question: What is EFI?


Answer: EFI stands for Extensible Firmware Interface. Like MBR, it helps
to load the program called Boot Loader but unlike MBR, it is not on the
zeroth index of the disk; instead, it exists in a partition of the disk called EFI
Partition.
Question: Name the Boot Loader of various Operating Systems.
Answer: The Boot Loader of various Operating Systems are:

Sr. No. Operating System Boot Loader


1. Windows BOOTMGR(it eventually loads the
winload.exe file)
2. Mac boot.efi
3. Linux LILO (Linux Loader)
LOADLIN(Load Linux)
GRUB (Grand Unified Bootloader) -

LILO - it allows for fast boot-up


LOADLIN - it is preferred for those computers which have multiple OS. It is
sometimes used as a backup bootloader for Linux in case LILO fails.
GRUB - It is preferred by many users of Red Hat Linux.

Question: Name some Boot devices.


Answer: A boot device is any piece of hardware containing the files
required for a computer to start.
For example, a hard drive, floppy disk drive, CD-ROM drive, DVD drive,
and USB jump drive are all considered bootable devices.

Question: What are Cold Booting and Warm Booting?


Answer: Cold Booting: It is a phenomenon of powering on the computer
from a powered-off state. It is also known as the Hard Boot.
Warm Booting: It refers to the boot process in which a system re-acquires
its starting state without disturbing the power source.
Module 2: Process Management
Process:
●​ A process is a term that refers to a program when it is in execution.
●​ It is the unit of work in most systems.
●​ It needs system resources such as CPU, and memory for the
fulfillment of its task.
●​ It comprises of following:
○​ Text-Section: contains program code.
○​ Program Counter: It is a computer register that manages the
memory address of the instruction to be executed next.
○​ Stack: A process generally also includes a stack that contains
temporary data like local variables, and parameters.
○​ Data Section: It contains Global variables.
○​ Heap: A process may also include a heap, which is memory
dynamically allocated during process run time.

Figure: Process in memory

Sr. no. Program Process


1.​ It contains a set of It is an instance of an
instructions designed to executing program.
complete a specific task.
2. It is a passive entity as it It is an active entity as it
resides in secondary is created during
memory. execution and loaded
into main memory
3. It is static. It is dynamic.
4. Program does not have Process has its own
a control block. control block called
Process Control Block.
5. It exists at a single place Process exists for a
and continues to exist limited span of time as it
until it is deleted. gets terminated after the
completion of the task.

Thread:
●​ A Thread is a unit of execution within a process.
●​ A process can have many threads either it has one or more.

Process State: As a process executes, it changes state. The state of a


process is defined by the current activity of that process.
As the process executes, its state changes as:
●​ New: The process is being created.
●​ Running: Instructions are in execution.
●​ Waiting: The process is waiting for an event to occur like I/O
completion or reception of a signal.
●​ Ready: The process is waiting to be assigned to a processor.
●​ Terminated: The process has finished its execution.
Remember:
1.​ There is a thing called Job Scheduler or Long Term Scheduler, it is
used for scheduling the jobs from Job queue to Ready Queue.
2.​ Transferring of process from the ready queue to running state, this
process is done by CPU Scheduler. Here, is a term used i.e.
Dispatch and device known as Dispatcher.
3.​ It is interesting to know that the number of processes that can be in a
ready queue in one turn can be governed by a Job Scheduler or Long
Term Scheduler and this is known as Degree of Multiprogramming.

Process Control Block:


1.​ It is defined as a data structure that stores all the information related
to the process.
2.​ It has all the attributes related to the process.
3.​ To identify any process, attributes are very useful and important.
Process Scheduling: This phenomenon of scheduling of processes is
represented by the following diagrams:
Advice: Kindly refer to Figure: Diagram of Process State and Figure:
Process Scheduling both parallelly so that you come to know the
connectivity and depth of this topic.

Swapping: The medium-term scheduler is that sometimes it can be


advantageous to remove a process from memory and thus reduce the
degree of multiprogramming, this scheme is known as Swapping.
You can understand this by the following diagram:

Context switching:
●​ A Context Switching is defined as a term in which a CPU is switched
towards another process that requires performing a state save of a
current process and a state restore of a different process.
●​ These are highly dependent on the system hardware support.
●​ Interrupts cause the OS to change a CPU from its current task and to
run a kernel routine, these interrupts type of operations happens
frequently.
●​ In simple words, when an interrupt occurs, the system needs to save
the current context of the process running on the CPU so that it can
restore that context when its processing is done (means suspending
that process and resuming it after some time).
●​ This context is saved in the PCB of the process.
●​ Context Switching speed varies from machine to machine, depending
on the memory speed, and the number of registers( simply depends
on hardware). The typical speed is a few milliseconds.

Zombie Process:
●​ It is defined as a process whose execution is completed but it still has
an entry in a process table.
Note: Only need to remember is that a zombie process is a process whose
execution is completed, all the resources are deallocated by the operating
system but still have an entry in a process table.

●​ To remove the zombie process from the process table, the parent
process needs to call the wait() so that the parent process reads the
exit() status of the child process then the zombie process removes
from the process table.

Orphan Process:
●​ An orphan process is a process whose parent process no longer
exists.
●​ In a zombie process, the parent process needs to call wait and reads
the exit() status of the zombie process so that it removes from the
process table but here in the case of the orphan process, the parent
did not call wait(), instead it got terminated or completes its execution
and goes out of scope and the child process becomes an orphan
process.

Interprocess Communication (IPC):


●​ Processes executing concurrently in the OS may be either
independent processes or cooperating processes.
●​ Independent process is a process that cannot affect or be affected by
the processes executing in the system. Simply, any process that does
not share data with any other process is independent.
●​ Co-operating process is a process that can affect or be affected by
the other processes executing in the system. Simply, any process
that shares data with another process is a cooperating process.

Reasons for providing an environment that allows process cooperation:

1.​ Information Sharing: There are situations when several processes of


users may be interested in the same piece of information, we must
provide an environment to allow concurrent access to such information.
2.​ Computation Speedup: To make the particular process fast, we divide it
into sub-processes, each of which will be executed in parallel with others.
It is important to note that such a speedup is possible when a computer
has multiple processing cores.
3.​ Modularity: To make the system in a modular fashion, divide the system
functions into separate processes or threads.
4.​ Convenience: This provides convenience to users for performing many
tasks at the same time.

●​ Co-operating processes require an interprocess communication


mechanism that will allow them to exchange data and information.
●​ There are two fundamental models of IPC: Shared Memory and
Message Passing.
●​ Shared Memory model defines a region of memory that is shared by
co-operating processes is established. They can exchange the
information by reading and writing data in a shared region.
●​ Message Passing model refers that communication taking place by
means of messages exchanged between the co-operating processes.
Important To note:
●​ A Thread is a basic unit of CPU utilization.
●​ It comprises a thread ID, a program counter, a register set, and a
stack.
●​ If a process has multiple threads of control, it can perform more than
one task at a time.
Benefits of Multithreaded programming:
The benefits are:
1.​ Responsiveness
2.​ Resource Sharing
3.​ Economy
4.​ Scalability

Process Synchronization

As we also discussed above, there are two types of processes in our


system:
●​ Independent Processes:
○​ Processes that do not affect or be affected by other processes
executing in the system.
○​ They do not share any data directly or indirectly.
●​ Cooperative processes:
○​ Processes that can affect or be affected by other processes
executing in the system.
○​ They can either directly share code and data or be allowed to
share data only through files or messages.

What is Process synchronization?


●​ Cooperating processes directly share a logical address space, which
results in data inconsistency.
●​ Process synchronization is a mechanism to coordinate processes
that are cooperative in nature and share some resources or data so
that data consistency is maintained.

Race Condition
●​ A situation where several processes access and manipulate the
shared data concurrently.
●​ This can result in the incorrect value of shared data as various
processes try to change it.
●​ The final value of that shared data depends upon which process
finishes last.
●​ Let’s understand it with an example:
○​ Consider we have a function that performs the following task:
Read (shared) //Line 1
shared= shared + 1 //Line 2
○​ Let’s say we have two processes P1 and P2 that will run
parallelly and run the above function. Initially consider the value
of ‘shared’ = 5 (i.e. the variable that is shared by both
processes).
○​ So, let’s say P1 starts executing line 1, so it will read the value
of ‘shared’ which comes out to be 5.
○​ Since P2 is also running concurrently it also reads the value of
‘shared’ which also comes out to be 5.
○​ After this P1 executes line 2, which makes the value of ‘shared’
= ‘shared’ +1 i.e. 5+1= 6, ‘shared’ becomes 6.
○​ Now, P2 continues execution, as P2 also read ‘shared’ as 5,
after executing line 2 ‘shared’ becomes 6.
○​ So, finally the value of the shared data that is ‘shared’ here
becomes 6. But actually, it should have been 7 as two
processes executed the function, which would have led to a +1
increment in the value of ‘shared’ two times.
○​ Since here both the processes are competing to modify the
value of ‘shared’ concurrently is leading to a race condition.

Critical Section problem


●​ When we have various processes competing to use some shared
data.
●​ Each process has a code segment, called the critical section in which
the shared data is accessed.
●​ The critical section problem is to design a mechanism such that
when one process is executing in its critical section, no other process
is allowed to execute in its critical section.
●​ Entry section is the section of code where each process requests
permission to enter its critical section.
●​ And the critical section is followed by an exit section.
●​ The remaining code is the remainder section.
Solution to Critical-Section Problem
A solution to the critical-section problem must satisfy the following three
requirements:
●​ Mutual exclusion: If a process is executing in its critical section then
no other processes can execute in their critical sections.
●​ Progress:
a)​ If no process is executing in its critical section and some
processes want to enter their critical sections.
b)​Only those processes that are not executing in their remainder
sections can participate in deciding which process will enter its
critical section next.
c)​ This selection cannot be postponed indefinitely.
●​ Bounded waiting: There is a limit 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
●​ It is a classic software-based solution to the critical-section problem.
●​ Peterson’s solution is restricted to two processes that alternate
execution between their critical sections and remainder sections.
●​ There are two shared variables between the processes
○​ int turn: this variable indicates whose turn it is to enter its
critical section.
■​ If turn == i, then Process Pi is allowed to execute in its
critical section.
○​ boolean flag[2]: flag array is used to indicate if a process is
ready to enter its critical section.
■​ If flag[i] == true, this indicates that Pi is ready to enter its
critical section.
●​ Let’s understand how Peterson’s solution preserves all three
conditions:
○​ Mutual exclusion: We can see that using the while( flag[j] &&
turn == j) statement when one process is executing in its critical
section no other process is allowed to enter the critical section
until the other process has exited from the critical section.
Hence, mutual exclusion is preserved.
○​ Progress: When the processes are not executing in the
remainder section they are allowed to make changes in the turn
variable, hence they can decide which process will enter the
critical section, and this decision depends on the process that
finally updated the value of turn. Hence progress is fulfilled.
○​ Bounded-waiting:
■​ Let’s say Pi and Pj try to enter the critical section so both
will send the request, Pi will set turn = j, and Pj will set turn
= i, and finally Pi will execute the critical section after it
exits Pj will now start executing the critical section.
■​ Hence both processes get a chance to execute critical
sections without waiting indefinitely.

Synchronization in hardware
●​ Software-based solutions such as Peterson’s are not guaranteed to
work on modern computer systems.
●​ Operating systems make use of locks to protect the critical section.
●​ For a single processor system we can solve the critical section
problem using a test and set lock function.
●​ Here we can prevent interrupts from occurring while a shared variable
is being modified and this will ensure that the current sequence of
instructions is executed in order without preemption.

Mutex locks
●​ One of the simplest hardware-based solutions to critical section
problem is the mutex lock.
●​ Mutex is a short form of mutual exclusion.
●​ There are two functions namely acquire() and release() .
●​ The acquire()function acquires the lock before entering the critical
section.

●​ The release() function releases the lock when it exits the critical
section.
●​ A boolean variable available indicates the availability of the lock.
●​ If the lock is available a call to acquire() succeeds, and the lock is
then marked unavailable.
Disadvantages of Mutex locks:
●​ Spinlocks are created i.e. when a process is in its critical
section and another process tries to enter the critical section,
then it must loop continuously in the call to acquire() and this
situation is called busy waiting.
●​ Busy waiting leads to wastage of CPU time and lowers
productivity.

Semaphore
●​ A semaphore S is an integer variable used to solve the critical section
problem.
●​ It can be accessed only through two standard atomic operations:
wait() and signal().
●​ Wait() operation also referred as P(from the Dutch proberen, ‘to test’).
●​ Signal() also referred to as V (from verhogen, ‘to increment’).
●​ Following are the definitions of wait() and signal() function:

●​ No two processes can simultaneously modify the same semaphore


value.
●​ Types of Semaphores
○​ Counting Semaphore:
■​ The value of can range over an unrestricted domain.
■​ These semaphores are used to control access to a given
resource, and the semaphore is initialized to the number
of resources available.
■​ Count of semaphore is incremented when the process
uses a resource and wait() operation is performed.
■​ Count of semaphore is decremented when the process
releases a resource and signal() operation is performed.

○​ Binary Semaphore:
■​ The value can range only between 0 and 1.
■​ Binary semaphores behave similarly to mutex locks.
■​ When the value of the semaphore is 1 then wait() works
i.e. then only the process can enter the critical section.
■​ signal() only works when the semaphore is 0.

Disadvantages of Semaphores:
●​ Semaphore also suffers the problem of busy waiting.
○​ This problem can be resolved if the process is blocked after it
goes to the wait() operation and detects that the semaphore
value is less than 0.
○​ Blocking the process will stop it from looping and the process
will be pushed into the waiting queue.
○​ A wakeup() operation is used to restart the process, which
changes its state from waiting to ready.
●​ The implementation of a semaphore with a waiting queue can lead to
some processes waiting indefinitely because they are dependent on
some event that can be only caused by any of the waiting processes
only, this situation leads to a deadlock (discussed further in the
module).
○​ The deadlock situation also leads to the starvation of
processes.
●​ Semaphore can lead to priority inversion, i.e. low priority processes
execute the critical section before the high priority processes.

Classical problems of Synchronisation


We will be looking into the three problems of synchronization. These
problems are used for testing any newly proposed solution synchronization
scheme. Here we will use semaphore as the solution to these problems.

1)​ Bounded-Buffer Problem:


Let's first understand the problem,
●​ We have a buffer with n slots and each slot can store one item.
●​ There are two processes Producer and Consumer that are
working on this buffer.
●​ Producer adds data into the empty slots of the buffer while
Consumer consumes data from a filled buffer slot.
●​ Producer cannot add data when buffer is full and the
consumer cannot remove data when buffer is empty.
●​ These two processes should not occur at the same time, so
here we see the need for synchronization of these two
processes.

Solution to Bounded-Buffer problem


●​ We have the following variables that are shared by the producer
and consumer.
int n; //number of buffer slots
semaphore mutex = 1; //a binary semaphore used to acquire
and set lock
semaphore empty = n; //a counting semaphore used to track
the number of empty slots, initialized with the total number of
slots.
semaphore full = 0; //a counting semaphore used to track the
number of full slots, initialized with zero.

●​ Code for producer process:


○​ As you can see, initially wait() operation is performed on
empty semaphore which will wait until empty>0, then it
will decrement empty (refer to the function definition of
wait()).
○​ Then the producer will acquire the lock by performing
wait(mutex) and will perform the task of adding an item to
the buffer.
○​ After that it will release the lock by performing
signal(mutex), i.e changing the value of mutex.
○​ Finally, we will increment the value of full by performing
signal(full), as one of the buffer slots is occupied now.

●​ Code for consumer process, which is symmetric to the


producer’s code:
2)​ Readers –Writers Problem
Let's first understand the problem,
●​ There are databases that are shared among different
concurrent processes, some processes only read from the
database while others may update (read and write) the
database.
●​ The former type of processes are referred to as readers and
the later ones as writers.
●​ Two readers simultaneously reading the database will not
cause any issues.
●​ But a writer and some other process (reader/ writer) accessing
the database at the same time can create a problem.
●​ Hence we need to find a solution to synchronize this problem.

Solution to Readers-Writers problem


●​ We have the following variables:
int readcount = 0; //used to keep track of the number of
processes reading the object
semaphore mutex = 1; //used to ensure mutual exclusion
when the variable readcount is updated
semaphore rw_mutex = 1; //functions as a mutual exclusion
semaphore for the writers

●​ Code for writer process:

●​ Code for writer process:


3)​ Dining-Philosophers Problem
Let's first understand the problem,
●​ There are five philosophers seated in a circular fashion, who
can do only two things that are eating and thinking.
●​ Each philosopher has a rice bowl to eat and the table has 5
single chopsticks.
●​ At a time a philosopher can either eat or think. And for eating a
philosopher must use two chopsticks closest to them.
●​ Philosophers can pick only one chopstick at a time, and cannot
use a chopstick which is currently used by some other
philosophers.
●​ Since the chopsticks are limited, we must ensure no two
adjacent philosophers access the same chopstick at the same
time.
●​ It is a representation of how resources can be allocated to
processes such that deadlock and starvation are prevented.


Fig: Situation of Dining-Philosophers

Solution to Dining-Philosophers problem


●​ We have the following data structures:
Semaphore chopstick[5]; // array of chopsticks, all elements
of this array are binary semaphores that are initialized to 1.
●​ Problems related to the above solution:
○​ Even though above solution prevents two adjacent
philosophers eating at the same time, but it can create a
deadlock.
○​ Let’s say, all five philosophers become hungry at the
same time and each grabs their left chopstick. If each
philosopher tries to grab their right chopstick, they will be
delayed forever.
○​ Remedies to solve this deadlock situation:
■​ At most four philosophers are allowed to sit at the
table at a time.
■​ A philosopher must pick the chopsticks only if both
chopsticks are available i.e he must pick them up in
a critical section.
■​ Asymmetric solution can be used, that is, an
odd-numbered philosopher picks up a chopstick at
their left and then at right. Even-numbered
philosopher first picks their right chopstick and then
the left one.
CPU Scheduling in Operating Systems

●​ CPU scheduling is the basis of multiprogrammed operating systems.


●​ The purpose of multiprogramming is to have some process running at
all times, to maximize CPU utilization.
●​ When one process has to wait, typically for the completion of some
I/O request, the operating system takes the CPU away from that
process and gives the CPU to another process present in the
memory. This pattern continues.
●​ Scheduling of this kind is a fundamental operating-system function

CPU Scheduler
●​ The operating system needs to select a process from the ready
queue to be executed whenever the CPU is idle.
●​ The selection process is carried out by the short-term scheduler, or
CPU scheduler.
●​ It selects a process from the memory that is ready to execute and
allocates the CPU to that process.

Dispatcher
The dispatcher is the module that gives control of the CPU to the process
selected by the short-term scheduler. This function involves the following:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that program

Terminologies in CPU Scheduling Algorithm:

●​ Arrival Time: Time at which process arrives in the ready queue.


●​ Completion Time: Time at which process completes its execution.
●​ Burst Time: Amount of time required by a process for executing on
CPU.
●​ Turn Around Time: Amount of time spent by the process from
coming in the ready state for the first time to its completion.
Turn Around Time = Completion Time - Arrival Time

●​ Waiting Time: Amount of time that a process spends waiting in the


ready queue, i.e difference between Turn Around time and Burst time.
Waiting Time = Turn Around time - Burst Time

Scheduling Criteria:
We must consider the properties of the various algorithms while choosing
which algorithm to use in a particular situation.
Many criteria have been suggested for comparing CPU-scheduling
algorithms. The criteria include the following:

●​ CPU utilization: Conceptually, CPU utilization can range from 0 to


100 percent, but in a real system, it ranges from 40 to 90 percent
depending on system load. Our goal is to have maximum CPU
utilization.

●​ Throughput: Throughput is defined as the number of processes that


are completed per time unit. It varies according to the duration of a
process. An algorithm must work to maximize throughput.

●​ Turnaround time: The interval from the time of arrival of a process in


the ready queue to the time of completion is the turnaround time. It is
the sum of the time spent waiting to get into memory, waiting in the
ready queue, executing on the CPU, and doing I/O. An algorithm
must work to minimize this time.

●​ Waiting time: It is the sum of the periods spent waiting in the ready
queue. CPU-scheduling algorithm does not affect the amount of time
during which a process executes or does I/O, it can only minimize this
waiting time.
●​ Response Time: Turn Around time may not be the best criterion for
interactive systems. A process can produce results earlier and
continue to calculate new results while previous results are passed
on to the user. Thus, another metric is the time elapsed from sending
a request to giving the first response. This measure is called
response time. The goal of CPU is to minimize this time.

Types of CPU scheduling

There are mainly two types of CPU scheduling:

●​ Non-Preemptive Scheduling:
○​ In non-preemptive scheduling, a new process is executed only
after the current process has completed its execution.
○​ The process retains CPU resources until it terminates or is
switched to waiting state.
○​ Once the process completes its execution, the CPU is allocated
to the next process in the ready queue.

●​ Preemptive Scheduling:
○​ Preemptive scheduling is used when a process switches from
running state to ready state or from waiting state to ready state.
○​ In simple words, in preemptive scheduling, CPU can switch
from a process that is not yet terminated to the next process in
the queue due to various reasons.
○​ CPU resources are allocated to a process only for a limited
period of time, after which those resources are taken over and
assigned to another process.
○​ If the process was yet to complete its execution, it remains in
the ready queue, till it gets a chance to execute once again.
Let us discuss different CPU-scheduling algorithms:

1)​ First Come First Serve Scheduling

●​ FCFS is one of the simplest CPU scheduling algorithms. In this


algorithm, the process that requests the CPU first is allocated
the CPU first and the implementation is easily managed with a
FIFO queue.
●​ FCFS is a non-preemptive scheduling algorithm and one
process executes until it terminates or performs I/O operations.
●​ Process with minimal arrival time will be executed first by the
CPU.
●​ When a process enters the ready queue, its PCB(Process
Control Block) is linked to the tail of the queue. When the CPU
is free, it is allocated to the process at the head of the queue.
The running process is then removed from the queue.

Let us consider the following example to understand the algorithm better:


Process Arrival Time Burst Time

P1 0 3

P2 2 2

P3 4 1

1)​ At time = 0,
P1 arrives and starts executing.​

2)​ At time = 2,
P2 arrives and is kept in the queue because P1 is still executing.


3)​ At time = 3,
P1 finishes execution. Since P2 is waiting in the queue, its execution
will start now. Currently, no process in the queue.


4)​ At time = 4,
P3 arrives and is kept in the queue since P2 is executing.


5)​ At time = 5,
P2 finishes execution. Since P3 is waiting in the queue it will be
executed now.

After the processes are served in FCFS order, we get the result shown in
the following Gantt chart, which is a bar chart that illustrates a particular
schedule, including the start and finish times of each of the participating
processes:
​ ​
Now let us calculate the Average waiting time for the above example:

Process Arrival Time Burst Time Completion Turn Around Waiting


(ms) (ms) Time (ms) Time (ms) Time (ms)
P1 0 3 3 3 0
P2 2 2 5 3 1
P3 4 1 6 2 1

Turn Around Time = Completion Time - Arrival Time


Waiting Time = Turn Around time - Burst Time

Average Waiting Time = (0 + 1 + 1 )/3= 2/3 = 0.67ms

Advantages of FCFS:
●​ It is the simplest scheduling algorithm and easy to implement.
●​ Works on a first come first serve basis.

Disadvantages of FCFS:
●​ Average waiting time under FCFS policy is high.
●​ It is a non-preemptive algorithm, once the CPU is allocated to a
process, that process retains the CPU until it terminates or requests
I/O.
●​ FCFS algorithm is not suitable for time-sharing systems.
●​ It suffers from the Convoy effect.
○​ A phenomenon in which some smaller processes wait for one
big process to execute first.
○​ This effect results in lower CPU and device utilization than
might be possible if the shorter processes were allowed to go
first.
2)​ Shortest Job First Scheduling
●​ In this scheduling algorithm, the process present in the ready
queue with the shortest burst time is selected next for
execution.
●​ In simple words, the selection criteria is the process's burst
time.
●​ It is a non-preemptive scheduling algorithm.
●​ If the next CPU bursts of two processes are the same, FCFS
scheduling is used to break the tie.
●​ Preemptive SJF is known as Shortest Remaining Time First
scheduling, which we will discuss in the upcoming parts.

Let us consider the following example to understand the algorithm better:

Process Arrival Time Burst Time

P1 0 3

P2 2 1

P3 0 2

P4 4 4

1)​ At time = 0,
Both P1 and P3 arrive, as per SJF scheduling, P3 will be selected
since it has a smaller burst time, and P1 will be kept in the ready
queue.​

2)​ At time = 2,
P3 finishes execution. P2 arrives and starts executing since the burst
time of P2 is smaller than P3, which is currently in the ready queue.


3)​ At time = 3,
P2 finishes execution. Since P1 is waiting in the queue, its execution
will start now. Currently, no process in the queue.


4)​ At time = 4,
P4 arrives and is kept in the queue since P1 is executing.


5)​ At time = 6,
P1 finishes execution. Since P4 is waiting in the queue it will be
executed now.

Final Gantt Chart:

Now let us calculate the Average waiting time for the above example:

Process Arrival Time Burst Time Completion Turn Around Waiting


(ms) (ms) Time (ms) Time (ms) Time (ms)
P1 0 3 6 6 3
P2 2 1 3 1 0
P3 0 2 2 2 0
P4 4 4 10 6 2

Turn Around Time = Completion Time - Arrival Time


Waiting Time = Turn Around time - Burst Time

Average Waiting Time = (3+ 0 + 0 + 2 )/4= 5/4 = 1.25ms

Advantages of SJF scheduling:


●​ SJF is optimal, as it gives the minimum average waiting time for a
given set of processes.
●​ Throughput is increased in SJF as it executes shorter jobs first which
generally have a shorter turnaround time.
●​ SJF scheduling is used frequently in long-term scheduling.

Disadvantages of SJF scheduling:


●​ Can lead to starvation of longer processes, i.e. due to the incoming
of shorter processes a large process never gets a chance to execute.
●​ In SJF, the completion time of a process is required beforehand,
which may be difficult to get.

3)​ Priority Scheduling


●​ In this scheduling algorithm, a process is selected based on
priority.
●​ A priority is associated with each process and the CPU is
allocated to the process with the highest priority.
●​ Priorities can be defined either internally or externally, internal
factors include memory requirements, time limit, etc, and
external factors are set outside the OS, for example, the
importance of the process.
●​ Priority scheduling can either be preemptive or non-preemptive.
In this module, we will go through preemptive priority
scheduling.
●​ A preemptive priority scheduling algorithm will preempt the CPU
if the priority of the newly arrived process is higher than the
priority of the currently running process.
●​ Equal priority processes are scheduled in FCFS order.
Let us consider the following example to understand the algorithm better:

Process Priority Arrival Time Burst Time

P1 4 0 5

P2 3 1 4

P3 2 2 2

P4 1 4 1

Here we will assume that low numbers represent high priority.

1)​ At time = 0,
P1 arrives and starts executing since there is no other process in the
queue.​

2)​ At time = 1,
P2 arrives. P1 is yet to finish, but the priority of P2 is higher than that
of P1, it will take over the CPU and start executing. So, P1 is pushed
to the ready queue.


3)​ At time = 2,
P3 arrives and preempts the process P2 since it has a higher priority.
Since P2 is yet to complete, it will be pushed to the ready queue.



4)​ At time = 4,
P3 completes its execution. P4 arrives and starts executing.​ ​


5)​ At time = 5,
P4 finishes execution. Since P2 and P1 are waiting in the queue, P2
will be selected for execution since it has a higher priority and it will
execute till it completes.


6)​ At time = 6,
Only P1 is left in the queue, so it will continue its execution.

Final Gantt Chart:

Now let us calculate the Average waiting time for the above example:

Process Priority Arrival Time Burst Time Completion Turn Around Waiting
(ms) (ms) Time (ms) Time (ms) Time (ms)
P1 4 0 5 12 12 7
P2 3 1 4 8 7 3
P3 2 2 2 4 2 0
P4 1 4 1 5 1 0
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around time - Burst Time

Average Waiting Time = (7+ 3 + 0 + 0 )/4= 10/4 = 2.5ms

Advantages of Priority scheduling:


●​ Processes are executed according to priority hence we are able to
identify the relative importance of a process.

Disadvantages of Priority scheduling:


●​ Can lead to starvation of low-priority processes.
○​ A phenomenon that leads low-priority processes to wait for an
indefinite time as other higher-priority processes are being
executed before it.
○​ A solution to starvation is aging. Aging involves gradually
increasing the priority of processes that wait in the system for a
long time.
●​ If a heavily loaded computer system crashes then it loses all
unfinished low-priority processes.

4)​ Round-Robin Scheduling


●​ This scheduling algorithm is made for time-sharing systems.
●​ It is similar to FCFS scheduling, but it is preemptive, so CPU
can switch between processes.
●​ A small unit of time called time quanta or time slice is defined,
and every process is allowed to run for that particular time in a
preemptive way.
●​ Each process in the ready is executed for that fixed amount of
time called time quanta, if the process doesn’t terminate, then it
is moved to the end of the ready queue and there it waits for the
next turn to complete the execution until it terminates.
●​ If a process has burst time less than the time quanta, then it
voluntarily releases the CPU when it terminates, and the next
process proceeds.
Let’s go through an example to get more clarity about the algorithm,

Process Arrival Time Burst Time

P1 0 4

P2 2 2

P3 1 3

P4 4 1

Here we will assume time quanta = 2 units


1)​ At time = 0,
P1 arrives and starts executing since there is no other process in the
queue.​

2)​ At time = 1,
P3 arrives and is pushed to the ready queue, since P1 is executing.


3)​ At time = 2,
P2 arrives and is pushed to the ready queue. At this point, P1 has run
for the time slice of 2 units, now we need to preempt it, since it has
not completed its execution yet it will be pushed to the ready queue.
And P3 will start executing now.



4)​ At time = 4,
P4 arrives and is pushed to the ready queue. P3 has run for a period
of 2 units and yet has not completed its execution, so it moves to the
ready queue. Since the first process waiting in the ready queue is P2
now, it will be assigned to the CPU now.


5)​ At time = 6,
P2 had a burst time of 2 units so it finishes execution. P1 (remaining
burst time = 2 units) is executed next.


6)​ At time = 8,
P1 completes its execution. P4 waiting in the queue continues its
execution.


7)​ At time = 9,
P4 executes for 1 unit time and terminates (since it had a remaining
burst time of 1 unit). P3 waiting in the queue is assigned to the CPU
and it executes for 1 unit time and terminates (since it had a
remaining burst time of 1 unit).

Final Gantt Chart:


Now let us calculate the Average waiting time for the above example:

Process Arrival Time Burst Time Completion Turn Around Waiting Time
(ms) (ms) Time (ms) Time (ms) (ms)
P1 0 4 8 8 4
P2 2 2 6 4 2
P3 1 3 10 9 6
P4 4 1 9 5 4

Turn Around Time = Completion Time - Arrival Time


Waiting Time = Turn Around time - Burst Time

Average Waiting Time = (4+ 2 + 6 + 4 )/4= 16/4 = 4ms

Advantages of Round Robin scheduling:


●​ Ideal for time-sharing systems.
●​ All processes get an equal share of time hence fewer chances of
starvation.

Disadvantages of Round Robin scheduling:


●​ If the time slice is lower, it leads to higher context switching.
●​ If the time quantum is too large, RR scheduling converts into FCFS
policy.
●​ Performance heavily depends on time quantum.

5)​ Shortest Remaining Time First Scheduling


●​ Preemptive SJF is known as Shortest Remaining Time First
scheduling.
●​ The scheduling depends on the length of the next CPU burst of
a process, rather than its total length.
●​ In simple words, the process with the least remaining burst time
is selected to be executed on the CPU.

Let us understand the following example to get a better idea of the


algorithm:

Process Arrival Time Burst Time

P1 0 4

P2 1 5

P3 2 1

P4 3 2

1)​ At time = 0,
P1 arrives and starts executing since there is no other process in the
queue.​

2)​ At time = 1,
P2 arrives and is pushed to the ready queue, since at this point in
time, the remaining burst time of P1 is 3 units while that of P2 is 5
units.



3)​ At time = 2,
P3 arrives and preempts process P1 since the burst time of P3 is 1
unit which is lesser than the remaining burst time of P1 i.e. 2 units.
So P1 is pushed to the queue.



4)​ At time = 3,
P3 terminates. P4 arrives and starts executing since it has the
smallest burst time compared to other processes in the queue.


5)​ At time = 4,
P4 still has the lowest remaining burst time so it will continue
executing.



6)​ At time = 5,
P4 completes its execution. P1 waiting in the queue continues its
execution since it has a lesser remaining burst time compared to
other processes.


7)​ At time = 7,
P1 terminates. Only P2 is there in the queue, so it will continue its
execution till time = 12.

Final Gantt Chart:


Now let us calculate the Average waiting time for the above example:

Process Arrival Time Burst Time Completion Turn Around Waiting Time
(ms) (ms) Time (ms) Time (ms) (ms)
P1 0 4 7 7 3
P2 1 5 12 11 6
P3 2 1 3 1 0
P4 3 2 5 2 0

Turn Around Time = Completion Time - Arrival Time


Waiting Time = Turn Around time - Burst Time

Average Waiting Time = (3 + 6 + 0 + 0 )/4= 9/4 = 2.25ms

Advantages of SRTF scheduling:


●​ Faster than SJF since preemption is allowed.
Disadvantages of SRTF scheduling:
●​ Similar to SJF, it also leads to starvation of larger processes.
●​ Context switching leads to a wastage of CPU time.

6)​ Multilevel Queue Scheduling


●​ In this algorithm, the processes are divided into different groups
and each group follows a different scheduling algorithm.
●​ For example, a common division is made between foreground
(interactive) processes and background (batch) processes.
Since these two types of processes have different
response-time requirements and so may have different
scheduling needs.
●​ A multilevel queue scheduling algorithm partitions the ready
queue into several separate queues and each queue has its
own scheduling algorithm.
●​ Also, there must be scheduling among the queues, which is
commonly implemented as fixed-priority preemptive scheduling.

Let us consider the following multilevel queue algorithm with


four queues, listed in order of priority:

Advantages of Multilevel Queue scheduling:


●​ Different types of scheduling can be applied to different queues.

Disadvantages of Multilevel Queue scheduling:


●​ There might be a possibility that higher-level queues never get empty,
leading to starvation of lower-level processes.
●​ Inflexible in nature.

7)​ Multilevel Feedback Queue Scheduling


●​ This algorithm is similar to Multilevel Queue scheduling, the
only difference is that it allows a process to move between
queues.
●​ Here, the idea is to separate processes according to the
characteristics of their CPU bursts. If a process consumes too
much CPU time, it will be moved to a lower-priority queue.
●​ Also, that is waiting in a lower level for a long time is moved to a
higher level, which prevents starvation.

Let us go through the following example to get a better understanding:

Let us consider that Queue 1 and Queue 2 follow Round Robin scheduling
and Queue 3 follows FCFS.
●​ A process entering the ready queue is put in Queue 1. A process in
Queue 1 is given a time quantum of 4 ms. If it does not finish within
this time, it is moved to the tail of Queue 2.
●​ If Queue 1 is empty, the process at the head of Queue 2 is given a
quantum of 16 ms. If it does not complete, it is preempted and is put
into Queue 3.
●​ Processes in Queue 3 are run on an FCFS basis but are run only
when Queues 1 and 2 are empty.
●​ A process having burst time less than or equal to 4ms is given the
highest priority (because they are in Queue 1).
●​ Processes with burst time greater than 4ms but less than 20ms are
also served quickly.
●​ Long processes automatically sink to Queue 3 and are served in
FCFS order.

Advantages of Multilevel Feedback Queue scheduling:


●​ Through aging of processes it prevents starvation.
●​ It is more flexible.

Disadvantages of Multilevel Feedback Queue scheduling:


●​ It is the most complex algorithm.
●​ Defining the best scheduler requires some means by which to select
values for all the parameters.

Ch-6
Deadlocks

What is a deadlock?
●​ Deadlock is a situation in which some processes are blocked
because a process is holding a resource and also requesting some
resource that is already acquired by some other process.

●​ In a multiprogramming environment, processes compete with each


other for resources. If a process requests resources and it is
unavailable at that time then it enters waiting state. In some cases,
the process is never able to complete execution because the
resources it has requested are held by other waiting processes. This
situation is called deadlock.
Deadlock Characterization
(Necessary conditions for a deadlock to occur)

A deadlock situation can arise if the following four conditions hold


simultaneously in a system:

1)​ Mutual exclusion: It means that resources are non-sharable i.e. only
one process at a time can use the resource. Any other process can
access that resource only when it is released by the previous
process.
2)​ Hold and wait: A process must be holding at least one resource and
waiting to acquire other resources that are currently held by other
processes.
3)​ No preemption: Resources cannot be preempted i.e. a resource can
only be taken from a process when it voluntarily releases it.
4)​ Circular wait:
○​ A set of processes waiting for each other in a circular form.
○​ Let's say we have the following set {P0, P1, ..., Pn} of waiting
processes that exist such that P0 is waiting for a resource held
by P1, P1 is waiting for a resource held by P2, similarly Pn−1 is
waiting for a resource held by Pn, and Pn is waiting for a
resource held by P0.
○​ This leads to a circular dependency and the processes are
never able to execute.
Resource Allocation Graph (RAG)
●​ Deadlocks can be explained more properly using a graph called a
resource-allocation graph.
●​ This graph consists of a set of vertices V and a set of edges E.
Where the set of vertices V is divided into two types: PP = {P1, P2,
..., Pn}, the set consisting of all the active processes in the system,
and R = {R1, R2, ..., Rm}, the set consisting of all resource types in
the system.
●​ A directed edge from process Pi to resource Rj is denoted by Pi → Rj,
which means that process Pi has requested an instance of resource
type Rj and is currently waiting for that resource. This edge is called a
request edge.
●​ A directed edge from resource Rj to process Pi is denoted by Rj→ Pi, it
signifies that an instance of resource type Rj has been allocated to
process Pi. This edge is called an assignment edge.

●​ The system can have single instance resources like CPU and
multiple instance resources like registers. In RAG, we show the
multiple instance resources with multiple dots i.e. the number of
instances of that resource, for example, R3 in the above diagram has
two instances.
●​ In the above diagram, there are three types of resources and three
processes. Process P1 is requesting an instance of resource type R1.
Process P2 is holding an instance of resources R1 and R3 and
waiting for an instance of Resource R2. Process P3 is holding an
instance of resources R2 and R3.
●​ As per the definition of the resource-allocation graph, if the graph
contains no cycles, then no process in the system is deadlocked. If
the graph contains a cycle, then a deadlock may exist.

Methods to handle deadlocks


We can deal with deadlock in either of the following ways:

1)​ Deadlock ignorance: It means simply ignoring the deadlock. It is the


simplest approach and is commonly used in systems where deadlock
occurs very rarely. In systems where a deadlock may occur once in
five years, most engineers would not be willing to compensate for
performance or convenience to eliminate deadlocks.

2)​ Deadlock prevention: It provides a set of methods to ensure that at


least one of the four necessary conditions(that were mentioned
above) cannot hold. By ensuring this we can prevent a deadlock. Let
us have a look at each condition:
●​ Mutual Exclusion:
○​ It means that resources are non-sharable and can be
used by one process at a time which causes a deadlock
situation.
○​ If we violate mutual exclusion, that is by making the
resources sharable then deadlock can be prevented.
An example of a shareable resource is a read-only file.
○​ Practically we cannot eliminate mutual exclusion,
because some resources are fundamentally non-sharable
for example, printers.

●​ Hold and Wait:


○​ To eliminate the hold and wait situation we must ensure
that a process requesting some resource should not be
holding any other resource.
○​ One way to implement the above could be allocating all
the required resources to a process before it begins
execution.
○​ An alternative mechanism could be allowing a process to
request resources only when it has none. In simple
words, a process may request some resources and use
them, but before requesting additional resources it must
release all the resources that are currently allocated to it.
○​ Both approaches have some disadvantages:
■​ Low resource utilization since resources may be
allocated but unused for a long period.
■​ Starvation of a process, because at least one of the
required resources is allocated to another process
leading to an indefinite wait time.

●​ No Preemption
○​ To violate no-preemption we can use the following
approach. If a process is holding some resources and
requesting some new resources which cannot be
immediately allocated to it, then all the resources
currently held by the process are preempted.
○​ A process will restart only when it is allocated the new
resources and recovers its preempted resources as well.
○​ This approach is not efficient since it cannot be applied to
resources whose state cannot be saved and restored
later, for example, printers and tape drivers.

●​ Circular Wait
○​ To ensure that the circular wait condition doesn’t hold we
need to assign an order to all the resource types and
each process can request resources only in increasing
order.
○​ Let us understand with an example,
Resource type Ordering
Printer 2
Tape driver 3
Register 4

Now, let's say a process P1 requests for the printer, so


the printer is allocated to P1, then it requests for register,
so we allocate one instance of registers to P1 since it
comes after printer in ordering. Then let's say another
process P2 requests for registers, we allocate an instance
of registers to it, then it requests for the printer, which is
above registers in order so it cannot be assigned to P2.
Hence, circular wait never occurs.

○​ A process requesting any resource of type Rj then it must


release all the resources Ri allocated to it, where the
order number of (Ri) ≥ order of number (Rj).
○​ Violating circular wait is the only practical approach for
deadlock prevention.

3)​ Deadlock Avoidance: In the deadlock avoidance model we require


the process to declare the maximum number of resources of each
type that it may need beforehand. And with this given information, it
will be possible to construct an algorithm that ensures that the system
never enters a deadlock state.

●​ Safe and Unsafe states


○​ Safe state: A state is called a safe state if the system can
allocate resources to each process in some order that it
avoids deadlock. A system is in a safe state only if there
exists a safe sequence.

○​ Unsafe state: The opposite of a safe state is called an


unsafe state. A deadlock state is an unsafe state. Not all
unsafe states are deadlocks but an unsafe state may lead
to a deadlock.

Here are the deadlock avoidance algorithms:


●​ Banker’s Algorithm
○​ A scheduling algorithm to avoid deadlock is known as the
banker’s algorithm.
○​ It helps us in predetermining the safe sequence.
○​ It simulates the allocation of resources to processes
according to the maximum resources present.
○​ It is modeled on the way a bank might deal with a group
of customers to whom it has granted lines of credit.

Let us understand the algorithm with the help of an example:


Here is a table that represents four processes with the maximum
requirement of resources, here we have three types of resources A, B,
and C:

Process A B C

P1 2 1 4

P2 3 2 2

P3 5 3 3

P4 4 2 3
Then we have another table that shows the number of resources
currently allocated to each process:

Process A B C

P1 1 0 0

P2 2 1 2

P3 3 0 1

P4 2 1 0

The following table gives the total number of resources present on the
system:

A B C

10 6 5

The following table gives the currently available resources (that is, Total
resources - Resources currently allocated to each process):

A B C

2 4 2

At last, we have the table representing the remaining need of each


process (Maximum requirement - currently allocated):

Process A B C

P1 1 1 4
Process A B C

P2 1 1 0

P3 2 3 2

P4 2 1 3

○​ From the above tables we can infer that the resources quantity
of A, B, and C available are 2, 4, and 2 respectively.
○​ If we scan through the last table that contains the remaining
need we can see that the remaining need of process P1(A, B, C
→ 1, 1, 4) cannot be fulfilled as available resources are (A, B,
C → 2, 4, 2), so we check for P2 and we see that need of P2
can be fulfilled, so we allocate the remaining resources to P2.
○​ After P2 completes execution now the available resources will
be: currently available resources (A, B, C → 2, 4, 2) +
resources released by P2 i.e. the resources initially allocated to
P2 (A, B, C → 2, 1, 2).
So now we have remaining resources= (A, B, C → 4, 5, 4).
○​ In a similar way now we will scan the remaining table, the
remaining need of P3 is (A, B, C → 2, 3, 2) and the resources
we currently have are (A, B, C → 4, 5, 4). So, P3 can execute.
○​ After P3 executes we are now left with the following resources:
Currently available (A, B, C → 4, 5, 4) + resources released by
P3(A, B, C →3, 0, 1) = (A, B, C →7, 5, 5).
○​ Check for P4 next, need of P4 is (A, B, C →2, 1, 3) and we
currently have (A, B, C →7, 5, 5). So P4 can execute next.
○​ Once P4 terminates we are left with these resources we are left
with: Currently available (A, B, C → 7, 5, 5) + resources
released by P3(A, B, C →2, 1, 0) = (A, B, C →9, 6, 5).
○​ Since the above resources are sufficient to fulfill P1’s need (A,
B, C →1, 1, 4), P1 executes next.
○​ At last we are now left with the following resources:
Currently available (A, B, C → 9, 6, 5) + resources released by P1(A,
B, C →1, 0, 0) = (A, B, C →10, 6, 5), which are actually equal to the
total number of resources our system had.
○​ So, the safe sequence we get is (P2, P3, P4, P1). If we execute
the process in the following manner then deadlock will not
occur.
○​ There can be multiple safe sequences for a set of processes.

●​ Resource-Allocation-Graph Algorithm
○​ We discussed RAG above in-depth and similarly by
creating Resource Allocation Graph we can detect a
deadlock.
○​ We can check for a safe state using a cycle-detection
algorithm.
■​ If there is no cycle in RAG, then the allocation of
resources will leave the system in a safe state.
■​ If a cycle is found, then the allocation will put the
system in an unsafe state.
■​ A cycle indicates that the system is in an unsafe
state.

4)​ Deadlock Detection and Recovery from Deadlock


If the system is unable to implement any of the above deadlock
prevention and avoidance algorithms, then a deadlock situation
occurs. In such cases, the system will employ:
●​ An algorithm that detects whether a deadlock has
occurred.
●​ An algorithm to recover from it.
Deadlock Detection
●​ Single instance resource type: For resource types that have
only a single instance we can use a resource allocation graph
to detect a deadlock. If cycle is present in RAG then definitely
there is a deadlock.
●​ Multiple instance resource type: In this case presence of a
cycle in RAG does not confirm a deadlock, so we use some
safety algorithm that is similar to the banker's algorithm to
detect a deadlock.
Recovery from a deadlock
When the detection algorithm confirms the existence of a deadlock
then we can use the following recovery methods:
●​ Process Termination
There are two methods to eliminate deadlocks by aborting a
process:
○​ End all the deadlocked processes. This is an inefficient
approach as all the computations are discarded and will
be recomputed once the processes start again.
○​ End one process at a time until the deadlock is
eliminated. This is a better algorithm but it requires a
detection algorithm to be executed at every step.

●​ Resource Preemption
Here we preempt the resources of a process by giving the
resources to other processes until the deadlock cycle breaks.
In this case, these three issues need to be addressed:

○​ Selecting a victim: It means selecting which resources of


processes to be preempted. Cost factors which are the
number of resources held by the process and the time it
has executed on the CPU are taken into consideration.
○​ Rollback: It means that the preempted processes must
roll back to a safe state. Since it is difficult to determine
the safe state, the simplest method is total rollback i.e.
restart the process.
○​ Starvation: How can we ensure that the processes that
are preempted do not starve? Also starvation can occur if
the same process is victim every time. So to avoid this
situation we can include the number of rollbacks in the
cost factor while selecting the victim.
Module 3: Memory Management
Memory: Memory is defined as the place where we can store data and the
CPU can process it.
Memory Management: Memory management is defined as the
management of memory in such a way that all the required tasks have
equal opportunity to get memory for processing.

The operating system is responsible for the following activities in following


activities in connection with memory management:
●​ Keeping track of which parts of memory are currently being used and
who is using them.
●​ Deciding which processes (or parts of processes) and data to move
into and out of the memory.
●​ Allocating and deallocating memory space as needed.

Now, before diving into much, kindly try to understand what the OS has to
manage in memory; basically OS deals with memory address space so that
it can manage the task accordingly in address spaces.

Now, address spaces are mainly of two types:


1.​ Physical Address Space.
2.​ Logical Address Space.

1.​ Physical Address Space: Physical Address Space is defined as the


address seen by the memory unit i.e. the one loaded into the
memory-address register of the memory - is commonly referred to as
a Physical Address.
●​ The set of all physical addresses corresponding to these logical
addresses is Physical address space.
●​ Address generated into the memory-address register of
physical memory.
●​ Users can never access the physical address of the program.
●​ Range: (R+0) to (R+max) for base value R.
●​ Set of the physical addresses corresponding to logical
addresses is commonly called Physical Address Space.
●​ Physical addresses can be accessed by a user indirectly but
not directly.
●​ It is computed by the memory management unit (MMU).

2.​ Logical Address Space: Logical Address Space is defined as the


address generated by the CPU commonly referred to as a Logical
Address.
●​ Sometimes, the logical address, and virtual address are used
interchangeably.
●​ The set of all the logical addresses generated by the program is
Logical address space.
●​ Range: 0 to the max.
●​ Users can access the logical address of a process.
●​ It is basically the address of instructions or data used by a
process.
●​ Logical Address does not exist physically, hence also Virtual
Address.

Swapping:
●​ As the name suggests, it does swapping of processes, it is that
simple.
●​ Now, carefully observe the following figure:
●​ Definition: A process can be swapped temporarily out of memory to
a backing store and then brought back into memory to continue
execution.
●​ What is happening basically here is that,
Let’s suppose process P1 wants I/O so in that set of time interval OS
did the process of swapping, it swaps out the P1 and take it into the
secondary memory and with the help of the dispatcher next process
P2 which is in the ready queue comes for execution and when P1
comes back then the P2 swaps out and P1 swaps in.
●​ If you remember the previous concept or whenever you hear about
that a process goes here and there then you must remember an
important concept i.e. Context Switching.
●​ So, note that context switch time in the case of swapping is fairly
high.

Allocation methods on memory:


As we know, a process is executed in memory to increase the output,
which means for increasing the performance if more processes execute in
memory, it saves time and as a result, the task is efficiently and effectively
completed.
So, there are two methods of memory allocation are:
1.​ Contiguous Memory Allocation.
2.​ Non-Contiguous Memory Allocation.

1.​ Contiguous Memory Allocation:


●​ In this, each process is contained in a single section of memory which
is contiguous to the section containing the other process.
●​ In other words, each process is contained in a single contiguous
block.
●​ So, to allocate the process in memory, there is the simplest method
which is to divide the memory into several partitions.
●​ These partitions can be fixed or multiple.
●​ Each partition may contain exactly one process. Thus, the degree of
multiprogramming is bound by the number of partitions.
●​ Fixed Partitioning happens when the memory is divided into the
partitioning of a particular size in a contiguous manner. In this
situation, a fixed partition of memory is available when a new process
comes for execution.
○​ Advantages: a) Easy to implement for the developer.
​ ​ ​ ​ b) Little OS overhead.
○​ Disadvantages: a) Internal Fragmentation
​ ​ ​ ​ ​ b) External Fragmentation
​ c) If the size of the process is larger than the
size of maximum sized partitions then the process
cannot be loaded into memory because the partition
is of fixed size then there is difficulty to fit the
process of larger size for further execution. Here,
the no of partitions is equal to the no of processes
that executes hence less degree of
multiprogramming.
●​ Dynamic Partition happens when the memory is freely available for
the processes. As the process comes, the OS makes a partition
according to the size of the partition which solves the one problem
that happens in a fixed partition.
○​ Advantages: a) No Internal Fragmentation.
​ ​ ​ ​ b) Partitions creation is according to the no of
processes so better degree of multiprogramming.
○​ Disadvantages: a) External Fragmentation.

Free Space:
●​ Free space is created in two situations, when the size of the process
is less than the size of partitions then there is a free space leftover
another case is when a process executes and immediately goes out
from the memory then after leaving that process leaves free space in
memory.
●​ Till this point, you fairly have an idea of free space.
●​ OS manages the free space with the help of a free list (which itself is
a type of linked list).

Fragmentation:
●​ It is defined as the process in which when the process is loaded and
removed from memory, it creates a small free space( commonly
known as Holes).
●​ To achieve a degree of multiprogramming, we must reduce the
number of holes.
●​ There are two types of fragmentation which if you understand first
then you can easily understand and apply in further topics as well:

Internal Fragmentation External Fragmentation


Fixed Partition: Fixed Fragmentation:
There is a fixed partition of memory In a fixed partition of memory, there
and when the process is coming for are many free spaces that exist due
execution and the size of the to internal fragmentation which, if
process being smaller than the size combined, seems to be an available
of the partition then there is free space for a new process.
space leftover. Here, in this case, if But if that new process is not able
this free space created in each to load itself into that because the
process then many free spaces are available space is in a different
created in memory and hence lead partition.
to internal fragmentation.

Dynamic Partition: There is no Dynamic Partition: If there are


internal fragmentation in the case of processes that have low execution
dynamic partition because the time then they go out immediately
partition of memory is created and then there is a free space
according to the size of the leftover which if combined seems to
process. be an available space for a new
process but here there are chances
that a new process is able to fit in it
or not.

So to solve the problem of fragmentation, we have a process called


Defragmentation or Compaction.

Defragmentation: It is defined as the process in which the free space


available in different places of memory is combined and all the processes
that are in the memory shifted to other places and the free space combined
itself becomes large to accommodate the coming new process.
●​ It is also known as Compaction.
●​ Remember, Compaction is a time-consuming process that decreases
the efficiency of the CPU.
Concept:
Now, try to understand that the memory blocks available comprise a set of
holes of various sizes scattered throughout the memory. When a process
arrives and needs memory, the system searches the set for a hole that is
large enough for this process. If the hole is too large, it is split into two
parts. One part is allocated to the arriving process and the other is returned
to the set of holes. When a process terminates, it releases its block of
memory which is then placed back in the set of holes. If the new hole is
adjacent to other holes, these adjacent holes are merged to form one larger
hole. At this point, the system may need to check whether there are
processes waiting for memory and whether this newly freed and
recombined memory could satisfy the demands of any of these waiting
processes.
This procedure is a particular instance of the general dynamic storage
allocation problem, which concerns how to satisfy a request of size n from
a list of free holes. There are many solutions to this problem are:
First-Fit, Best Fit, and Worst Fit.

First Fit Best Fit Worst Fit


●​ In this, the first ●​ Allocate the ●​ Allocate the
ever big hole that smallest hole that largest hole.
is big enough to is big enough to ●​ In this, the entire
accommodate accommodate list is searched
the coming the coming first until and
process. process. unless the list is
●​ Searching can be ●​ In this, the entire sorted by size.
started either at list is searched ●​ This strategy
the beginning of first until and produces the
the starting of the unless the list is largest leftover
free list or at the ordered by size. hole.
location where ●​ This strategy ●​ It is slow.
the previous first produces the ●​ Lesser External
fir search ended. smallest leftover Fragmentation.
●​ Storage of hole.
pointer required. ●​ Very low internal
●​ It is fast, simple, fragmentation.
and easy to ●​ It is slow.
implement. ●​ It has high time
●​ It has low time complexity.
complexity. ●​ Problem: Major
External
Fragmentation.

Paging:
●​ Paging is the memory management technique that is used under the
non-contiguous memory allocation.
●​ This offers the physical address space of a process to be
non-contiguous.
●​ It avoids external fragmentation and it needs a process called
compaction.
●​ It is implemented through the cooperation between the operating
system and the computer hardware.
●​ Paging involves:
○​ Breaking of physical memory into fixed-sized blocks called
frames; and
○​ Breaking of logical memory into blocks of the same size called
pages.
●​ Page Size = Frame Size (Always)
●​ The logical address is generated by the CPU commonly represented
in bits while the logical address space is a set of all logical addresses
commonly represented in bytes or words.
●​ The Physical address actually exists on a memory unit commonly
represented in bits while the physical address space is a set of all
physical addresses corresponding to logical addresses commonly
represented in bytes or words.
●​ The Logical address generated by the 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 a
particular word in a page or page size of Logical Address Space
or word number of a page or page offset.
●​ The Physical Address available in the memory unit 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 a
particular word in a frame or frame size of Physical Address
Space or word number of a frame or frame offset.
●​ The mapping of logical to physical address is done by the memory
management unit (MMU).
●​ MMU is a hardware and the mapping process called Paging.
●​ Remember:
○​ 1K = 210, 1M = 220, 1G = 230 (okay!)
○​ If Logical Address is in bits then Logical Address Space is in
words.
■​ To convert logical/physical address to logical/physical
address space, take the logical/physical address to the
power of 2 like Logical/Physical Address = k bits then
Logical/Physical Address Space = 2k words or bytes.
■​ To convert logical/physical address space to
logical/physical address, take the logical/physical address
space with (log.2) like Logical/Physical Address = 2k words
then Logical/Physical Address = log2 2k = k (log22) = k.
●​ Let us consider an example:
○​ Logical Address = 11 bits then Logical Address Space = 211 =
210 * 2 = 2K bytes. (210 = 1K).
○​ Physical Address = 12 bits then Physical Address Space = 212 =
210 * 22 = 4K bytes. (210 = 1K).
○​ Page Size = Frame Size = 1K.
●​ Now look into the following figure:

●​ Now, calculate the number of pages and frames.


○​ Number of Pages = Logical Address Space / Page Size =
2K/1K = 2.
○​ Number of Frames = Physical Address Space / Frame Size =
4K/1K = 4.

Dedicated registers are present and used to implement the page table in
hardware. However, using a register for the page table is only useful if the
page table is tiny. So, if in any case, the page table is large, then we use
TLB (Translation Look-Aside Buffer), a particular device that is fast
hardware that acts as a cache.
●​ The TLB is a high-speed, associative memory.
●​ It is used to speed up the paging process.
●​ It has a key-value pair.
●​ When the memory is accessed, the whole TLB process and if any key
is found then the value associated with it is returned and if not found
then it looks in the Page Table and stores that in TLB for future use
and simultaneously goes to the physical address.

●​ If the page number is found in TLB called TLB hit and if not found
called TLB Miss.
●​ TLB also stores ASIDs (Address Space Identifier) in each TLB entry.
An ASID uniquely identifies each process and is used to provide
address-space protection for that process.

Important to Note:
●​ Every process has a different page table because the logical address
space which is mapped on physical address space is different.
●​ Page Table is stored in memory.
●​ TLB is used to speed up the paging process.
●​ TLB will have multiple entries and even it can have entries of different
processes. To handle this, we can use ASIDs.
Advantages of Paging:
1.​ Paging reduces external fragmentation.
2.​ It is easy to implement and assumed as an efficient memory
management technique.
3.​ Due to the equal size of the pages and frames, swapping becomes
very easy.
Disadvantages of Paging:
1.​ It suffers from internal fragmentation.
2.​ It is slow, and it adds overhead due to too many memory references.
3.​ Page Table requires extra memory space, so may not be good for a
system having small size RAM.

Segmentation:
●​ Segmentation is defined as a memory management technique.
●​ A process is divided into chunks that are not necessarily all of the
same sizes called Segments.
●​ Segmentation gives the user’s view of the process means here the
user’s view is mapped to physical memory.
●​ Here we do variable partitioning of logical address space while on the
other hand in paging we do fixed partitioning of address space.
●​ Like in Paging, we have a page table, in segmentation, we have a
segment table.
●​ Segment Table stores the information about all such segments of a
process.
●​ Segment Table has two entries i.e. Segment Base and Segment
Limit which means it is a two-dimensional table mapped into
one-dimensional physical address.
○​ Segment Base: It contains the starting physical address where
the segments reside in memory.
○​ Segment Limit: It specifies the length of the segment.
●​ Logical Address generated by the CPU is divided into:
○​ Segment Number (s): Number of bits required to represent the
segment.
○​ Segment Offset (d): Number of bits required to represent the
size of the segment.
●​ MMU translates the two-dimensional logical address to
one-dimensional physical address.
Advantages of Segmentation:
●​ No internal fragmentation.
●​ Segment Table consumes less space as compared to page table in
paging.
●​ No need of Compaction in case of Segmentation while Paging needs
compaction.
Disadvantages of Segmentation:
●​ External Fragmentation.
●​ As the size of the segment is different it is not good at the time of
swapping.

Virtual Memory:
●​ Definition: Virtual Memory is a technique that allows the execution of
processes that are not completely in memory.
●​ It frees programmers from the concerns of memory storage
limitations.
●​ It is not easy to implement.
●​ It allows processes to share files easily and to implement the shared
memory.
●​ It is an efficient mechanism for process creation.
●​ It is a technique that allows the execution of processes that are not
completely in the memory.
●​ It provides an illusion to the user that they have a very big main
memory and you know what you will find interesting that this is done
with the help of treating the part of secondary memory as the main
memory and this part of secondary memory, commonly known as the
Swap Space.
●​ You can remember it as: RAM + Swap Area === Virtual Memory.
●​ Sometimes, the size of programs can be larger than the size of
physical memory.
●​ As we know, it is required that instructions must be in physical
memory to be executed. You must be wondering that, in many cases,
the entire memory is not needed at the same time. So, we want the
ability to execute a program that is only half in memory that would
give many benefits.
○​ The size of the program would no longer be constrained by the
size of available physical memory.
○​ As, more programs would be able to take memory for their
execution means the degree of multiprogramming increases.
○​ The concept of virtual memory benefits the user and the
system.
○​ Programmers take much more advantage of this scheme as
they do not bother about the memory limitations.
●​ The concept of Swapping helps you to understand more about this
topic.
●​ Basically, the phenomenon swaps out the process from the main
memory to secondary memory and swaps in the process from
secondary memory to main memory for further execution and
vice-versa, all this comes under the task called Swapping.
●​ The area of secondary memory which is used for swapping is called
Swap Space.
●​ But, here the process of swap in-and-out works with pages (page
means a division of process).

Demand Paging:
●​ It is a popular method of Virtual Memory Management.
●​ In simple terms, Demand Paging refers to a phenomenon of keeping
all the pages of a process in the secondary memory and loading
one-by-one as the demand of any particular page occurs.
●​ Page Fault refers to a situation when any page is not in main
memory and a demand has to be made in order for delivery of that
page from secondary memory to main memory for execution.
●​ And, when demand or page fault is made, that page is made
available to main memory with the help of a process called Swapping.
●​ Actually to identify which pages of a process will be replaced, various
Page Replacement Algorithms will be used.
●​ Remember, Lazy Swapper is used to swap the page of a process
and it makes the page available to the main memory as soon as the
demand or page fault occurs.
●​ Note: Here, we use the term, Swapping in the context of a page of a
process, not a whole process.

Pure Demand Paging:


●​ Pure Demand Paging refers to a situation of keeping all the pages of
a process in the secondary memory and loading one-by-one as the
demand of any particular page occurs.
Even, the first page of a process will load into the main memory when
its demand or page fault occurs.
●​ OS sets an instruction pointer to the first instruction of a process,
which is not in the memory. The process immediately faults the page
and the page is made available to the main memory.

Page Replacement Algorithms:


●​ These algorithms are used to identify which page is further needed
for execution so that it will be replaced with a less used page(present
in physical memory).
●​ Some pages are swapped out which are less needed and in return,
some are swapped in which are more important for further execution.
●​ The main aim of these page replacement algorithms is to have
minimum page faults.
●​ We must solve two major problems to implement demand paging that
we must develop:
○​ A Frame Allocation Algorithm.
○​ A Page Replacement Algorithm.
That is, if we have multiple processes in memory, we must decide
how many frames to allocate to each process, and when page
replacement is required, we must select the frames that are to be
replaced.
●​ Types of Page Replacement Algorithms:
○​ 1. FIFO Page Replacement:
■​ This is one of the easily implementable replacement
algorithms.
■​ This is the most simplest algorithm.
■​ A queue is maintained which maintained the pages in
memory and whenever a page needed to be replaced, the
page in the front of the queue is removed.

○​ 2. Optimal Page Replacement:


■​ It is one of the ideal page replacement algorithms.
■​ It seems to be the best algorithm.
■​ However, it seems to be feasible theoretically but it is
impossible and very difficult to implement.
■​ It has the lowest page fault rate among any algorithm.
■​ In this, OS needs to identify a page which will never be
referenced in the future. So, replacement becomes easy
with a new page.

○​ 3. LRU Page Replacement:


■​ It is one most used and widely popular page replacement
algorithms.
■​ It is feasible to implement and have an important role in
this category.
■​ In this, OS needs to identify the pages on the basis of
approximation of till now point means till now how many
pages are most used and how many pages are least
used?
■​ With this approximation, it is easy to identify that pages
will be replaced with new page.
■​ It can be implemented with the help of the following:
●​ Counters:
○​ In this, OS associates the time counter with
each page table entry and,
○​ Replace the page with the smallest time
value.
●​ Stack:
○​ A Stack is maintained which takes page
number.
○​ Whenever a page has to be replaced, OS will
put that page on top of the stack because that
page might be useful for further execution.

NOTE: Belady’s Anomaly is an anomaly which is something you will


experience when FIFO Page replacement shows the result on a large scale
(means while in execution).
Belady’s Anomaly:
●​ This anomaly arises when there is an increasing number of
page frames resulting in an increase in the number of page
faults.
●​ But this anomaly occurs sometimes in FIFO.
●​ While OPR and LRU shows that the number of page faults will
be reduced if we increase the number of frames.

Thrashing:
●​ Thrashing is defined as a situation when the system is giving its
precious time for servicing the page fault(means in swapping) rather
than giving its time for the execution of pages.
●​ You know better that the process of Swapping is a costly process in
terms of time.
●​ In simple terms, Thrashing is a high paging activity that means the
system gives its time for generating the page fault again and again.
●​ A system is in Thrashing when it spends more time servicing the
page faults than executing the page of process.
●​ Kindly, carefully observe the following graph:
Causes of Thrashing:
1.​ Initially, there is low CPU utilization, so the system tries to increase
the degree of multiprogramming so that it gives better performance.
2.​ Now, we have more processes than the available number of frames
in memory. Allocation of a limited amount of frames to each process.
3.​ When any higher priority process arrives in memory, and if the frame
is not freely available at that time then the other process that
occupied the frame which resides in the frame will move to secondary
memory and this free frame is now allocated to a newly arrived higher
priority process.
4.​ In simple terms, as memory is full then the process starts to spend a
lot of time for the required pages to be swapped in; again, CPU
utilization becomes low because more of the processes are waiting
for pages.

Techniques to Handle Thrashing: Following are the techniques for


handling the thrashing are:
1.​ Working Set Model:
●​ This model is based on the 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.
2.​ Page Fault Tolerance:
●​ Thrashing has a high page fault rate.
●​ We want to control the page fault rate.
●​ When it is too high, the process needs more frames. Conversely, if
the page-fault rate is too low, then the process may have too many
frames.
●​ We establish upper and lower bounds on the desired page fault rate.
●​ If the page fault rate exceeds the upper limit, allocate the process
another frame, if the page fault falls below the lower limit, remove a
frame from the process.
●​ By controlling the page fault rate, thrashing can be prevented.
●​ Actually, in this OS tries to be in the middle of the upper and lower
bound so that there will be no barrier in the execution of a page of a
process.

You might also like