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

Operating System Imp Notes

The document discusses operating systems and their functions. It defines an operating system as software that acts as an intermediary between computer hardware and application programs. Major functions of operating systems include memory management, processor management, device management, running software applications, data management, and providing a user interface. Common types of operating systems discussed are batch operating systems, multiprogramming operating systems, and multiprocessing operating systems.

Uploaded by

Sadhna Mall
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Operating System Imp Notes

The document discusses operating systems and their functions. It defines an operating system as software that acts as an intermediary between computer hardware and application programs. Major functions of operating systems include memory management, processor management, device management, running software applications, data management, and providing a user interface. Common types of operating systems discussed are batch operating systems, multiprogramming operating systems, and multiprocessing operating systems.

Uploaded by

Sadhna Mall
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 191

OPERATING SYSTEM

Operating System

As the name suggests, an operating system is a type of


software without which you cannot operate or run a computer.
It acts as an intermediary or translation system between
computer hardware and application programs installed on the
computer. In other words, you cannot directly use computer
programs with computer hardware without having a medium to
establish a connection between them.

Besides this, it is also an intermediary between the computer


user and the computer hardware as it provides a standard user
interface that you see on your computer screen after you
switch on your computer. For example, the Windows and the
Mac OS are also operating systems that provide a graphical
interface with icons and pictures to enable users to access
multiple files and applications simultaneously.

So, although the operating system is itself a program or


software, it allows users to run other programs or applications
on the system. We can say that is works behind the scenes to
run your computer.

Major Functions of Operating System:


o Memory management: It manages both the primary and
secondary memory such as RAM, ROM, hard disk, pen
drive, etc. It checks and decides the allocations and
deallocation of memory space to different processes.
When a user interacts with a system, the CPU is supposed
to read or write operations, in this case, OS decides the
amount of memory to be allocated for loading the
program instructions and data into RAM. After this
program is terminated, the memory area is again free and
is ready to be allocated to other programs by the OS.
o Processor Management: It facilitates processor
management, where it decides the order for the processes
to access the processor as well as decides the processing
time to be allocated for each process. Besides this, it
monitors the status of processes, frees the processor
when a process is executed then allocates it to a new
process.
o Device/ hardware management: The operating system also
contains drivers to manage devices. A driver is a type of
translation software that allows the operating system to
communicate with devices, and there are different drivers
for different devices as each device speaks a different
language.

o Run software applications: It offers the environment to run


or use software applications developed to perform specific
tasks, for example, Ms Word, Ms Excel, Photoshop, etc.
o Data management: It helps in data management by
offering and displaying directories for data management.
You can view and manipulate files, folders, e.g., you can
move, copy, name, or rename, delete a file or a folder.
o Evaluates the system's health: It gives us an idea about the
performance of the hardware of the system. For example,
you can see how busy the CPU is, how fast the data is
retrieved from the hard disk, etc.
o Provides user interface: It acts as an interface between the
user and the hardware. It can be a GUI where you can see
and click elements on the screen to perform various tasks.
It enables you to communicate with the computer even
without knowing the computer's language.
o I/O management: It manages the input output devices and
makes the I/O process smooth and effective. For example,
it receives the input provided by the user through an input
device and stores it in the main memory. Then it directs
the CPU to process this input and accordingly provides the
output through an output device such as a monitor.
o Security: It has a security module to protect the data or
information stored in the memories of the computer
against malware and unauthorized access. Thus, it not
only manages your data but also helps to protect it.
o Time Management: It helps CPU in time management. The
Kernel OS keeps checking the frequency of processes that
requests CPU time. When two or more processes that are
equally important compete for the CPU time, then the CPU
time is sliced into segments and allocated to these
processes in a round-robin fashion to prevent a single
process from monopolizing the CPU.
o Deadlock Prevention: Sometimes a resource that is
supposed to be shared by two or more processes is held
by one process due to which the resource cannot
continue. This situation is known as deadlock. The OS
does not let this situation arise by carefully distributing
the resources among the different processes.
o Interrupt Handling: OS also responds to interrupts, which
are signals generated by a program or a device to seek
the attention of the CPU. The OS checks the priority of the
interrupt, and if it is more important than the currently
running process, it stops the execution of the current
process and preserves this state of CPU then executes the
requested process. Thereafter the CPU returns to the
same state where it was stopped.

Operating System can be defined as an interface between user


and the hardware. It provides an environment to the user so
that, the user can perform its task in convenient and efficient
way.

The Operating System Tutorial is divided into various parts


based on its functions such as Process Management, Process
Synchronization, Deadlocks and File Management.

Operating System Definition and Function

In the Computer System (comprises of Hardware and


software), Hardware can only understand machine code (in the
form of 0 and 1) which doesn't make any sense to a naive
user.

We need a system which can act as an intermediary and


manage all the processes and resources present in the system.
An Operating System can be defined as an interface between user
and hardware. It is responsible for the execution of all the
processes, Resource Allocation, CPU management, File
Management and many other tasks.

The purpose of an operating system is to provide an


environment in which a user can execute programs in
convenient and efficient manner.

Structure of a Computer System

A Computer System consists of:

o Users (people who are using the computer)


o Application Programs (Compilers, Databases, Games,
Video player, Browsers, etc.)
o System Programs (Shells, Editors, Compilers, etc.)
o Operating System ( A special program which acts as an
interface between user and hardware )
o Hardware ( CPU, Disks, Memory, etc)
What does an Operating system do?
1. Process Management
2. Process Synchronization
3. Memory Management
4. CPU Scheduling
5. File Management
6. Security

Types of Operating Systems

An operating system is a well-organized collection of programs


that manages the computer hardware. It is a type of system
software that is responsible for the smooth functioning of the
computer system.
Batch Operating System

In the 1970s, Batch processing was very popular. In this


technique, similar types of jobs were batched together and
executed in time. People were used to having a single
computer which was called a mainframe.

In Batch operating system, access is given to more than one


person; they submit their respective jobs to the system for the
execution.

The system put all of the jobs in a queue on the basis of first
come first serve and then executes the jobs one by one. The
users collect their respective output when all the jobs get
executed.
The purpose of this operating system was mainly to transfer
control from one job to another as soon as the job was
completed. It contained a small set of programs called the
resident monitor that always resided in one part of the main
memory. The remaining part is used for servicing jobs.
Advantages of Batch OS
o The use of a resident monitor improves computer
efficiency as it eliminates CPU time between two jobs.

Disadvantages of Batch OS

1. Starvation

Batch processing suffers from starvation.

For Example:
There are five jobs J1, J2, J3, J4, and J5, present in the batch.
If the execution time of J1 is very high, then the other four
jobs will never be executed, or they will have to wait for a very
long time. Hence the other processes get starved.

2. Not Interactive

Batch Processing is not suitable for jobs that are dependent on


the user's input. If a job requires the input of two numbers
from the console, then it will never get it in the batch
processing scenario since the user is not present at the time of
execution.

Multiprogramming Operating System

Multiprogramming is an extension to batch processing where


the CPU is always kept busy. Each process needs two types of
system time: CPU time and IO time.

In a multiprogramming environment, when a process does its


I/O, The CPU can start the execution of other processes.
Therefore, multiprogramming improves the efficiency of the
system.
Advantages of Multiprogramming OS

o Throughout the system, it increased as the CPU always


had one program to execute.
o Response time can also be reduced.

Disadvantages of Multiprogramming OS
o Multiprogramming systems provide an environment in
which various systems resources are used efficiently, but
they do not provide any user interaction with the
computer system.

Multiprocessing Operating System

In Multiprocessing, Parallel computing is achieved. There are


more than one processors present in the system which can
execute more than one process at the same time. This will
increase the throughput of the system.
In Multiprocessing, Parallel computing is achieved. More than
one processor present in the system can execute more than
one process simultaneously, which will increase the throughput
of the system.

Advantages of Multiprocessing operating system:

o Increased reliability: Due to the multiprocessing system,


processing tasks can be distributed among several
processors. This increases reliability as if one processor
fails, the task can be given to another processor for
completion.
o Increased throughout: As several processors increase, more
work can be done in less.

Disadvantages of Multiprocessing operating System

o Multiprocessing operating system is more complex and


sophisticated as it takes care of multiple CPUs
simultaneously.

Multitasking Operating System

The multitasking operating system is a logical extension of a


multiprogramming system that enables multiple programs
simultaneously. It allows a user to perform more than one
computer task at the same time.
Advantages of Multitasking operating system
o This operating system is more suited to supporting
multiple users simultaneously.
o The multitasking operating systems have well-defined
memory management.

Disadvantages of Multitasking operating system


o The multiple processors are busier at the same time to
complete any task in a multitasking environment, so the
CPU generates more heat.
Network Operating System

An Operating system, which includes software and associated


protocols to communicate with other computers via a network
conveniently and cost-effectively, is called Network Operating
System.

Advantages of Network Operating System


o In this type of operating system, network traffic reduces
due to the division between clients and the server.
o This type of system is less expensive to set up and
maintain.

Disadvantages of Network Operating System


o In this type of operating system, the failure of any node in
a system affects the whole system.
o Security and performance are important issues. So trained
network administrators are required for network
administration.

Real Time Operating System

In Real-Time Systems, each job carries a certain deadline


within which the job is supposed to be completed, otherwise,
the huge loss will be there, or even if the result is produced, it
will be completely useless.

The Application of a Real-Time system exists in the case of


military applications, if you want to drop a missile, then the
missile is supposed to be dropped with a certain precision.
Advantages of Real-time operating system:
o Easy to layout, develop and execute real-time applications
under the real-time operating system.
o In a Real-time operating system, the maximum utilization
of devices and systems.

Disadvantages of Real-time operating system:


o Real-time operating systems are very costly to develop.
o Real-time operating systems are very complex and can
consume critical CPU cycles.

Time-Sharing Operating System

In the Time Sharing operating system, computer resources are


allocated in a time-dependent fashion to several programs
simultaneously. Thus it helps to provide a large number of
user's direct access to the main computer. It is a logical
extension of multiprogramming. In time-sharing, the CPU is
switched among multiple programs given by different users on
a scheduled basis.
A time-sharing operating system allows many users to be
served simultaneously, so sophisticated CPU scheduling
schemes and Input/output management are required.

Time-sharing operating systems are very difficult and


expensive to build.

Advantages of Time Sharing Operating System


o The time-sharing operating system provides effective
utilization and sharing of resources.
o This system reduces CPU idle and response time.

Disadvantages of Time Sharing Operating System


o Data transmission rates are very high in comparison to
other methods.
o Security and integrity of user programs loaded in memory
and data need to be maintained as many users access the
system at the same time.
Distributed Operating System

The Distributed Operating system is not installed on a single


machine, it is divided into parts, and these parts are loaded on
different machines. A part of the distributed Operating system
is installed on each machine to make their communication
possible. Distributed Operating systems are much more
complex, large, and sophisticated than Network operating
systems because they also have to take care of varying
networking protocols.

Advantages of Distributed Operating System


o The distributed operating system provides sharing of
resources.
o This type of system is fault-tolerant.

Disadvantages of Distributed Operating System


o Protocol overhead can dominate computation cost.

 Throughput − Throughput is the amount of work completed in a


unit of time. In other words throughput is the processes executed
to number of jobs completed in a unit of time. The scheduling
algorithm must look to maximize the number of jobs processed
per time unit.

What is Spooling

Spooling is a process in which data is temporarily held to be


used and executed by a device, program, or system. Data is
sent to and stored in memory or other volatile storage until the
program or computer requests it for execution.

SPOOL is an acronym for simultaneous peripheral operations


online. Generally, the spool is maintained on the computer's
physical memory, buffers, or the I/O device-specific interrupts.
The spool is processed in ascending order, working based on a
FIFO (first-in, first-out) algorithm.

Spooling refers to putting data of various I/O jobs in a buffer.


This buffer is a special area in memory or hard disk which is
accessible to I/O devices. An operating system does the
following activities related to the distributed environment:

o Handles I/O device data spooling as devices have different


data access rates.
o Maintains the spooling buffer, which provides a waiting
station where data can rest while the slower device
catches up.
o Maintains parallel computation because of the spooling
process as a computer can perform I/O in parallel order.
It becomes possible to have the computer read data from
a tape, write data to disk, and write out to a tape printer
while it is doing its computing task.

How Spooling Works in Operating System

In an operating system, spooling works in the following steps,


such as:
1. Spooling involves creating a buffer called SPOOL, which is
used to hold off jobs and data till the device in which the
SPOOL is created is ready to make use and execute that
job or operate on the data.
2. When a faster device sends data to a slower device to
perform some operation, it uses any secondary memory
attached as a SPOOL buffer. This data is kept in the
SPOOL until the slower device is ready to operate on this
data. When the slower device is ready, then the data in
the SPOOL is loaded onto the main memory for the
required operations.
3. required operations.

4. Spooling considers the entire secondary memory as a


huge buffer that can store many jobs and data for many
operations. The advantage of Spooling is that it can create
a queue of jobs that execute in FIFO order to execute the
jobs one by one.
5. A device can connect to many input devices, which may
require some operation on their data. So, all of these
input devices may put their data onto the secondary
memory (SPOOL), which can then be executed one by one
by the device. This will make sure that the CPU is not idle
at any time. So, we can say that Spooling is a
combination of buffering and queuing.
6. After the CPU generates some output, this output is first
saved in the main memory. This output is transferred to
the secondary memory from the main memory, and from
there, the output is sent to the respective output devices.

1. make sure that the CPU is not idle at any time. So, we can
say that Spooling is a combination of buffering and
queuing.
2. After the CPU generates some output, this output is first
saved in the main memory. This output is transferred to
the secondary memory from the main memory, and from
there, the output is sent to the respective output devices.

Example of Spooling

The biggest example of Spooling is printing. The documents


which are to be printed are stored in the SPOOL and then
added to the queue for printing. During this time, many
processes can perform their operations and use the CPU
without waiting while the printer executes the printing process
on the documents one-by-one.
Many features can also be added to the Spooling printing
process, like setting priorities or notification when the printing
process has been completed or selecting the different types of
paper to print on according to the user's choice.

Advantages of Spooling

Here are the following advantages of spooling in an operating


system, such as:

o The number of I/O devices or operations does not matter.


Many I/O devices can work together simultaneously
without any interference or disruption to each other.
o In spooling, there is no interaction between the I/O
devices and the CPU. That means there is no need for the
CPU to wait for the I/O operations to take place. Such
operations take a long time to finish executing, so the CPU
will not wait for them to finish.
o CPU in the idle state is not considered very efficient. Most
protocols are created to utilize the CPU efficiently in the
minimum amount of time. In spooling, the CPU is kept
busy most of the time and only goes to the idle state
when the queue is exhausted. So, all the tasks are added
to the queue, and the CPU will finish all those tasks and
then go into the idle state.
o It allows applications to run at the speed of the CPU while
operating the I/O devices at their respective full speeds.

Disadvantages of Spooling

In an operating system, spooling has the following


disadvantages, such as:

o Spooling requires a large amount of storage depending on


the number of requests made by the input and the
number of input devices connected.
o Because the SPOOL is created in the secondary storage,
having many input devices working simultaneously may
take up a lot of space on the secondary storage and thus
increase disk traffic. This results in the disk getting slower
and slower as the traffic increases more and more.

Spooling is used for copying and executing data from a slower


device to a faster device. The slower device creates a SPOOL to
store the data to be operated upon in a queue, and the CPU
works on it. This process in itself makes Spooling futile to use
in real-time environments where we need real-time results
from the CPU. This is because the input device is slower and
thus produces its data at a slower pace while the CPU can
operate faster, so it moves on to the next process in the
queue. This is why the final result or output is produced at a
later time instead of I real time.
PROCESS

A Program does nothing unless its


instructions are executed by a
CPU. A program in execution is
called a process. In order to
accomplish its task, process
needs the computer resources.
There may exist more than one
process in the system which may
require the same resource at the
same time. Therefore, the
operating system has to manage
all the processes and the
resources in a convenient and
efficient way.
Some resources may need to be
executed by one process at one
time to maintain the consistency
otherwise the system can become
inconsistent and deadlock may
occur.

A process is basically a program in


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

Features of the Process


There are various features of the
process. Some of them are as
follows:
1. Each process contains a
definite set of data linked to it.
For example, its parent's
name, the address of the
allotted memory space, and
security properties such as
ownership credentials and
rights.
2. A process contains a limited
lifetime, i.e., only when a
process is being executed.
3. Processes are allotted
system resources. Network
ports and file descriptors are
two examples.
4. It is an active entity.
5. It contains high resources.
6. It needs resources including
memory address, CPU, I/O
during its working.
7. Each process may produce
child processes. Furthermore,
they may die or be killed.
8. Program vs Process 
A process is a program in execution.
For example, when we write a program
in C or C++ and compile it, the
compiler creates binary code. The
original code and binary code are both
programs. When we actually run the
binary code, it becomes a process. 
9. A process is an ‘active’ entity,
instead of a program, which is
considered a ‘passive’ entity. A single
program can create many processes
when run multiple times; for example,
when we open a .exe or binary file
multiple times, multiple instances begin
(multiple processes are created). 

Here, you will learn the head-to-


head difference between the
process and program. Some of
the differences are as follows:

Featu Proce Progra


res ss m

Defin A A
ition progr proce
am ss is
has a an
collec exam
tion ple of
of an
instru execu
ctions tion
desig progr
ned am.
to
acco
mplis
h a
certai
n
task.
Natur It is It is a
e an passiv
activ e
e entity
entity .
.

Lifes It has It has


pan a a
limite much
d higher
lifesp lifesp
an. an.

Reso It has It
urces a doesn
high 't
resou have
rces any
requi resour
reme ces
nt, requir
and it ement
requi s; it
res only
includ needs
ing memo
CPU, ry
mem space
ory to
addre store
ss, the
disk, instru
Input ctions
/outp .
ut
durin
g its
lifeti
me.

Creat The No
ion new much
proce duplic
ss ation
needs is
duplic neede
ation d.
of the
paren
t
proce
ss.

Com The It has


putat proce no
ion ss comp
Time takes utatio
a n time
long and
time cost.
to
acces
s and
comp
ute a
single
fact.

Requi It The
red holds progr
Proce resou am is
ss rces stored
includ on a
ing disk
CPU, in a
disk, file
mem and
ory doesn
addre 't
ss, need
Input any
/Outp additi
ut onal
etc. resour
ces.

Over It has It has


head consi no
derab signifi
le cant
overh overh
ead. ead
cost.

Cach It It has
e may the
Data use instru
the ction
cache to use
to cache
store for its
the data.
retrie
ve
the
data
as it
uses
OS
pagin
g
sche
me
and
cache
repla
ceme
nt
policy
like
FCFS,
LRU,
RR,
LIFO.

Conclusion
Process and program are related
terms, but they are not the same.
A program is simply a script or
file that contains ordered and
sequential operations and is kept
on disk, just like a previous stage
of the process. The process is the
event produced by executing the
program, which is executed to
produce the process event.

Process States
State Diagram
The process, from its creation to
completion, passes through
various states. The minimum
number of states is five.
The names of the states are not
standardized although the process
may be in one of the following
states during execution.
1. New
A program which is going to be
picked up by the OS into the main
memory is called a new process.
2. Ready
Whenever a process is created, it
directly enters in the ready state,
in which, it waits for the CPU to
be assigned. The OS picks the
new processes from the
secondary memory and put all of
them in the main memory.
The processes which are ready for
the execution and reside in the
main memory are called ready
state processes. There can be
many processes present in the
ready state.
3. Running
One of the processes from the
ready state will be chosen by the
OS depending upon the
scheduling algorithm. Hence, if
we have only one CPU in our
system, the number of running
processes for a particular time will
always be one. If we have n
processors in the system then we
can have n processes running
simultaneously.
4. Block or wait
From the Running state, a
process can make the transition
to the block or wait state
depending upon the scheduling
algorithm or the intrinsic behavior
of the process.
When a process waits for a
certain resource to be assigned or
for the input from the user then
the OS move this process to the
block or wait state and assigns
the CPU to the other processes.
5. Completion or termination
When a process finishes its
execution, it comes in the
termination state. All the context
of the process (Process Control
Block) will also be deleted the
process will be terminated by the
Operating system.
6. Suspend ready
A process in the ready state,
which is moved to secondary
memory from the main memory
due to lack of the resources
(mainly primary memory) is
called in the suspend ready state.
If the main memory is full and a
higher priority process comes for
the execution then the OS have to
make the room for the process in
the main memory by throwing the
lower priority process out into the
secondary memory. The suspend
ready processes remain in the
secondary memory until the main
memory gets available.
7. Suspend wait
Instead of removing the process
from the ready queue, it's better
to remove the blocked process
which is waiting for some
resources in the main memory.
Since it is already waiting for
some resource to get available
hence it is better if it waits in the
secondary memory and make
room for the higher priority
process. These processes
complete their execution once the
main memory gets available and
their wait is finished.
PROCESS CREATION BY COPY

A process control block (PCB) contains


information about the process, i.e.
registers, quantum, priority, etc. The
process table is an array of PCB’s, that
means logically contains a PCB for all of
the current processes in the system.
 Pointer – It is a stack pointer which is
required to be saved when the process
is switched from one state to another
to retain the current position of the
process.
 Process state – It stores the respective
state of the process.
 Process number – Every process is
assigned with a unique id known as
process ID or PID which stores the
process identifier.
 Program counter – It stores the
counter which contains the address of
the next instruction that is to be
executed for the process.
 Register – These are the CPU registers
which includes: accumulator, base,
registers and general purpose
registers.
 Memory limits – This field contains
the information about memory
management system used by operating
system. This may include the page
tables, segment tables etc.
 Open files list – This information
includes the list of files opened for a
process.

Process spawing by copy also


 When the OS at the explicit request
of another process creates a
process, this action is called
process spawning
 It refers to function that loads and
executes a new child process.
 The current process may wait for
the child process or may continue
executing concurrent computing 
 Creating a new sub-process
requires enough memory in which
both the child process and the
current program can execute.
Concurrent process by copy also
Concurrent processing is a computing model in which multiple
processors execute instructions simultaneously for better
performance. Concurrent means, which occurs when something else
happens. The tasks are broken into sub-types, which are then assigned
to different processors to perform simultaneously, sequentially
instead, as they would have to be performed by one processor.
Concurrent processing is sometimes synonymous with parallel
processing.
The term real and virtual concurrency in concurrent processing:

THREAD

Threads in Operating System

A thread is a single sequential flow of execution of tasks of a


process so it is also known as thread of execution or thread of
control. There is a way of thread execution inside the process
of any operating system. Apart from this, there can be more
than one thread inside a process. Each thread of the same
process makes use of a separate program counter and a stack
of activation records and control blocks. Thread is often
referred to as a lightweight process.

The process can be split down into so many threads. For


example, in a browser, many tabs can be viewed as threads.
MS Word uses many threads - formatting text from one thread,
processing input from another thread, etc.

Life cycle of a Thread (Thread States)

In Java, a thread always exists in any one of the following


states. These states are:

1. New
2. Active
3. Blocked / Waiting
4. Timed Waiting
5. Terminated

Explanation of Different Thread States

New: Whenever a new thread is created, it is always in the new


state. For a thread in the new state, the code has not been run
yet and thus has not begun its execution.

Active: When a thread invokes the start() method, it moves


from the new state to the active state. The active state
contains two states within it: one is runnable, and the other
is running.

o Runnable: A thread, that is ready to run is then moved to


the runnable state. In the runnable state, the thread may
be running or may be ready to run at any given instant of
time. It is the duty of the thread scheduler to provide the
thread time to run, i.e., moving the thread the running
state.
A program implementing multithreading acquires a fixed
slice of time to each individual thread. Each and every
thread runs for a short span of time and when that
allocated time slice is over, the thread voluntarily gives up
the CPU to the other thread, so that the other threads can
also run for their slice of time. Whenever such a scenario
occurs, all those threads that are willing to run, waiting
for their turn to run, lie in the runnable state. In the
runnable state, there is a queue where the threads lie.
o Running: When the thread gets the CPU, it moves from the
runnable to the running state. Generally, the most
common change in the state of a thread is from runnable
to running and again back to runnable.
Blocked or Waiting: Whenever a thread is inactive for a span of
time (not permanently) then, either the thread is in the blocked
state or is in the waiting state.

For example, a thread (let's say its name is A) may want to


print some data from the printer. However, at the same time,
the other thread (let's say its name is B) is using the printer to
print some data. Therefore, thread A has to wait for thread B to
use the printer. Thus, thread A is in the blocked state. A thread
in the blocked state is unable to perform any execution and
thus never consume any cycle of the Central Processing Unit
(CPU). Hence, we can say that thread A remains idle until the
thread scheduler reactivates thread A, which is in the waiting
or blocked state.

When the main thread invokes the join() method then, it is said
that the main thread is in the waiting state. The main thread
then waits for the child threads to complete their tasks. When
the child threads complete their job, a notification is sent to the
main thread, which again moves the thread from waiting to the
active state.

If there are a lot of threads in the waiting or blocked state,


then it is the duty of the thread scheduler to determine which
thread to choose and which one to reject, and the chosen
thread is then given the opportunity to run.

Timed Waiting: Sometimes, waiting for leads to starvation. For


example, a thread (its name is A) has entered the critical
section of a code and is not willing to leave that critical section.
In such a scenario, another thread (its name is B) has to wait
forever, which leads to starvation. To avoid such scenario, a
timed waiting state is given to thread B. Thus, thread lies in
the waiting state for a specific span of time, and not forever. A
real example of timed waiting is when we invoke the sleep()
method on a specific thread. The sleep() method puts the
thread in the timed wait state. After the time runs out, the
thread wakes up and start its execution from when it has left
earlier.
Terminated: A thread reaches the termination state because of
the following reasons:

o When a thread has finished its job, then it exists or


terminates normally.
o Abnormal termination: It occurs when some unusual events
such as an unhandled exception or segmentation fault.

A terminated thread means the thread is no more in the


system. In other words, the thread is dead, and there is no
way one can respawn (active after kill) the dead thread.

The following diagram shows the different states involved in


the life cycle of a thread.

There exists three established multithreading models classifying


these relationships are:
o Many to one multithreading model
o One to one multithreading model
o Many to Many multithreading models

Many to one multithreading model:

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

The disadvantage of this model is that since there is only one


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

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

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


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

In the above figure, one model associates that one user-level


thread to a single kernel-level thread.

Many to Many Model multithreading model

In this type of model, there are several user-level threads and


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

Many to many versions of the multithreading model associate


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

Difference Table Between Process and Thread


Process Thread

A process is an Thread is a
instance of a segment of a
program that is process or a
being executed lightweight
or processed. process that is
managed by the
scheduler
independently.

Processes are Threads are


independent of interdependent
each other and and share
hence don't memory.
share a
memory or
other
resources.

Each process is The operating


treated as a system takes all
new process by the user-level
the operating threads as a single
system. process.

If one process If any user-level


gets blocked thread gets
by the blocked, all of its
operating peer threads also
system, then get blocked
the other because OS takes
process can all of them as a
continue the single process.
execution.
Context Context switching
switching between the
between two threads is fast
processes because they are
takes much very lightweight.
time as they
are heavy
compared to
thread.

The data Threads share


segment and data segment and
code segment code segment with
of each process their peer threads;
are hence are the
independent of same for other
the other. threads also.

The operating Threads can be


system takes terminated in very
more time to little time.
terminate a
process.

New process A thread needs


creation is less time for
more time creation.
taking as each
new process
takes all the
resources.
CPU SCHEDULING

CPU Scheduling
In the uniprogrammming
systems like MS DOS, when a
process waits for any I/O
operation to be done, the CPU
remains idol. This is an overhead
since it wastes the time and
causes the problem of starvation.
However, In Multiprogramming
systems, the CPU doesn't remain
idle during the waiting time of the
Process and it starts executing
other processes. Operating
System has to define which
process the CPU will be given.
In Multiprogramming systems, the
Operating system schedules the
processes on the CPU to have the
maximum utilization of it and this
procedure is called CPU
scheduling. The Operating System
uses various scheduling algorithm
to schedule the processes.
This is a task of the short term
scheduler to schedule the CPU for
the number of processes present
in the Job Pool. Whenever the
running process requests some IO
operation then the short term
scheduler saves the current
context of the process (also called
PCB) and changes its state from
running to waiting. During the
time, process is in waiting state;
the Short term scheduler picks
another process from the ready
queue and assigns the CPU to this
process. This procedure is
called context switching.
Why do we need Scheduling?
In Multiprogramming, if the long
term scheduler picks more I/O
bound processes then most of the
time, the CPU remains idol. The
task of Operating system is to
optimize the utilization of
resources.
If most of the running processes
change their state from running
to waiting then there may always
be a possibility of deadlock in the
system. Hence to reduce this
overhead, the OS needs to
schedule the jobs to get the
optimal utilization of CPU and to
avoid the possibility to deadlock.
What is Preemptive Scheduling?
Preemptive scheduling is a
method that may be used when a
process switches from a running
state to a ready state or from a
waiting state to a ready state.
The resources are assigned to the
process for a particular time and
then removed. If the resources
still have the remaining CPU burst
time, the process is placed back
in the ready queue. The process
remains in the ready queue until
it is given a chance to execute
again.
When a high-priority process
comes in the ready queue, it
doesn't have to wait for the
running process to finish its burst
time. However, the running
process is interrupted in the
middle of its execution and placed
in the ready queue until the high-
priority process uses the
resources. As a result, each
process gets some CPU time in
the ready queue. It improves the
overhead of switching a process
from running to ready state and
vice versa, increasing preemptive
scheduling flexibility. It may or
may not include SJF and Priority
scheduling.
For example:
Let us take the example of
Preemptive Scheduling. We have
taken P0, P1, P2, and P3 are the
four processes.

Proce Arriv CPU


ss al Burst
Time time
(in
millisec
.)

P0 3 2

P1 2 4
P2 0 6

P3 1 4

o Firstly, the process P2 comes


at time 0. So, the CPU is
assigned to process P2.
o When process P2 was running,
process P3 arrived at time 1,
and the remaining time for
process P2 (5 ms) is greater
than the time needed by
process P3 (4 ms). So, the
processor is assigned to P3.
o When process P3 was running,
process P1 came at time 2, and
the remaining time for
process P3 (3 ms) is less than
the time needed by
processes P1 (4 ms) and P2 (5
ms). As a result, P3 continues
the execution.
o When process P3 continues the
process, process P0 arrives at
time 3. P3's remaining time (2
ms) is equal to P0's necessary
time (2 ms). So,
process P3 continues the
execution.
o When process P3 finishes, the
CPU is assigned to P0, which
has a shorter burst time than
the other processes.
o After process P0 completes, the
CPU is assigned to
process P1 and then to
process P2.
Advantages and disadvantages of
Preemptive Scheduling
There are various advantages and
disadvantages of Preemptive
scheduling. The advantages and
disadvantages of non-preemptive
scheduling are as follows:
Advantages
1. It is a more robust method
because a process may not
monopolize the processor.
2. Each event causes an
interruption in the execution of
ongoing tasks.
3. It improves the average
response time.
4. It is more beneficial when
you use this method in a multi-
programming environment.
5. The operating system
ensures that all running
processes use the same
amount of CPU.
Disadvantages
1. It requires the use of limited
computational resources.
2. It takes more time
suspending the executing
process, switching the context,
and dispatching the new
incoming process.
3. If several high-priority
processes arrive at the same
time, the low-priority process
would have to wait longer.
What is Non-Preemptive
Scheduling?
Non-preemptive scheduling is a
method that may be used when a
process terminates or switches
from a running to a waiting state.
When processors are assigned to
a process, they keep the process
until it is eliminated or reaches a
waiting state. When the processor
starts the process execution, it
must complete it before executing
the other process, and it may not
be interrupted in the middle.
When a non-preemptive process
with a high CPU burst time is
running, the other process would
have to wait for a long time, and
that increases the process
average waiting time in the ready
queue. However, there is no
overhead in transferring
processes from the ready queue
to the CPU under non-preemptive
scheduling. The scheduling is
strict because the execution
process is not even preempted for
a higher priority process.
For example:
Let's take the above preemptive
scheduling example and solve it
in a non-preemptive manner.

o The process P2 comes at 0, so


the processor is assigned to
process P2 and takes (6 ms) to
execute.
o All of the processes, P0, P1,
and P3, arrive in the ready
queue in between. But all
processes wait till
process P2 finishes its CPU
burst time.
o After that, the process that
comes after process P2,
i.e., P3, is assigned to the CPU
until it finishes its burst time.
o When process P1 completes its
execution, the CPU is given to
process P0.
Advantages and disadvantages of
Non-preemptive Scheduling
There are various advantages and
disadvantages of non-preemptive
scheduling. The advantages and
disadvantages of non-preemptive
scheduling are as follows:
Advantages
1. It provides a low scheduling
overhead.
2. It is a very simple method.
3. It uses less computational
resources.
4. It offers high throughput.
Disadvantages
1. It has a poor response time
for the process.
2. A machine can freeze up
due to bugs.
3. Pree Non-
mptive Preemp
Scheduli tive
ng Scheduli
ng

The Once
resources resourc
are es are
assigned assigne
to a d to a
process process
for a long , they
time are
period. held
until it
comple
tes its
burst
period
or
change
s to the
waiting
state.

Its When
process the
may be process
paused in or
the middle starts
of the the
execution. process
executi
on, it
must
comple
te it
before
executi
ng the
other
process
, and it
may
not be
interru
pted in
the
middle.
When a When a
high- high
priority burst
process time
continuous process
ly comes uses a
in the CPU,
ready another
queue, a process
low- with a
priority shorter
process burst
can time
starve. can
starve.

It is It is
flexible. rigid.

It is cost It does
associated not
. cost
associa
ted.

It has It
overheads doesn't
associated have
with overhe
process ad.
scheduling
.

It affects It
the design doesn't
of the affect
operating the
system design
kernel. of the
OS
kernel.

Its CPU Its CPU


utilization utilizati
is very on is
high. very
low.

Examples: FCFS
Round and SJF
Robin and are
Shortest exampl
Remaining es of
Time First non-
preemp
tive
schedul
ing.

Process Schedulers
Operating system uses various
schedulers for the process
scheduling described below.
1. Long term scheduler
Long term scheduler is also
known as job scheduler. It
chooses the processes from the
pool (secondary memory) and
keeps them in the ready queue
maintained in the primary
memory.
Long Term scheduler mainly
controls the degree of
Multiprogramming. The purpose
of long term scheduler is to
choose a perfect mix of IO bound
and CPU bound processes among
the jobs present in the pool.
If the job scheduler chooses more
IO bound processes then all of the
jobs may reside in the blocked
state all the time and the CPU will
remain idle most of the time. This
will reduce the degree of
Multiprogramming. Therefore, the
Job of long term scheduler is very
critical and may affect the system
for a very long time.
2. Short term scheduler
Short term scheduler is also
known as CPU scheduler. It
selects one of the Jobs from the
ready queue and dispatch to the
CPU for the execution.
A scheduling algorithm is used to
select which job is going to be
dispatched for the execution. The
Job of the short term scheduler
can be very critical in the sense
that if it selects job whose CPU
burst time is very high then all
the jobs after that, will have to
wait in the ready queue for a very
long time.
This problem is called starvation
which may arise if the short term
scheduler makes some mistakes
while selecting the job.
3. Medium term scheduler
Medium term scheduler takes
care of the swapped out
processes.If the running state
processes needs some IO time for
the completion then there is a
need to change its state from
running to waiting.
Medium term scheduler is used
for this purpose. It removes the
process from the running state to
make room for the other
processes. Such processes are the
swapped out processes and this
procedure is called swapping. The
medium term scheduler is
responsible for suspending and
resuming the processes.
It reduces the degree of
multiprogramming. The swapping
is necessary to have a perfect mix
of processes in the ready queue.

What is Dispatcher in OS
A dispatcher is a special program
that comes into play after the
scheduler. When the short term
scheduler selects from the ready
queue, the Dispatcher performs
the task of allocating the selected
process to the CPU. A running
process goes to the waiting state
for IO operation etc., and then
the CPU is allocated to some
other process. This switching of
CPU from one process to the
other is called context switching.

A dispatcher performs various


tasks, including context switching,
setting up user registers and
memory mapping. These are
necessary for the process to
execute and transfer CPU control
to that process. When
dispatching, the process changes
from the ready state to the
running state.
The Dispatcher needs to be as
fast as possible, as it is run on
every context switch. The time
consumed by the Dispatcher is
known as dispatch latency.
Sometimes, the Dispatcher is
considered part of the short-term
scheduler, so the whole unit is
called the short-term scheduler. In
this scenario, the task of the
short term scheduler is to select a
process from the ready queue and
allocate the CPU for that process.
In the operating system, a
dispatcher has the following
responsibilities:
o Switching to user mode: All of
the low-level operating system
processes run on the kernel
level security access, but all
application code and user
issued processes run in the
application space or the user
permission mode. The
Dispatcher switches the
processes to the user mode.
o Addressing: The program
counter (PC) register points
towards the next process to be
executed. The Dispatcher is
responsible for addressing that
address.
o Initiation of context switch: A
context switch is when a
currently running process is
halted, and all of its data and
its process control block (PCB)
are stored in the main
memory, and another process
is loaded in its place for
execution.
o Managing dispatch
latency: Dispatch latency is
calculated as the time it takes
to stop one process and start
another. The lower the
dispatch latency, the more
efficient the software for the
same hardware configuration.
NOTE: A dispatcher is NOT a
thread. The Dispatcher runs on
each core, runs a thread for a while,
saves its state, loads the state of
another thread and runs it.

Scheduling methodology by copy


Scheduling
Algorithms
There are various
algorithms which
are used by the
Operating System
to schedule the
processes on the
processor in an
efficient way.
The Purpose of a
Scheduling algorithm
1. Maximum CPU
utilization
2. Fare allocation
of CPU
3. Maximum
throughput
4. Minimum
turnaround time
5. Minimum
waiting time
6. Minimum
response time
There are the
following algorithms
which can be used
to schedule the
jobs.
1. First Come First
Serve
It is the simplest
algorithm to
implement. The
process with the
minimal arrival time
will get the CPU
first. The lesser the
arrival time, the
sooner will the
process gets the
CPU. It is the non-
preemptive type of
scheduling.
2. Round Robin
In the Round Robin
scheduling
algorithm, the OS
defines a time
quantum (slice). All
the processes will
get executed in the
cyclic way. Each of
the process will get
the CPU for a small
amount of time
(called time
quantum) and then
get back to the
ready queue to wait
for its next turn. It
is a preemptive type
of scheduling.
3. Shortest Job First
The job with the
shortest burst time
will get the CPU
first. The lesser the
burst time, the
sooner will the
process get the
CPU. It is the non-
preemptive type of
scheduling.
4. Shortest remaining
time first
It is the preemptive
form of SJF. In this
algorithm, the OS
schedules the Job
according to the
remaining time of
the execution.
. Priority based scheduling
In this algorithm, the priority will
be assigned to each of the
processes. The higher the priority,
the sooner will the process get
the CPU. If the priority of the two
processes is same then they will
be scheduled according to their
arrival time.

FCFS Scheduling
First come first serve (FCFS)
scheduling algorithm simply
schedules the jobs according to
their arrival time. The job which
comes first in the ready queue
will get the CPU first. The lesser
the arrival time of the job, the
sooner will the job get the CPU.
FCFS scheduling may cause the
problem of starvation if the burst
time of the first process is the
longest among all the jobs.
Advantages of FCFS
oSimple
oEasy
oFirst come, First serv
Disadvantages of FCFS
1. The scheduling method is
non preemptive, the process
will run to the completion.
2. Due to the non-preemptive
nature of the algorithm, the
problem of starvation may
occur.
3. Although it is easy to
implement, but it is poor in
performance since the average
waiting time is higher as
compare to other scheduling
algorithms.

EXAMPLE LEARN BY COPY

Shortest Job First (SJF) Scheduling


Till now, we were scheduling the
processes according to their
arrival time (in FCFS scheduling).
However, SJF scheduling
algorithm, schedules the
processes according to their burst
time.
In SJF scheduling, the process
with the lowest burst time, among
the list of available processes in
the ready queue, is going to be
scheduled next.
However, it is very difficult to
predict the burst time needed for
a process hence this algorithm is
very difficult to implement in the
system.
Advantages of SJF
1. Maximum throughput
2. Minimum average waiting
and turnaround time
Disadvantages of SJF
1. May suffer with the problem
of starvation
2. It is not implementable
because the exact Burst time
for a process can't be known in
advance.
There are different techniques
available by which, the CPU burst
time of the process can be
determined. We will discuss them
later in detail.

EXAMPLE LEARN BY COPY

Round Robin Scheduling Algorithm


Round Robin scheduling algorithm
is one of the most popular
scheduling algorithm which can
actually be implemented in most
of the operating systems. This is
the preemptive version of first
come first serve scheduling. The
Algorithm focuses on Time
Sharing. In this algorithm, every
process gets executed in a cyclic
way. A certain time slice is
defined in the system which is
called time quantum. Each
process present in the ready
queue is assigned the CPU for
that time quantum, if the
execution of the process is
completed during that time then
the process will terminate else the
process will go back to the ready
queue and waits for the next turn
to complete the execution.
Advantages
1. It can be actually
implementable in the system
because it is not depending on
the burst time.
2. It doesn't suffer from the
problem of starvation or
convoy effect.
3. All the jobs get a fare
allocation of CPU.
Disadvantages
1. The higher the time
quantum, the higher the
response time in the system.
2. The lower the time
quantum, the higher the
context switching overhead in
the system.
3. Deciding a perfect time
quantum is really a very
difficult task in the system.
Priority Scheduling
In Priority scheduling, there is a
priority number assigned to each
process. In some systems, the
lower the number, the higher the
priority. While, in the others, the
higher the number, the higher will
be the priority. The Process with
the higher priority among the
available processes is given the
CPU. There are two types of
priority scheduling algorithm
exists. One is Preemptive priority
scheduling while the other is Non
Preemptive Priority scheduling.

The priority number assigned to


each of the process may or may
not vary. If the priority number
doesn't change itself throughout
the process, it is called static
priority, while if it keeps changing
itself at the regular intervals, it is
called dynamic priority.
EXAMPLE BY COPY

Multi-level Queue Scheduling (MLQ)

 Multilevel Queue
Scheduling classifies the processes
according to their types. For
example, a multilevel queue
scheduling algorithm makes a
common division between the
interactive processes (foreground)
and batch processes (background).
These two processes have different
response times, so they have
different scheduling requirements.
Also, the interactive process has
higher priority than the batch
process.
 In this scheduling, ready queue is
divided into various queues that are
called subqueues. A subqueue is a
distinct operational queue.
 The method separates the ready
queue into various separate queues
is Multilevel Queue.
 The processes are permanently
assigned to subqueues, generally
based on some property of the
process such as memory size,
priority or process type.
 Each subqueue has its own
scheduling algorithm. For example,
interactive processes at the
foreground may use round robin
scheduling while batch jobs at the
background may use
the FCFS method.
 In addition, there is a scheduling
algorithm that works globally
between the different subqueues.
Usually this is a fixed priority
preemptive scheduling. For
example, the foreground
queue may have absolute priority
over the background queue.

Multi-level Feedback Queue


Scheduling – CPU Scheduling

 Multi-level feedback queue


scheduling is an enhancement of
multi level queue scheduling. In this
scheme, processes can move
between the different queues.
 The various processes separated in
different queues on the basis of
their CPU burst characteristics.
 If a process consumes a lot of CPU
time, placed into a lower priority
queue. Thus I/0 bound and
interactive processes placed in the
higher priority queue and CPU
bound processes are in lower
priority queue.

 If a process waits too long in a
lower priority queue. It is moved
higher priority queue Such an aging
prevents starvation.
 The top priority queue has given
smallest CPU time quantum.
 If the quantum expires before the
process voluntarily relinquishes the
CPU, the process placed at the
back of the next lower queue.
 In many multi-level feedback
schemes, the quantum given to the
process an it moves to each lower
level queue becomes larger.

Introduction to Deadlock
Every process needs some resources to complete
its execution. However, the resource is granted
in a sequential order.
1. The process requests for some resource.
2. OS grant the resource if it is available
otherwise let the process waits.
3. The process uses it and release on the
completion.
A Deadlock is a situation where each of the
computer process waits for a resource which is
being assigned to some another process. In this
situation, none of the process gets executed
since the resource it needs, is held by some
other process which is also waiting for some
other resource to be released.
Let us assume that there are three processes P1,
P2 and P3. There are three different resources
R1, R2 and R3. R1 is assigned to P1, R2 is
assigned to P2 and R3 is assigned to P3.
After some time, P1 demands for R1 which is
being used by P2. P1 halts its execution since it
can't complete without R2. P2 also demands for
R3 which is being used by P3. P2 also stops its
execution because it can't continue without R3.
P3 also demands for R1 which is being used by
P1 therefore P3 also stops its execution.
In this scenario, a cycle is being formed among
the three processes. None of the process is
progressing and they are all waiting. The
computer becomes unresponsive since all the
processes got blocked.
Difference between Starvation and Deadlock
S Deadlock Starvation
r.

1 Deadlock is Starvation
a situation is a
where no situation
process got where the
blocked and low
no process priority
proceeds process
got
blocked
and the
high
priority
processes
proceed.

2 Deadlock is Starvation
an infinite is a long
waiting. waiting
but not
infinite.

3 Every Every
Deadlock is starvation
always a need not
starvation. be
deadlock.
4 The The
requested requested
resource is resource is
blocked by continuous
the other ly be used
process. by the
higher
priority
processes.

5 Deadlock It occurs
happens due to the
when uncontroll
Mutual ed priority
exclusion, and
hold and resource
wait, No managem
preemption ent.
and circular
wait occurs
simultaneou
sly.

Necessary conditions for Deadlocks


1. Mutual Exclusion

A resource can only be shared in mutually


exclusive manner. It implies, if two process
cannot use the same resource at the same
time.
2. Hold and Wait

A process waits for some resources while


holding another resource at the same time.

3. No preemption

The process which once scheduled will be


executed till the completion. No other
process can be scheduled by the scheduler
meanwhile.

4. Circular Wait

All the processes must be waiting for the


resources in a cyclic manner so that the last
process is waiting for the resource which is
being held by the first process.

Strategies for handling Deadlock


1. Deadlock Ignorance
Deadlock Ignorance is the most widely used
approach among all the mechanism. This is being
used by many operating systems mainly for end
user uses. In this approach, the Operating
system assumes that deadlock never occurs. It
simply ignores deadlock. This approach is best
suitable for a single end user system where User
uses the system only for browsing and all other
normal stuff.
There is always a tradeoff between Correctness
and performance. The operating systems like
Windows and Linux mainly focus upon
performance. However, the performance of the
system decreases if it uses deadlock handling
mechanism all the time if deadlock happens 1
out of 100 times then it is completely
unnecessary to use the deadlock handling
mechanism all the time.
In these types of systems, the user has to simply
restart the computer in the case of deadlock.
Windows and Linux are mainly using this
approach.
2. Deadlock prevention
Deadlock happens only when Mutual Exclusion,
hold and wait, No preemption and circular wait
holds simultaneously. If it is possible to violate
one of the four conditions at any time then the
deadlock can never occur in the system.
The idea behind the approach is very simple that
we have to fail one of the four conditions but
there can be a big argument on its physical
implementation in the system.
3. Deadlock avoidance
In deadlock avoidance, the operating system
checks whether the system is in safe state or in
unsafe state at every step which the operating
system performs. The process continues until the
system is in safe state. Once the system moves
to unsafe state, the OS has to backtrack one
step.
In simple words, The OS reviews each allocation
so that the allocation doesn't cause the deadlock
in the system.
We will discuss Deadlock avoidance later in
detail.
4. Deadlock detection and recovery
This approach let the processes fall in deadlock
and then periodically check whether deadlock
occur in the system or not. If it occurs then it
applies some of the recovery methods to the
system to get rid of deadlock.
We will discuss deadlock detection and recovery
later in more detail since it is a matter of
discussion.

Deadlock Prevention
If we simulate deadlock with a table which is
standing on its four legs then we can also
simulate four legs with the four conditions which
when occurs simultaneously, cause the deadlock.
However, if we break one of the legs of the table
then the table will fall definitely. The same
happens with deadlock, if we can be able to
violate one of the four necessary conditions and
don't let them occur together then we can
prevent the deadlock.
Let's see how we can prevent each of the
conditions.
1. Mutual Exclusion
Mutual section from the resource point of view is
the fact that a resource can never be used by
more than one process simultaneously which is
fair enough but that is the main reason behind
the deadlock. If a resource could have been used
by more than one process at the same time then
the process would have never been waiting for
any resource.
However, if we can be able to violate resources
behaving in the mutually exclusive manner then
the deadlock can be prevented.
Spooling
For a device like printer, spooling can work.
There is a memory associated with the printer
which stores jobs from each of the process into
it. Later, Printer collects all the jobs and print
each one of them according to FCFS. By using
this mechanism, the process doesn't have to wait
for the printer and it can continue whatever it
was doing. Later, it collects the output when it is
produced.
Although, Spooling can be an effective approach
to violate mutual exclusion but it suffers from
two kinds of problems.
1. This cannot be applied to every resource.
2. After some point of time, there may arise
a race condition between the processes to
get space in that spool.
We cannot force a resource to be used by more
than one process at the same time since it will
not be fair enough and some serious problems
may arise in the performance. Therefore, we
cannot violate mutual exclusion for a process
practically.
2. Hold and Wait
Hold and wait condition lies when a process holds
a resource and waiting for some other resource
to complete its task. Deadlock occurs because
there can be more than one process which are
holding one resource and waiting for other in the
cyclic order.
However, we have to find out some mechanism
by which a process either doesn't hold any
resource or doesn't wait. That means, a process
must be assigned all the necessary resources
before the execution starts. A process must not
wait for any resource once the execution has
been started.

!(Hold and wait) = !hold or !wait (negation of hold


and wait is, either you don't hold or you don't wait)

This can be implemented practically if a process


declares all the resources initially. However, this
sounds very practical but can't be done in the
computer system because a process can't
determine necessary resources initially.
Process is the set of instructions which are
executed by the CPU. Each of the instruction may
demand multiple resources at the multiple times.
The need cannot be fixed by the OS.
The problem with the approach is:
1. Practically not possible.
2. Possibility of getting starved will be
increases due to the fact that some process
may hold a resource for a very long time.
3. No Preemption
Deadlock arises due to the fact that a process
can't be stopped once it starts. However, if we
take the resource away from the process which is
causing deadlock then we can prevent deadlock.
This is not a good approach at all since if we take
a resource away which is being used by the
process then all the work which it has done till
now can become inconsistent.
Consider a printer is being used by any process.
If we take the printer away from that process
and assign it to some other process then all the
data which has been printed can become
inconsistent and ineffective and also the fact that
the process can't start printing again from where
it has left which causes performance inefficiency.
4. Circular Wait
To violate circular wait, we can assign a priority
number to each of the resource. A process can't
request for a lesser priority resource. This
ensures that not a single process can request a
resource which is being utilized by some other
process and no cycle will be formed.
Among all the methods, violating Circular wait is
the only approach that can be implemented
practically.
Deadlock avoidance
In deadlock avoidance, the request for any
resource will be granted if the resulting state of
the system doesn't cause deadlock in the
system. The state of the system will continuously
be checked for safe and unsafe states.
In order to avoid deadlocks, the process must tell
OS, the maximum number of resources a process
can request to complete its execution.
The simplest and most useful approach states
that the process should declare the maximum
number of resources of each type it may ever
need. The Deadlock avoidance algorithm
examines the resource allocations so that there
can never be a circular wait condition.
Safe and Unsafe States
The resource allocation state of a system can be
defined by the instances of available and
allocated resources, and the maximum instance
of the resources demanded by the processes.
A state of a system recorded at some random
time is shown below.
Resources Assigned
Proces Typ Typ Typ Typ
s e1 e2 e3 e4

A 3 0 2 2

B 0 0 1 1

C 1 1 1 0

D 2 1 4 0

Resources still needed


Proces Typ Typ Typ Typ
s e1 e2 e3 e4

A 1 1 0 0

B 0 1 1 2
C 1 2 1 0

D 2 1 1 2

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

Above tables and vector E, P and A describes the


resource allocation state of a system. There are
4 processes and 4 types of the resources in a
system. Table 1 shows the instances of each
resource assigned to each process.
Table 2 shows the instances of the resources,
each process still needs. Vector E is the
representation of total instances of each resource
in the system.
Vector P represents the instances of resources
that have been assigned to processes. Vector A
represents the number of resources that are not
in use.
A state of the system is called safe if the system
can allocate all the resources requested by all the
processes without entering into deadlock.
If the system cannot fulfill the request of all
processes then the state of the system is called
unsafe.
The key of Deadlock avoidance approach is when
the request is made for resources then the
request must only be approved in the case if the
resulting state is also a safe state.

Resource Allocation Graph


The resource allocation graph is the pictorial
representation of the state of a system. As its
name suggests, the resource allocation graph is
the complete information about all the processes
which are holding some resources or waiting for
some resources.
It also contains the information about all the
instances of all the resources whether they are
available or being used by the processes.
In Resource allocation graph, the process is
represented by a Circle while the Resource is
represented by a rectangle. Let's see the types of
vertices and edges in detail.
Vertices are mainly of two types, Resource and
process. Each of them will be represented by a
different shape. Circle represents process while
rectangle represents resource.
A resource can have more than one instance.
Each instance will be represented by a dot inside
the rectangle.
Edges in RAG are also of two types, one
represents assignment and other represents the
wait of a process for a resource. The above
image shows each of them.
A resource is shown as assigned to a process if
the tail of the arrow is attached to an instance to
the resource and the head is attached to a
process.
A process is shown as waiting for a resource if
the tail of an arrow is attached to the process
while the head is pointing towards the resource.
 

Example
Let'sconsider 3 processes P1, P2 and P3, and two
types of resources R1 and R2. The resources are
having 1 instance each.
According to the graph, R1 is being used by P1,
P2 is holding R2 and waiting for R1, P3 is waiting
for R1 as well as R2.
The graph is deadlock free since no cycle is being
formed in the graph.

Deadlock Detection and Recovery


In this approach, The OS doesn't apply any
mechanism to avoid or prevent the deadlocks.
Therefore the system considers that the deadlock
will definitely occur. In order to get rid of
deadlocks, The OS periodically checks the system
for any deadlock. In case, it finds any of the
deadlock then the OS will recover the system
using some recovery techniques.
The main task of the OS is detecting the
deadlocks. The OS can detect the deadlocks with
the help of Resource allocation graph.
In single instanced resource types, if a cycle is
being formed in the system then there will
definitely be a deadlock. On the other hand, in
multiple instanced resource type graph, detecting
a cycle is not just enough. We have to apply the
safety algorithm on the system by converting the
resource allocation graph into the allocation
matrix and request matrix.
In order to recover the system from deadlocks,
either OS considers resources or processes.
For Resource

Preempt the resource


We can snatch one of the resources from the
owner of the resource (process) and give it to
the other process with the expectation that it will
complete the execution and will release this
resource sooner. Well, choosing a resource which
will be snatched is going to be a bit difficult.
Rollback to a safe state
System passes through various states to get into
the deadlock state. The operating system
canrollback the system to the previous safe
state. For this purpose, OS needs to implement
check pointing at every state.
The moment, we get into deadlock, we will
rollback all the allocations to get into the
previous safe state.
For Process

Kill a process
Killing a process can solve our problem but the
bigger concern is to decide which process to kill.
Generally, Operating system kills a process which
has done least amount of work until now.
Kill all process
This is not a suggestible approach but can be
implemented if the problem becomes very
serious. Killing all process will lead to inefficiency
in the system because all the processes will
execute again from starting.
Banker's Algorithm in Operating System

It is a banker algorithm used to avoid


deadlock and allocate resources safely to each
process in the computer system. The 'S-
State' examines all possible tests or activities
before deciding whether the allocation should be
allowed to each process. It also helps the
operating system to successfully share the
resources between all the processes. The
banker's algorithm is named because it checks
whether a person should be sanctioned a loan
amount or not to help the bank system safely
simulate allocation resources. In this section, we
will learn the Banker's Algorithm in detail. Also,
we will solve problems based on the Banker's
Algorithm. To understand the Banker's Algorithm
first we will see a real word example of it.
Suppose the number of account holders in a
particular bank is 'n', and the total money in a
bank is 'T'. If an account holder applies for a
loan; first, the bank subtracts the loan amount
from full cash and then estimates the cash
difference is greater than T to approve the loan
amount. These steps are taken because if
another person applies for a loan or withdraws
some amount from the bank, it helps the bank
manage and operate all things without any
restriction in the functionality of the banking
system.

Similarly, it works in an operating system. When a


new process is created in a computer system,
the process must provide all types of information
to the operating system like upcoming processes,
requests for their resources, counting them, and
delays. Based on these criteria, the operating
system decides which process sequence should
be executed or waited so that no deadlock occurs
in a system. Therefore, it is also known
as deadlock avoidance algorithm or deadlock
detection in the operating system.
Advantages
Following are the essential characteristics of the
Banker's algorithm:
1. It contains various resources that meet
the requirements of each process.
2. Each process should provide information
to the operating system for upcoming
resource requests, the number of resources,
and how long the resources will be held.
3. It helps the operating system manage
and control process requests for each type of
resource in the computer system.
4. The algorithm has a Max resource
attribute that represents indicates each
process can hold the maximum number of
resources in a system.
Disadvantages
1. It requires a fixed number of processes,
and no additional processes can be started in
the system while executing the process.
2. The algorithm does no longer allows the
processes to exchange its maximum needs
while processing its tasks.
3. Each process has to know and state their
maximum resource requirement in advance
for the system.
4. The number of resource requests can be
granted in a finite time, but the time limit for
allocating the resources is one year.
When working with a banker's algorithm, it
requests to know about three things:
1. How much each process can request for
each resource in the system. It is denoted by
the [MAX] request.
2. How much each process is currently
holding each resource in a system. It is
denoted by the [ALLOCATED] resource.
3. It represents the number of each
resource currently available in the system. It
is denoted by the [AVAILABLE] resource.

Following are the important data structures


terms applied in the banker's algorithm as
follows:
Suppose n is the number of processes, and m is
the number of each type of resource used in a
computer system.

1. Available: It is an array of length 'm' that


defines each type of resource available in the
system. When Available[j] = K, means that
'K' instances of Resources type R[j] are
available in the system.
2. Max: It is a [n x m] matrix that indicates
each process P[i] can store the maximum
number of resources R[j] (each type) in a
system.
3. Allocation: It is a matrix of m x n orders
that indicates the type of resources currently
allocated to each process in the system.
When Allocation [i, j] = K, it means that
process P[i] is currently allocated K instances
of Resources type R[j] in the system.
4. Need: It is an M x N matrix sequence
representing the number of remaining
resources for each process. When the
Need[i] [j] = k, then process P[i] may
require K more instances of resources type
Rj to complete the assigned work.
Nedd[i][j] = Max[i][j] - Allocation[i][j].
5. Finish: It is the vector of the order m. It
includes a Boolean value (true/false)
indicating whether the process has been
allocated to the requested resources, and all
resources have been released after finishing
its task.
The Banker's Algorithm is the combination of the
safety algorithm and the resource request
algorithm to control the processes and avoid
deadlock in a system:
MEMORY MANAGEMENT

What is Memory?
Computer memory can be defined as a collection
of some data represented in the binary format.
On the basis of various functions, memory can
be classified into various categories. We will
discuss each one of them later in detail.
A computer device that is capable to store any
information or data temporally or permanently, is
called storage device.
How Data is being stored in a computer system?
In order to understand memory management,
we have to make everything clear about how
data is being stored in a computer system.
Machine understands only binary language that is
0 or 1. Computer converts every data into binary
language first and then stores it into the
memory.
That means if we have a program line written
as int α = 10 then the computer converts it into
the binary language and then store it into the
memory blocks.

The representation of inti = 10 is shown below.

The binary representation of 10 is 1010. Here,


we are considering 32 bit system therefore, the
size of int is 2 bytes i.e. 16 bit. 1 memory block
stores 1 bit. If we are using signed integer then
the most significant bit in the memory array is
always a signed bit.
Signed bit value 0 represents positive integer
while 1 represents negative integer. Here, the
range of values that can be stored using the
memory array is -32768 to +32767.
well, we can enlarge this range by using
unsigned int. in that case, the bit which is now
storing the sign will also store the bit value and
therefore the range will be 0 to 65,535.
Need for Multi programming
However, The CPU can directly access the main
memory, Registers and cache of the system. The
program always executes in main memory. The
size of main memory affects degree of Multi
programming to most of the extant. If the size of
the main memory is larger than CPU can load
more processes in the main memory at the same
time and therefore will increase degree of Multi
programming as well as CPU utilization.
ain memory affects degree of Multi programming
to most of the extant. If the size of the main
memory is larger than CPU can load more
processes in the main memory at the same time
and therefore will increase degree of Multi
programming as well as CPU utilization.
o Let's consider,   
o Process Size = 4 MB  
o Main memory size = 4 MB   
o The process can only reside in the main mem
ory at any time.   
o If the time for which the process does IO is P
,   
o   
o Then,   
o   
o CPU utilization = (1-P)   
o let's say,   
o P = 70%   
o CPU utilization = 30 %   
o Now, increase the memory size, Let's say it i
s 8 MB.   
o Process Size = 4 MB   
o Two processes can reside in the main memor
y at the same time.   
o Let's say the time for which, one process doe
s its IO is P,   
o   
o Then  
o   
o CPU utilization = (1-P^2)   
o let's say P = 70 %   
o CPU utilization = (1-0.49) =0.51 = 51 %   
Therefore, we can state that the CPU utilization
will be increased if the memory size gets
increased.

Memory Management
In this article, we will understand memory
management in detail.

What do you mean by memory management?

Memory is the important part of the computer


that is used to store the data. Its management is
critical to the computer system because the
amount of main memory available in a computer
system is very limited. At any time, many
processes are competing for it. Moreover, to
increase performance, several processes are
executed simultaneously. For this, we must keep
several processes in the main memory, so it is
even more important to manage them
effectively.
Memory management plays several roles in a
computer system.
Following are the important roles in a computer
system:
o Memory manager is used to keep track of the
status of memory locations, whether it is free
or allocated. It addresses primary memory
by providing abstractions so that software
perceives a large memory is allocated to it.
o Memory manager permits computers with a
small amount of main memory to execute
programs larger than the size or amount of
available memory. It does this by moving
information back and forth between primary
memory and secondary memory by using the
concept of swapping.
o The memory manager is responsible for
protecting the memory allocated to each
process from being corrupted by another
process. If this is not ensured, then the
system may exhibit unpredictable behavior.
o Memory managers should enable sharing of
memory space between processes. Thus, two
programs can reside at the same memory
location although at different times.
Swapping in Operating System
Swapping is a memory management scheme in
which any process can be temporarily swapped
from main memory to secondary memory so
that the main memory can be made available
for other processes. It is used to improve main
memory utilization. In secondary memory, the
place where the swapped-out process is stored
is called swap space.
The purpose of the swapping in operating
system is to access the data present in the hard
disk and bring it to RAM so that the application
programs can use it. The thing to remember is
that swapping is used only when data is not
present in RAM.
Although the process of swapping affects the
performance of the system, it helps to run
larger and more than one process. This is the
reason why swapping is also referred to as
memory compaction.
The concept of swapping has divided into two
more concepts: Swap-in and Swap-out.
o Swap-out is a method of removing a
process from RAM and adding it to the hard
disk.
o Swap-in is a method of removing a program
from a hard disk and putting it back into the
main memory or RAM.

Example: Suppose the user process's size is


2048KB and is a standard hard disk where
swapping has a data transfer rate of 1Mbps.
Now we will calculate how long it will take to
transfer from main memory to secondary
memory.
o User process size is 2048Kb  
o Data transfer rate is 1Mbps = 1024 kbps  
o Time = process size / transfer rate  
o      = 2048 / 1024  
o      = 2 seconds  
o      = 2000 milliseconds  
o Now taking swap-in and swap-out time, the 
process will take 4000 milliseconds. 
o   

Requirements of Memory Management System


Memory management keeps track of the status of each
memory location, whether it is allocated or free. It allocates
the memory dynamically to the programs at their request and
frees it for reuse when it is no longer needed. Memory
management meant to satisfy some requirements that we
should keep in mind.
These Requirements of memory management are:
o Relocation – The available memory is generally shared
among a number of processes in a multiprogramming
system, so it is not possible to know in advance which
other programs will be resident in main memory at the
time of execution of his program. Swapping the active
processes in and out of the main memory enables the
operating system to have a larger pool of ready-to-
execute process.

When a program gets swapped out to a disk memory,


then it is not always possible that when it is swapped
back into main memory then it occupies the previous
memory location, since the location may still be
occupied by another process. We may need
to relocate the process to a different area of memory.
Thus there is a possibility that program may be moved
in main memory due to swapping.
The figure depicts a process image. The process image
is occupying a continuous region of main memory. The
operating system will need to know many things
including the location of process control information,
the execution stack, and the code entry. Within a
program, there are memory references in various
instructions and these are called logical addresses.

After loading of the program into main memory, the


processor and the operating system must be able to
translate logical addresses into physical addresses.
Branch instructions contain the address of the next
instruction to be executed. Data reference instructions
contain the address of byte or word of data referenced.
o Protection – There is always a danger when we have
multiple programs at the same time as one program
may write to the address space of another program. So
every process must be protected against unwanted
interference when other process tries to write in a
process whether accidental or incidental. Between
relocation and protection requirement a trade-off occurs
as the satisfaction of relocation requirement increases
the difficulty of satisfying the protection requirement.

Prediction of the location of a program in main


memory is not possible, that’s why it is impossible to
check the absolute address at compile time to assure
protection. Most of the programming language allows
the dynamic calculation of address at run time. The
memory protection requirement must be satisfied by
the processor rather than the operating system because
the operating system can hardly control a process when
it occupies the processor. Thus it is possible to check
the validity of memory references.
o Sharing – A protection mechanism must have to allow
several processes to access the same portion of main
memory. Allowing each processes access to the same
copy of the program rather than have their own
separate copy has an advantage.

For example, multiple processes may use the same


system file and it is natural to load one copy of the file
in main memory and let it shared by those processes. It
is the task of Memory management to allow controlled
access to the shared areas of memory without
compromising the protection. Mechanisms are used to
support relocation supported sharing capabilities.
o Logical organization – Main memory is organized as
linear or it can be a one-dimensional address space
which consists of a sequence of bytes or words. Most
of the programs can be organized into modules, some
of those are unmodifiable (read-only, execute only) and
some of those contain data that can be modified. To
effectively deal with a user program, the operating
system and computer hardware must support a basic
module to provide the required protection and sharing.
It has the following advantages:
o Modules are written and compiled independently

and all the references from one module to


another module are resolved by `the system at
run time.
o Different modules are provided with different

degrees of protection.
o There are mechanisms by which modules can be

shared among processes. Sharing can be


provided on a module level that lets the user
specify the sharing that is desired.
o Physical organization – The structure of computer
memory has two levels referred to as main memory and
secondary memory. Main memory is relatively very
fast and costly as compared to the secondary memory.
Main memory is volatile. Thus secondary memory is
provided for storage of data on a long-term basis while
the main memory holds currently used programs. The
major system concern between main memory and
secondary memory is the flow of information and it is
impractical for programmers to understand this for two
reasons:
o The programmer may engage in a practice

known as overlaying when the main memory


available for a program and its data may be
insufficient. It allows different modules to be
assigned to the same region of memory. One
disadvantage is that it is time-consuming for the
programmer.
o In a multiprogramming environment, the

programmer does not know how much space


will be available at the time of coding and where
that space will be located inside the memory.

Difference between Loading and Linking


Linking and Loading are the utility programs that play a
important role in the execution of a program. Linking intakes
the object codes generated by the assembler and combines
them to generate the executable module. On the other hand,
the loading loads this executable module to the main memory
for execution. 
Loading: 
Bringing the program from secondary memory to main
memory is called Loading. 
Linking: 
Establishing the linking between all the modules or all the
functions of the program in order to continue the program
execution is called linking. 
 

Differences between Linking and Loading: 


 
o The key difference between linking and loading is that
the linking generates the executable file of a program
whereas, the loading loads the executable file obtained
from the linking into main memory for execution.
o The linking intakes the object module of a program

generated by the assembler. However, the loading


intakes the executable module generated by the linking.
o The linking combines all object modules of a program

to generate executable modules it also links the library


function in the object module to built-in libraries of the
high-level programming language. On the other hand,
loading allocates space to an executable module in
main memory.
o Loading and Linking are further categorized into 2
types: 
o  
Static Dynamic
Loading the entire program
into the main memory Loading the program into the
before start of the program main memory on demand is
execution is called as static called as dynamic loading.
loading.
Inefficient utilization of
memory because whether it
is required or not required Efficient utilization of memory.
entire program is brought
into the main memory.
Program execution will be Program execution will be
faster. slower.
Statically linked program
Dynamic linking is performed at
takes constant load time
run time by the operating
every time it is loaded into
system.
the memory for execution.
If the static loading is used If the dynamic loading is used
then accordingly static then accordingly dynamic
linking is applied. linking is applied.
Static linking is performed In dynamic linking this is not the
by programs called linkers as case and individual shared
the last step in compiling a modules can be updated and
program. Linkers are also recompiled.This is one of the
called link editors. greatest advantages dynamic
Static Dynamic
linking offers.
In static linking if any of the
external programs has
In dynamic linking load time
changed then they have to
might be reduced if the shared
be recompiled and re-linked
library code is already present
again else the changes won’t
in memory.
reflect in existing executable
file.

Memory management Techniques:

The Memory management Techniques can be classified


into following main categories:

o Contiguous memory management schemes


o Non-Contiguous memory management
schemes
Contiguous memory management schemes:
In a Contiguous memory management scheme,
each program occupies a single contiguous block
of storage locations, i.e., a set of memory
locations with consecutive addresses.
Single contiguous memory management schemes:
The Single contiguous memory management
scheme is the simplest memory management
scheme used in the earliest generation of
computer systems. In this scheme, the main
memory is divided into two contiguous areas or
partitions. The operating systems reside
permanently in one partition, generally at the
lower memory, and the user process is loaded
into the other partition.

Advantages of Single contiguous memory management


schemes:

o Simple to implement.
o Easy to manage and design.
o In a Single contiguous memory management
scheme, once a process is loaded, it is given
full processor's time, and no other processor
will interrupt it.

Disadvantages of Single contiguous memory


management schemes:

o Wastage of memory space due to unused


memory as the process is unlikely to use all
the available memory space.
o The CPU remains idle, waiting for the disk to
load the binary image into the main memory.
o It can not be executed if the program is too
large to fit the entire available main memory
space.
o It does not support multiprogramming, i.e., it
cannot handle multiple programs
simultaneously.
Multiple Partitioning:
The single Contiguous memory management
scheme is inefficient as it limits computers to
execute only one program at a time resulting in
wastage in memory space and CPU time. The
problem of inefficient CPU use can be overcome
using multiprogramming that allows more than
one program to run concurrently. To switch
between two processes, the operating systems
need to load both processes into the main
memory. The operating system needs to divide
the available main memory into multiple parts to
load multiple processes into the main memory.
Thus multiple processes can reside in the main
memory simultaneously.

The multiple partitioning schemes can be of two types:

o Fixed Partitioning
o Dynamic Partitioning
Fixed Partitioning
The main memory is divided into several fixed-
sized partitions in a fixed partition memory
management scheme or static partitioning. These
partitions can be of the same size or different
sizes. Each partition can hold a single process.
The number of partitions determines the degree
of multiprogramming, i.e., the maximum number
of processes in memory. These partitions are
made at the time of system generation and
remain fixed after that.

Advantages of Fixed Partitioning memory management


schemes:

o Simple to implement.
o Easy to manage and design.

Disadvantages of Fixed Partitioning memory


management schemes:

o This scheme suffers from internal


fragmentation.
o The number of partitions is specified at the
time of system generation.
o This scheme suffers from internal
fragmentation.
o The number of partitions is specified at the
time of system generation.
Fixed Partitioning
The earliest and one of the simplest technique
which can be used to load more than one
processes into the main memory is Fixed
partitioning or Contiguous memory allocation.
In this technique, the main memory is divided
into partitions of equal or different sizes. The
operating system always resides in the first
partition while the other partitions can be used to
store user processes. The memory is assigned to
the processes in contiguous way.
In fixed partitioning,
o The partitions cannot overlap.
o A process must be contiguously present in a
partition for the execution.
There are various cons of using this technique.

1. Internal Fragmentation

If the size of the process is lesser then the total


size of the partition then some size of the
partition get wasted and remain unused. This is
wastage of the memory and called internal
fragmentation.
As shown in the image below, the 4 MB partition
is used to load only 3 MB process and the
remaining 1 MB got wasted.

. External Fragmentation
The total unused space of various partitions
cannot be used to load the processes even
though there is space available but not in the
contiguous form.
As shown in the image below, the remaining 1
MB space of each partition cannot be used as a
unit to store a 4 MB process. Despite of the fact
that the sufficient space is available to load the
process, process will not be loaded.
3. Limitation on the size of the process
If the process size is larger than the size of
maximum sized partition then that process
cannot be loaded into the memory. Therefore, a
limitation can be imposed on the process size
that is it cannot be larger than the size of the
largest partition.
4. Degree of multiprogramming is less
By Degree of multi programming, we simply
mean the maximum number of processes that
can be loaded into the memory at the same time.
In fixed partitioning, the degree of
multiprogramming is fixed and very less due to
the fact that the size of the partition cannot be
varied according to the size of processes.
Dynamic Partitioning
The dynamic partitioning was designed to
overcome the problems of a fixed partitioning
scheme. In a dynamic partitioning scheme, each
process occupies only as much memory as they
require when loaded for processing. Requested
processes are allocated memory until the entire
physical memory is exhausted or the remaining
space is insufficient to hold the requesting
process. In this scheme the partitions used are of
variable size, and the number of partitions is not
defined at the system generation time.

Advantages of Dynamic Partitioning memory


management schemes:

o Simple to implement.
o Easy to manage and design.

Disadvantages of Dynamic Partitioning memory


management schemes:

o This scheme also suffers from internal


fragmentation.
o The number of partitions is specified at the
time of system segmentation.
Dynamic Partitioning
Dynamic partitioning tries to overcome the
problems caused by fixed partitioning. In this
technique, the partition size is not declared
initially. It is declared at the time of process
loading.
The first partition is reserved for the operating
system. The remaining space is divided into
parts. The size of each partition will be equal to
the size of the process. The partition size varies
according to the need of the process so that the
internal fragmentation can be avoided.
Advantages of Dynamic Partitioning over fixed
partitioning

1. No Internal Fragmentation
Given the fact that the partitions in dynamic
partitioning are created according to the need of
the process, It is clear that there will not be any
internal fragmentation because there will not be
any unused remaining space in the partition.
2. No Limitation on the size of the process
In Fixed partitioning, the process with the size
greater than the size of the largest partition
could not be executed due to the lack of
sufficient contiguous memory. Here, In Dynamic
partitioning, the process size can't be restricted
since the partition size is decided according to
the process size.
3. Degree of multiprogramming is dynamic
Due to the absence of internal fragmentation,
there will not be any unused space in the
partition hence more processes can be loaded in
the memory at the same time.
Disadvantages of dynamic partitioning
External Fragmentation
Absence of internal fragmentation doesn't mean
that there will not be external fragmentation.
Let's consider three processes P1 (1 MB) and P2
(3 MB) and P3 (1 MB) are being loaded in the
respective partitions of the main memory.
After some time P1 and P3 got completed and
their assigned space is freed. Now there are two
unused partitions (1 MB and 1 MB) available in
the main memory but they cannot be used to
load a 2 MB process in the memory since they
are not contiguously located.
The rule says that the process must be
contiguously present in the main memory to get
executed. We need to change this rule to avoid
external fragmentation.
Complex Memory Allocation
In Fixed partitioning, the list of partitions is made
once and will never change but in dynamic
partitioning, the allocation and deallocation is
very complex since the partition size will be
varied every time when it is assigned to a new
process. OS has to keep track of all the
partitions.
Due to the fact that the allocation and
deallocation are done very frequently in dynamic
memory allocation and the partition size will be
changed at each time, it is going to be very
difficult for OS to manage everything.

Non-Contiguous memory management schemes:


In a Non-Contiguous memory management
scheme, the program is divided into different
blocks and loaded at different portions of the
memory that need not necessarily be adjacent to
one another. This scheme can be classified
depending upon the size of blocks and whether
the blocks reside in the main memory or not.
What is paging?
Paging is a technique that eliminates the
requirements of contiguous allocation of main
memory. In this, the main memory is divided
into fixed-size blocks of physical memory called
frames. The size of a frame should be kept the
same as that of a page to maximize the main
memory and avoid external fragmentation.

Advantages of paging:

o Pages reduce external fragmentation.


o Simple to implement.
o Memory efficient.
o Due to the equal size of frames, swapping
becomes very easy.
o It is used for faster access of data.
Paging with Example
In Operating Systems, Paging is a storage
mechanism used to retrieve processes from the
secondary storage into the main memory in the
form of pages.
The main idea behind the paging is to divide
each process in the form of pages. The main
memory will also be divided in the form of
frames.
One page of the process is to be stored in one of
the frames of the memory. The pages can be
stored at the different locations of the memory
but the priority is always to find the contiguous
frames or holes.
Pages of the process are brought into the main
memory only when they are required otherwise
they reside in the secondary storage.
Different operating system defines different
frame sizes. The sizes of each frame must be
equal. Considering the fact that the pages are
mapped to the frames in Paging, page size needs
to be as same as frame size.
Example
Let us consider the main memory size 16 Kb and
Frame size is 1 KB therefore the main memory
will be divided into the collection of 16 frames of
1 KB each.
There are 4 processes in the system that is P1,
P2, P3 and P4 of 4 KB each. Each process is
divided into pages of 1 KB each so that one page
can be stored in one frame.
Initially, all the frames are empty therefore
pages of the processes will get stored in the
contiguous way.
Frames, pages and the mapping between the two
is shown in the image below.

Let us consider that, P2 and P4 are moved to


waiting state after some time. Now, 8 frames
become empty and therefore other pages can be
loaded in that empty place. The process P5 of
size 8 KB (8 pages) is waiting inside the ready
queue.
Given the fact that, we have 8 non contiguous
frames available in the memory and paging
provides the flexibility of storing the process at
the different places. Therefore, we can load the
pages of process P5 in the place of P2 and P4.
Memory Management Unit
The purpose of Memory Management Unit (MMU)
is to convert the logical address into the physical
address. The logical address is the address
generated by the CPU for every page while the
physical address is the actual address of the
frame where each page will be stored.
When a page is to be accessed by the CPU by
using the logical address, the operating system
needs to obtain the physical address to access
that page physically.
The logical address has two parts.
o Page Number
o Offset
Memory management unit of OS needs to
convert the page number to the frame number.
Page Table
Page Table is a data structure used by the virtual
memory system to store the mapping between
logical addresses and physical addresses.
Logical addresses are generated by the CPU for
the pages of the processes therefore they are
generally used by the processes.
Physical addresses are the actual frame address
of the memory. They are generally used by the
hardware or more specifically by RAM
subsystems.
The CPU always accesses the processes through
their logical addresses. However, the main
memory recognizes physical address only.
In this situation, a unit named as Memory
Management Unit comes into the picture. It
converts the page number of the logical address
to the frame number of the physical address. The
offset remains same in both the addresses.
To perform this task, Memory Management unit
needs a special kind of mapping which is done by
page table. The page table stores all the Frame
numbers corresponding to the page numbers of
the page table.
In other words, the page table maps the page
number to its actual location (frame number) in
the memory.
In the image given below shows, how the
required word of the frame is accessed with the
help of offset.
Size of the page table
However, the part of the process which is being
executed by the CPU must be present in the
main memory during that time period. The page
table must also be present in the main memory
all the time because it has the entry for all the
pages.
The size of the page table depends upon the
number of entries in the table and the bytes
stored in one entry.

Let's consider,

o Logical Address = 24 bits   
o Logical Address space = 2 ^ 24 bytes   
o Let's say, Page size = 4 KB = 2 ^ 12 Bytes   
o Page offset = 12  
o Number of bits in a page = Logical Address - 
Page Offset = 24 - 12 = 12 bits   
o Number of pages = 2 ^ 12 = 2 X 2 X 10 ^ 1
0 = 4 KB  
o Let's say, Page table entry = 1 Byte  
o Therefore, the size of the page table = 4 KB 
X 1 Byte = 4 KB   
Here we are lucky enough to get the page table
size equal to the frame size. Now, the page table
will be simply stored in one of the frames of the
main memory. The CPU maintains a register
which contains the base address of that frame,
every page number from the logical address will
first be added to that base address so that we
can access the actual location of the word being
asked.
However, in some cases, the page table size and
the frame size might not be same. In those
cases, the page table is considered as the
collection of frames and will be stored in the
different frames.

What is Segmentation?
Segmentation is a technique that eliminates the
requirements of contiguous allocation of main
memory. In this, the main memory is divided
into variable-size blocks of physical memory
called segments. It is based on the way the
programmer follows to structure their programs.
With segmented memory allocation, each job is
divided into several segments of different sizes,
one for each module. Functions, subroutines,
stack, array, etc., are examples of such modules.

o
Segmentation
In Operating Systems, Segmentation is a
memory management technique in which the
memory is divided into the variable size parts.
Each part is known as a segment which can be
allocated to a process.
The details about each segment are stored in a
table called a segment table. Segment table is
stored in one (or many) of the segments.
Segment table contains mainly two information
about segment:
o Base: It is the base address of the segment
o Limit: It is the length of the segment.
Why Segmentation is required?
Till now, we were using Paging as our main
memory management technique. Paging is more
close to the Operating system rather than the
User. It divides all the processes into the form of
pages regardless of the fact that a process can
have some relative parts of functions which need
to be loaded in the same page.
Operating system doesn't care about the User's
view of the process. It may divide the same
function into different pages and those pages
may or may not be loaded at the same time into
the memory. It decreases the efficiency of the
system.
It is better to have segmentation which divides
the process into the segments. Each segment
contains the same type of functions such as the
main function can be included in one segment
and the library functions can be included in the
other segment.

other segment.
Translation of Logical address into physical address
by segment table
CPU generates a logical address which contains
two parts:
o Segment Number
o Offset

For Example:
Suppose a 16 bit address is used with 4 bits for
the segment number and 12 bits for the segment
offset so the maximum segment size is 4096 and
the maximum number of segments that can be
refereed is 16.
When a program is loaded into memory, the
segmentation system tries to locate space that is
large enough to hold the first segment of the
process, space information is obtained from the
free list maintained by memory manager. Then it
tries to locate space for other segments. Once
adequate space is located for all the segments, it
loads them into their respective areas.
The operating system also generates a segment
map table for each program.

With the help of segment map tables and


hardware assistance, the operating system can
easily translate a logical address into physical
address on execution of a program.
The Segment number is mapped to the segment
table. The limit of the respective segment is
compared with the offset. If the offset is less
than the limit then the address is valid otherwise
it throws an error as the address is invalid.
In the case of valid addresses, the base address
of the segment is added to the offset to get the
physical address of the actual word in the main
memory.
The above figure shows how address translation
is done in case of segmentation.
Advantages of Segmentation
o No internal fragmentation

o Average Segment Size is larger than the

actual page size.


o Less overhead

o It is easier to relocate segments than entire

address space.
o The segment table is of lesser size as
compared to the page table in paging.
Disadvantages
o It can have external fragmentation.

o it is difficult to allocate contiguous memory

to variable sized partition.


o Costly memory management algorithms.

Paging VS Segmentation
Sr Paging Segmentatio
N n
o.

1 Non- Non-
Contiguous contiguous
memory memory
allocation allocation

2 Paging Segmentati
divides on divides
program program
into fixed into
size pages. variable
size
segments.

3 OS is Compiler is
responsible responsible
.

4 Paging is Segmentati
faster than on is
segmentati slower
on than
paging

5 Paging is Segmentati
closer to on is closer
Operating to User
System

6 It suffers It suffers
from from
internal external
fragmentat fragmentat
ion ion

7 There is no There is no
external external
fragmentat fragmentat
ion ion

8 Logical Logical
address is address is
divided divided
into page into
number segment
and page number
offset and
segment
offset

9 Page table Segment


is used to Table
maintain maintains
the page the
information segment
. information

10 Page table Segment


entry has table entry
the frame has the
number base
and some address of
flag bits to the
represent segment
details and some
about protection
pages. bits for the
segments.

Physical and Logical Address Space


Physical Address Space
Physical address space in a system can be
defined as the size of the main memory. It is
really important to compare the process size with
the physical address space. The process size
must be less than the physical address space.

Physical Address Space = Size of the Main


Memory

If, physical address space = 64 KB = 2 ^ 6 KB =


2 ^ 6 X 2 ^ 10 Bytes = 2 ^ 16 bytes

Let us consider,
word size = 8 Bytes = 2 ^ 3 Bytes

Hence,
Physical address space (in words) = (2 ^ 16) /
(2 ^ 3) = 2 ^ 13 Words

Therefore,
Physical Address = 13 bits

In General,
If, Physical Address Space = N Words

then, Physical Address = log2 N


Logical Address Space
Logical address space can be defined as the size
of the process. The size of the process should be
less enough so that it can reside in the main
memory.
Let's say,
Logical Address Space = 128 MB = (2 ^ 7 X 2 ^
20) Bytes = 2 ^ 27 Bytes
Word size = 4 Bytes = 2 ^ 2 Bytes

Logical Address Space (in words) = (2 ^ 27) / (2


^ 2) = 2 ^ 25 Words
Logical Address = 25 Bits

In general,
If, logical address space = L words
Then, Logical Address = Log2L bits
What is a Word?
The Word is the smallest unit of the memory. It
is the collection of bytes. Every operating system
defines different word sizes after analyzing the n-
bit address that is inputted to the decoder and
the 2 ^ n memory locations that are produced
from the decoder.
Virtual Memory
Virtual Memory is a storage scheme that provides
user an illusion of having a very big main
memory. This is done by treating a part of
secondary memory as the main memory.
In this scheme, User can load the bigger size
processes than the available main memory by
having the illusion that the memory is available
to load the process.
Instead of loading one big process in the main
memory, the Operating System loads the
different parts of more than one process in the
main memory.
By doing this, the degree of multiprogramming
will be increased and therefore, the CPU
utilization will also be increased.
How Virtual Memory Works?
In modern word, virtual memory has become
quite common these days. In this scheme,
whenever some pages needs to be loaded in the
main memory for the execution and the memory
is not available for those many pages, then in
that case, instead of stopping the pages from
entering in the main memory, the OS search for
the RAM area that are least used in the recent
times or that are not referenced and copy that
into the secondary memory to make the space
for the new pages in the main memory.
Since all this procedure happens automatically,
therefore it makes the computer feel like it is
having the unlimited RAM.
Demand Paging
Demand Paging is a popular method of virtual
memory management. In demand paging, the
pages of a process which are least used, get
stored in the secondary memory.
A page is copied to the main memory when its
demand is made or page fault occurs. There are
various page replacement algorithms which are
used to determine the pages which will be
replaced. We will discuss each one of them later
in detail.
Snapshot of a virtual memory management system
Let us assume 2 processes, P1 and P2, contains
4 pages each. Each page size is 1 KB. The main
memory contains 8 frame of 1 KB each. The OS
resides in the first two partitions. In the third
partition, 1st page of P1 is stored and the other
frames are also shown as filled with the different
pages of processes in the main memory.
The page tables of both the pages are 1 KB size
each and therefore they can be fit in one frame
each. The page tables of both the processes
contain various information that is also shown in
the image.
The CPU contains a register which contains the
base address of page table that is 5 in the case
of P1 and 7 in the case of P2. This page table
base address will be added to the page number
of the Logical address when it comes to
accessing the actual corresponding entry.
entry.
Advantages of Virtual Memory
o The degree of Multiprogramming will be
increased.
o User can run large application with less real

RAM.
o There is no need to buy more memory RAMs.

Disadvantages of Virtual Memory


o The system becomes slower since swapping

takes time.
o It takes more time in switching between
applications.
o The user will have the lesser hard disk space
for its use.
Page Fault Handling in Operating System
In this article, you will learn about page fault
handling in the operating system and its steps.
What is Page Fault in Operating System?

Page faults dominate more like an error. A page


fault will happen if a program tries to access a
piece of memory that does not exist in physical
memory (main memory). The fault specifies the
operating system to trace all data into virtual
memory management and then relocate it from
secondary memory to its primary memory, such
as a hard disk.
A page fault trap occurs if the requested page is
not loaded into memory. The page fault primarily
causes an exception, which is used to notify the
operating system to retrieve the "pages" from
virtual memory to continue operation. Once all of
the data has been placed into physical memory,
the program resumes normal operation. The
Page fault process occurs in the background, and
thus the user is unaware of it.
o The computer's hardware track to the kernel
and the program counter is often saved on
the stack. The CPU registers hold information
about the current state of instruction.
o An assembly program is started, which saves
the general registers and other volatile data
to prevent the Operating system from
destroying it.
Page Fault Handling
A Page Fault happens when you access a page
that has been marked as invalid. The paging
hardware would notice that the invalid bit is set
while translating the address across the page
table, which will cause an operating system trap.
The trap is caused primarily by the OS's failure to
load the needed page into memory.
Now, let's understand the procedure of page fault
handling in the OS:
o assess whether the reference was valid or
invalid memory access.
o If the reference becomes invalid, the system
process would be terminated. Otherwise, the
page will be paged in.
o After that, the free-frame list finds the free
frame in the system.
o Now, the disk operation would be scheduled
to get the required page from the disk.
o When the I/O operation is completed, the
process's page table will be updated with a
new frame number, and the invalid bit will be
changed. Now, it is a valid page reference.
o If any page fault is found, restart these steps
from starting.
Page Fault Terminology
There are various page fault terminologies in the
operating system. Some terminologies of page
fault are as follows:

1. Page Hit

When the CPU attempts to obtain a needed page


from main memory and the page exists in main
memory (RAM), it is referred to as a "PAGE HIT".

2. Page Miss

If the needed page has not existed in the main


memory (RAM), it is known as "PAGE MISS".

3. Page Fault Time

The time it takes to get a page from secondary


memory and recover it from the main memory
after loading the required page is known as "PAGE
FAULT TIME".

4. Page Fault Delay

The rate at which threads locate page faults in


memory is referred to as the "PAGE FAULT RATE".
The page fault rate is measured per second.

5. Hard Page Fault

If a required page exists in the hard disk's page


file, it is referred to as a "HARD PAGE FAULT".
6. Soft Page Fault

If a required page is not located on the hard disk


but is found somewhere else in memory, it is
referred to as a "SOFT PAGE FAULT".

7. Minor Page Fault

If a process needs data and that data exists in


memory but is being allotted to another process
at the same moment, it is referred to as
a "MINOR PAGE FAULT".

Page Replacement Algorithms


The page replacement algorithm decides which
memory page is to be replaced. The process of
replacement is sometimes called swap out or
write to disk. Page replacement is done when the
requested page is not found in the main memory
(page fault).
There are two main aspects of virtual memory,
Frame allocation and Page Replacement. It is
very important to have the optimal frame
allocation and page replacement algorithm.
Frame allocation is all about how many frames
are to be allocated to the process while the page
replacement is all about determining the page
number which needs to be replaced in order to
make space for the requested page.
What If the algorithm is not optimal?
1. if the number of frames which are allocated to
a process is not sufficient or accurate then there
can be a problem of thrashing. Due to the lack of
frames, most of the pages will be residing in the
main memory and therefore more page faults will
occur.
However, if OS allocates more frames to the
process then there can be internal
fragmentation.
2. If the page replacement algorithm is not
optimal then there will also be the problem of
thrashing. If the number of pages that are
replaced by the requested pages will be referred
in the near future then there will be more
number of swap-in and swap-out and therefore
the OS has to perform more replacements then
usual which causes performance deficiency.
Therefore, the task of an optimal page
replacement algorithm is to choose the page
which can limit the thrashing.
Types of Page Replacement Algorithms

There are various page replacement algorithms.


Each algorithm has a different method by which
the pages can be replaced.
o Optimal Page Replacement algorithm → this
algorithms replaces the page which will not
be referred for so long in future. Although it
can not be practically implementable but it
can be used as a benchmark. Other
algorithms are compared to this in terms of
optimality.
o Least recent used (LRU) page replacement
algorithm → this algorithm replaces the page
which has not been referred for a long time.
This algorithm is just opposite to the optimal
page replacement algorithm. In this, we look
at the past instead of staring at future.
o FIFO → in this algorithm, a queue is
maintained. The page which is assigned the
frame first will be replaced first. In other
words, the page which resides at the rare
end of the queue will be replaced on the
every page fault.
GATE 2015 question on LRU
and FIFO
Q. Consider a main memory
with five page frames and the
following sequence of page
references: 3, 8, 2, 3, 9, 1, 6, 3,
8, 9, 3, 6, 2, 1, 3. which one of
the following is true with
respect to page replacement
policies First-In-First-out
(FIFO) and Least Recently
Used (LRU)?
A. Both incur the same
number of page faults
B. FIFO incurs 2 more page
faults than LRU
C. LRU incurs 2 more page
faults than FIFO
D. FIFO incurs 1 more page
faults than LRU
Solution:
Number of frames = 5
FIFO
According to FIFO, the page
which first comes in the
memory will first goes out.
Number of Page Faults = 9
Number of hits = 6
LRU
According to LRU, the page
which has not been
requested for a long time will
get replaced with the new
one.

Number of Page Faults = 9


Number of Hits = 6
The Number of page faults in
both the cases is equal
therefore the Answer
is option (A).

Numerical on Optimal, LRU and FIFO


Q. Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7,
2. the number of frames in the memory is 3. Find out
the number of page faults respective to:
o Optimal Page Replacement Algorithm

o FIFO Page Replacement Algorithm

o LRU Page Replacement Algorithm

Optimal Page Replacement Algorithm

Number of Page Faults in Optimal Page Replacement


Algorithm = 5
LRU Page Replacement Algorithm

Number of Page Faults in LRU = 6


FIFO Page Replacement Algorithm

Number of Page Faults in FIFO = 6

You might also like